tracker
All repositories: gitoria
2.8 KB
// lib/images.hl — THE IMAGE FILES the pages show (mission 028: out of project.hl, whose routes /posters/:name and// /profiles/:name name the adapters below):// /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'import { postersDir, profilesDir } from './util.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, redirects.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') })}static posterRoute = (&route, &req) => { return imageResponse(&postersDir, route.params != null ? route.params.name : null, 'poster') }static profileRoute = (&route, &req) => { return imageResponse(&profilesDir, route.params != null ? route.params.name : null, 'photo') }
Branches
Latest commits
- 613ae0ecMerge branch 'bs3' into mainTracker
- 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
- 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
- 310613f1Merge branch 'records-dp72' into mainTracker
- 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker
- c41587d2Merge branch 'bs2' into mainTracker
- 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
- 019d5cedMerge branch 'records-1110' into mainTracker
- f88ae8b9|||PR records: day56 toggle live, reviewTracker
- b6b3bb4dMerge branch 'ap56e' into mainTracker
- 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker
- 7c9ec236|||PR AP56e: re-vendor Hybriel set day56 (source 36ae83a8, bin c1f595a2)Tracker
- 4b54f6f9Merge branch 'records-audit' into mainTracker
- cab4a3bc|||PR records: branch audit, PR-onlyTracker
- 641e1c4cMerge branch 'records-dp71' into mainTracker
- 85a6e9f5|||PR records: DP71 checked in ChromeTracker
- f5be5f18records: gitoria PR-only planTracker
- 46a37036records: DP71 liveTracker
- 3bcb7039Status/log: DP71 in progressTracker
- 5b3a5693Log: SR1b merged via PR 2, DP71 startedTracker