gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-hm55c9ba258d3|||PR records: HM55b resultTrackerrecords-hm55c/lib/images.hl

2.8 KB

  1. // lib/images.hl — THE IMAGE FILES the pages show (mission 028: out of project.hl, whose routes /posters/:name and
  2. // /profiles/:name name the adapters below):
  3. // /posters/<name> a title's poster (tracker.worldapi.org#4) from storage/mpackdb/posters/ — the file name is the title's
  4. // oldId + its extension (shows.hl posterName), downloaded by the sync (tmdb.hl syncPoster)
  5. // /profiles/<name> a person's photo (tracker.worldapi.org#16) from storage/mpackdb/profiles/ — downloaded on the first visit
  6. // of the person page (people.hl syncProfile)
  7. // Both fall back to a placeholder svg when the file isn't there yet (the mongo export never included the poster bytes): "no poster",
  8. // a person's "no photo" (tracker#37: /people's tiles).
  9. import { Response } from 'hl:http1'
  10. import { readBytes, exists } from 'hl:fs'
  11. import { imageHeaders } from './security.hl'
  12. import { postersDir, profilesDir } from './util.hl'
  13. static placeholderPoster = './assets/poster-placeholder.svg'
  14. static placeholderPhoto = './assets/photo-placeholder.svg'
  15. static posterNameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'
  16. // a plain file name: letters, digits, '.', '-', '_', at most 100 (also the genre redirect's check, redirects.hl genreMoved)
  17. static isSafePosterName = (&name) => {
  18. if (name == null || hlTypeName(name) != 'String' || name == '' || name.length > 100) { return false }
  19. let i = 0
  20. while (i < name.length) {
  21. if (!posterNameChars.includes(name[i])) { return false }
  22. i = i + 1
  23. }
  24. return true
  25. }
  26. static endsWithStr = (&s, suffix) => { return s.length >= suffix.length && s.slice(s.length - suffix.length) == suffix }
  27. static mimeOfPoster = (&name) => {
  28. if (endsWithStr(&name, '.png')) { return 'image/png' }
  29. if (endsWithStr(&name, '.webp')) { return 'image/webp' }
  30. if (endsWithStr(&name, '.svg')) { return 'image/svg+xml' }
  31. return 'image/jpeg'
  32. }
  33. // the answer for `<dir>/<name>`: the file (a day in the browser's cache), else the placeholder (an hour); a name that is no
  34. // plain file name → 400 "bad <what> name"
  35. static imageResponse = (&dir, &name, &what) => {
  36. if (!isSafePosterName(&name)) { return new Response('bad ' + what + ' name', { status = 400 headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }
  37. path = dir + '/' + name
  38. if (exists(path)) { return new Response(readBytes(path), { headers = imageHeaders(mimeOfPoster(&name), 'public, max-age=86400') }) }
  39. return new Response(readBytes(what == 'photo' ? placeholderPhoto : placeholderPoster), { headers = imageHeaders('image/svg+xml', 'public, max-age=3600') })
  40. }
  41. static posterRoute = (&route, &req) => { return imageResponse(&postersDir, route.params != null ? route.params.name : null, 'poster') }
  42. static profileRoute = (&route, &req) => { return imageResponse(&profilesDir, route.params != null ? route.params.name : null, 'photo') }

Branches

Latest commits

  • 9ba258d3|||PR records: HM55b resultTracker
  • 64f2d300Merge branch 'records-hm55b2' into mainTracker
  • ae3b80dc|||PR records: HM55b briefTracker
  • 7ef3cb3bMerge branch 'records-st55' into mainTracker
  • fec2e080|||PR records: STATUS HM55Tracker
  • 8e174918Merge branch 'records-hm55b' into mainTracker
  • b1ecee57|||PR records: HM55 stateTracker
  • 28eab08cMerge branch 'records-hm55' into mainTracker
  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker
  • 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
  • b1496147Merge branch 'ap61' into mainTracker