gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitac58c1e5ac58c1e5mission: HM56mreac58c1e5/lib/images.hl

2.5 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 call `imageResponse`):
  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. static placeholderPoster = './assets/poster-placeholder.svg'
  13. static placeholderPhoto = './assets/photo-placeholder.svg'
  14. static posterNameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'
  15. // a plain file name: letters, digits, '.', '-', '_', at most 100 (also the genre redirect's check, project.hl genreMoved)
  16. static isSafePosterName = (&name) => {
  17. if (name == null || hlTypeName(name) != 'String' || name == '' || name.length > 100) { return false }
  18. let i = 0
  19. while (i < name.length) {
  20. if (!posterNameChars.includes(name[i])) { return false }
  21. i = i + 1
  22. }
  23. return true
  24. }
  25. static endsWithStr = (&s, suffix) => { return s.length >= suffix.length && s.slice(s.length - suffix.length) == suffix }
  26. static mimeOfPoster = (&name) => {
  27. if (endsWithStr(&name, '.png')) { return 'image/png' }
  28. if (endsWithStr(&name, '.webp')) { return 'image/webp' }
  29. if (endsWithStr(&name, '.svg')) { return 'image/svg+xml' }
  30. return 'image/jpeg'
  31. }
  32. // the answer for `<dir>/<name>`: the file (a day in the browser's cache), else the placeholder (an hour); a name that is no
  33. // plain file name → 400 "bad <what> name"
  34. static imageResponse = (&dir, &name, &what) => {
  35. if (!isSafePosterName(&name)) { return new Response('bad ' + what + ' name', { status = 400 headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }
  36. path = dir + '/' + name
  37. if (exists(path)) { return new Response(readBytes(path), { headers = imageHeaders(mimeOfPoster(&name), 'public, max-age=86400') }) }
  38. return new Response(readBytes(what == 'photo' ? placeholderPhoto : placeholderPoster), { headers = imageHeaders('image/svg+xml', 'public, max-age=3600') })
  39. }

Branches

Latest commits

  • ac58c1e5mission: HM56mre
  • 58979c0clog: HM54 held for decision, DP65 startedmre
  • bb2ebc67log: #64 live, #53 mergedmre
  • 27cde42fMerge hm53-dom: tracker#53 inputs bound to membersmre
  • 2156cc7flog: HM54 reviewmre
  • 9fb82efclog: HM54 gap, fill continuesmre
  • b632eb8dlog: HM53 done, HM54 startedmre
  • ccb1a263tracker#53: bind editor drafts and use key event focus targetmre
  • 3356fcfclog: #52 blocked, HM53 startedmre
  • 833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre
  • 73010cc0HM52: report replace-navigation search draft regressionmre
  • 1742e92bstatus: DP64 and HM52 runningmre
  • 29ad5b58log: #64 merged, DP64 and HM52 startedmre
  • fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
  • b2e2206dlog: #63 livemre
  • 5831e0e0log: HM64 donemre
  • 4042b923Flatten catalog membership rows into paired slotsmre
  • ea8a759elog: #61 #62 live, DP63 and HM64 startedmre
  • 40439456Merge hm63-searchtext: tracker#63 packed search textmre
  • b7e6b48ePack normalized search text behind numeric offsetsmre