gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5135879451358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre51358794/lib/stream.hl

4.2 KB

  1. // lib/stream.hl — THE AUDIO ROUTE: GET /stream/<song id>. The id is looked up in the songs table; the file path comes from the database and never
  2. // from the URL, and the read is only ever of a file that table lists. HTTP RANGE like a media element needs it: `Range: bytes=a-b`, `a-`, `-n` answer 206
  3. // with Content-Range; a start behind the end of the file answers 416. A single answer is never bigger than `chunk` (1 MiB): `bytes=0-` (what Chrome sends
  4. // first) gets the first megabyte and the browser asks for the next one as it plays, so no file is read into memory whole. The bytes come from hl:fs `readBytes(path, offset, length)`
  5. // (hybriel#233), which reads only that range; the size of the file comes from `stat(path)`.
  6. // A request without a Range header is answered like `bytes=0-` (206): a media element always sends one.
  7. import { Response } from 'hl:http1'
  8. import { readBytes, stat } from 'hl:fs'
  9. import { songById } from './db.hl'
  10. static chunk = 1048576
  11. static kinds = { 'mp3' = 'audio/mpeg' 'ogg' = 'audio/ogg' 'oga' = 'audio/ogg' 'opus' = 'audio/ogg' 'm4a' = 'audio/mp4' 'aac' = 'audio/aac' 'flac' = 'audio/flac'
  12. 'wav' = 'audio/wav' 'wma' = 'audio/x-ms-wma' 'ape' = 'audio/x-ape' 'mp4' = 'video/mp4' 'm4v' = 'video/mp4' 'webm' = 'video/webm' 'mkv' = 'video/x-matroska' }
  13. static contentType = (format) => {
  14. t = kinds[format]
  15. return t == null ? 'application/octet-stream' : t
  16. }
  17. static fail = (status, text) => {
  18. return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' } })
  19. }
  20. // a song id is mpackdb's twelve characters of 0-9 a-z (anything else is not a song)
  21. static validId = (id) => {
  22. if (id == null || id.length < 4 || id.length > 24) { return false }
  23. let i = 0
  24. while (i < id.length) {
  25. c = id.charCodeAt(i)
  26. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 45 || c == 95)) { return false }
  27. i = i + 1
  28. }
  29. return true
  30. }
  31. // the range asked for as { start, end } within a file of `size` bytes; null = no usable Range header (a list of ranges, garbage); { bad = true } = past the end
  32. static rangeOf = (asked, size) => {
  33. if (asked == null || !asked.startsWith('bytes=') || asked.includes(',')) { return null }
  34. parts = asked.slice(6).split('-')
  35. if (parts.length != 2) { return null }
  36. let start = null
  37. let end = null
  38. if (parts[0] == '' && parts[1] != '') {
  39. n = toNumber(parts[1])
  40. if (n == null || n <= 0) { return null }
  41. start = n >= size ? 0 : size - n
  42. end = size - 1
  43. } else if (parts[0] != '') {
  44. start = toNumber(parts[0])
  45. end = parts[1] == '' ? size - 1 : toNumber(parts[1])
  46. if (start == null || end == null) { return null }
  47. if (end >= size) { end = size - 1 }
  48. } else { return null }
  49. if (start > end || start >= size) { return { bad = true } }
  50. return { start = start end = end }
  51. }
  52. // GET /stream/<id>
  53. static serve = (route, req) => {
  54. if (req.method != 'GET' && req.method != 'HEAD') { return fail(405, 'GET only') }
  55. id = route.params.id
  56. if (!validId(id)) { return fail(404, 'no such song') }
  57. song = songById(id)
  58. if (song == null || song.size == null || song.size <= 0) { return fail(404, 'no such song') }
  59. size = song.size
  60. h = { 'Content-Type' = contentType(song.format) 'Accept-Ranges' = 'bytes' 'Cache-Control' = 'no-cache' 'X-Content-Type-Options' = 'nosniff' }
  61. asked = req.headers['range'] != null ? req.headers['range'] : req.headers['Range']
  62. r = rangeOf(asked, size)
  63. if (r != null && r.bad == true) { return new Response('', { status = 416 headers = { 'Content-Range' = 'bytes */' + size } }) }
  64. let start = r == null ? 0 : r.start
  65. let end = r == null ? size - 1 : r.end
  66. if (end - start + 1 > chunk) { end = start + chunk - 1 }
  67. if (req.method == 'HEAD') {
  68. h['Content-Range'] = 'bytes ' + start + '-' + end + '/' + size
  69. return new Response('', { status = 206 headers = h })
  70. }
  71. if (stat(song.path) == null) { return fail(404, 'the file cannot be read') }
  72. data = readBytes(song.path, start, end - start + 1)
  73. if (data == null) { return fail(404, 'the file cannot be read') }
  74. // the file may have changed since the scan: say what was really read
  75. len = data.length
  76. if (len == 0) { return fail(404, 'the file cannot be read') }
  77. h['Content-Range'] = 'bytes ' + start + '-' + (start + len - 1) + '/' + size
  78. return new Response(data, { status = 206 headers = h })
  79. }

Branches

Latest commits

  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre