tracker
All repositories: gitoria
2.5 KB
// lib/images.hl — THE IMAGE FILES the pages show (mission 028: out of project.hl, whose routes /posters/:name and// /profiles/:name call `imageResponse`):// /posters/<name> a title's poster (tracker.worldapi.org#4) from storage/mpackdb/posters/ — the file name is the title's// oldId + its extension (shows.hl posterName), downloaded by the sync (tmdb.hl syncPoster)// /profiles/<name> a person's photo (tracker.worldapi.org#16) from storage/mpackdb/profiles/ — downloaded on the first visit// of the person page (people.hl syncProfile)// Both fall back to a placeholder svg when the file isn't there yet (the mongo export never included the poster bytes): "no poster",// a person's "no photo" (tracker#37: /people's tiles).import { Response } from 'hl:http1'import { readBytes, exists } from 'hl:fs'import { imageHeaders } from './security.hl'static placeholderPoster = './assets/poster-placeholder.svg'static placeholderPhoto = './assets/photo-placeholder.svg'static posterNameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'// a plain file name: letters, digits, '.', '-', '_', at most 100 (also the genre redirect's check, project.hl genreMoved)static isSafePosterName = (&name) => {if (name == null || hlTypeName(name) != 'String' || name == '' || name.length > 100) { return false }let i = 0while (i < name.length) {if (!posterNameChars.includes(name[i])) { return false }i = i + 1}return true}static endsWithStr = (&s, suffix) => { return s.length >= suffix.length && s.slice(s.length - suffix.length) == suffix }static mimeOfPoster = (&name) => {if (endsWithStr(&name, '.png')) { return 'image/png' }if (endsWithStr(&name, '.webp')) { return 'image/webp' }if (endsWithStr(&name, '.svg')) { return 'image/svg+xml' }return 'image/jpeg'}// the answer for `<dir>/<name>`: the file (a day in the browser's cache), else the placeholder (an hour); a name that is no// plain file name → 400 "bad <what> name"static imageResponse = (&dir, &name, &what) => {if (!isSafePosterName(&name)) { return new Response('bad ' + what + ' name', { status = 400 headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }path = dir + '/' + nameif (exists(path)) { return new Response(readBytes(path), { headers = imageHeaders(mimeOfPoster(&name), 'public, max-age=86400') }) }return new Response(readBytes(what == 'photo' ? placeholderPhoto : placeholderPoster), { headers = imageHeaders('image/svg+xml', 'public, max-age=3600') })}
Branches
Latest commits
- f29e2502log: RV39 gate fixmre
- ad529ac1log: m572 verdictmre
- 1b37d404RV39 on hold: hybriel#308mre
- f9127c68Revert "Merge branch 'rv39'"mre
- cd39f3d3log: hybriel#308mre
- 51341d6elog: RV39 deploy startedmre
- c9e29e06Merge branch 'rv39'mre
- 0f8d1fdclog: RV39 report, browser 387/2mre
- 50249b0bRV39: reportmre
- 10ed9581compose: HL_GC_STATS=1mre
- 955da19bRV39: re-vendor Hybriel set day39 (source 51e921d7, bin 6b481aa7)mre
- 64b8d1dclog: RV39 startedmre
- 1f2df72amission RV39mre
- 6c1936f9log+status: GC planmre
- 2846a840log: m569 result, GC hypothesismre
- b24cd2d5log: Anton on hl:fetch, mission 569mre
- 7d42cac1log+status: SL1 measured, fetch stalls remainmre
- 8e5f2ceflog: SL1 deployedmre
- 29f34212log: SL1 deploy startedmre
- 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre