gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2a4cfc552a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre2a4cfc55/lib/api.hl

4.8 KB

  1. // lib/api.hl — THE JSON API (project.hl wires the routes): thin wrappers — check the request, call the topic, answer.
  2. // GET /api/stats library numbers, the scan state, the last scan, the settings
  3. // GET /api/songs?sort=title|artist|album|plays&offset&limit[&minPlays][&artist=<id>][&album=<id>]
  4. // GET /api/artists?offset&limit GET /api/albums?offset&limit[&artist=<id>]
  5. // GET /api/search?q=&offset&limit GET /api/shuffle?minPlays=&count=
  6. // GET /api/cover/<id> the cover picture (cached for a year: the id is the content hash); /api/thumb/<id> the 96 px square one
  7. // GET /stream/<id> is in lib/stream.hl (the audio, with HTTP Range)
  8. // POST /api/rescan ask for a scan now (answers at once, 202)
  9. // GET /api/settings, POST /api/settings the library paths and the rescan interval
  10. // Every list is a page { total, offset, limit, items }; the default is 50 rows and no page is bigger than 200.
  11. import { Response } from 'hl:http1'
  12. import { reply, fail, queryNumber, queryText } from './api-helpers.hl'
  13. import { jsonErrorAt } from './jsoncheck.hl'
  14. import { loadSettings, saveSettings } from './settings.hl'
  15. import { scanState } from './library.hl'
  16. import { requestScan } from './schedule.hl'
  17. import { lastScan } from './db.hl'
  18. import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'
  19. import { coverFile, thumbFile } from './covers.hl'
  20. static scanSummary = () => {
  21. st = scanState
  22. return { running = st.running phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed
  23. unreadable = st.unreadable lastSeconds = st.lastSeconds lastError = st.lastError queued = st.wanted }
  24. }
  25. static apiStats = (route, req) => {
  26. if (req.method != 'GET') { return fail(405, 'GET only') }
  27. return reply(200, { library = libraryStats() scan = scanSummary() lastScan = lastScan() settings = loadSettings() })
  28. }
  29. static apiSongs = (route, req) => {
  30. if (req.method != 'GET') { return fail(405, 'GET only') }
  31. sort = queryText(req, 'sort')
  32. if (sort != null && sort != 'title' && sort != 'artist' && sort != 'album' && sort != 'plays') { return fail(400, 'sort is title, artist, album or plays') }
  33. return reply(200, songsPage(sort == null ? 'artist' : sort, queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryNumber(req, 'minPlays'), queryText(req, 'artist'), queryText(req, 'album')))
  34. }
  35. static apiArtists = (route, req) => {
  36. if (req.method != 'GET') { return fail(405, 'GET only') }
  37. return reply(200, artistsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  38. }
  39. static apiAlbums = (route, req) => {
  40. if (req.method != 'GET') { return fail(405, 'GET only') }
  41. return reply(200, albumsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryText(req, 'artist')))
  42. }
  43. static apiSearch = (route, req) => {
  44. if (req.method != 'GET') { return fail(405, 'GET only') }
  45. q = queryText(req, 'q')
  46. if (q == null) { return fail(400, 'q is needed') }
  47. if (q.length > 200) { return fail(400, 'q is too long') }
  48. return reply(200, searchAll(q, queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  49. }
  50. static apiShuffle = (route, req) => {
  51. if (req.method != 'GET') { return fail(405, 'GET only') }
  52. return reply(200, shuffle(queryNumber(req, 'minPlays'), queryNumber(req, 'count')))
  53. }
  54. static apiCover = (route, req) => {
  55. if (req.method != 'GET') { return fail(405, 'GET only') }
  56. id = route.params.id
  57. if (id == null || id.length > 64) { return fail(404, 'no such cover') }
  58. c = coverFile(id)
  59. if (c == null) { return fail(404, 'no such cover') }
  60. return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })
  61. }
  62. static apiThumb = (route, req) => {
  63. if (req.method != 'GET') { return fail(405, 'GET only') }
  64. id = route.params.id
  65. if (id == null || id.length > 64) { return fail(404, 'no such cover') }
  66. c = thumbFile(id)
  67. if (c == null) { return fail(404, 'no such cover') }
  68. return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })
  69. }
  70. static apiRescan = (route, req) => {
  71. if (req.method != 'POST') { return fail(405, 'POST only') }
  72. started = requestScan()
  73. return reply(202, { started = started running = scanState.running })
  74. }
  75. static apiSettings = (route, req) => {
  76. if (req.method == 'GET') { return reply(200, loadSettings()) }
  77. if (req.method != 'POST') { return fail(405, 'GET or POST only') }
  78. if (req.body == null || req.body == '') { return fail(400, 'a JSON body is needed') }
  79. if (jsonErrorAt(req.body) >= 0) { return fail(400, 'the body is not valid JSON') }
  80. r = saveSettings(JSON.parse(req.body))
  81. if (r.error != null) { return fail(400, r.error) }
  82. return reply(200, r)
  83. }
  84. static apiNotFound = (route, req) => { return fail(404, 'no such endpoint') }

Branches

Latest commits

  • 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