gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit93a4421293a44212jukebox: report, numbers of the full scan, notes on the index key rulemre93a44212/lib/api.hl

4.2 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&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)
  7. // POST /api/rescan ask for a scan now (answers at once, 202)
  8. // GET /api/settings, POST /api/settings the library paths and the rescan interval
  9. // Every list is a page { total, offset, limit, items }; the default is 50 rows and no page is bigger than 200.
  10. import { Response } from 'hl:http1'
  11. import { reply, fail, queryNumber, queryText } from './api-helpers.hl'
  12. import { jsonErrorAt } from './jsoncheck.hl'
  13. import { loadSettings, saveSettings } from './settings.hl'
  14. import { scanState } from './library.hl'
  15. import { requestScan } from './schedule.hl'
  16. import { lastScan } from './db.hl'
  17. import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'
  18. import { coverFile } from './covers.hl'
  19. static scanSummary = () => {
  20. st = scanState
  21. return { running = st.running phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed
  22. unreadable = st.unreadable lastSeconds = st.lastSeconds lastError = st.lastError queued = st.wanted }
  23. }
  24. static apiStats = (route, req) => {
  25. if (req.method != 'GET') { return fail(405, 'GET only') }
  26. return reply(200, { library = libraryStats() scan = scanSummary() lastScan = lastScan() settings = loadSettings() })
  27. }
  28. static apiSongs = (route, req) => {
  29. if (req.method != 'GET') { return fail(405, 'GET only') }
  30. sort = queryText(req, 'sort')
  31. if (sort != null && sort != 'title' && sort != 'artist' && sort != 'album') { return fail(400, 'sort is title, artist or album') }
  32. return reply(200, songsPage(sort == null ? 'artist' : sort, queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryNumber(req, 'minPlays'), queryText(req, 'artist'), queryText(req, 'album')))
  33. }
  34. static apiArtists = (route, req) => {
  35. if (req.method != 'GET') { return fail(405, 'GET only') }
  36. return reply(200, artistsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  37. }
  38. static apiAlbums = (route, req) => {
  39. if (req.method != 'GET') { return fail(405, 'GET only') }
  40. return reply(200, albumsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryText(req, 'artist')))
  41. }
  42. static apiSearch = (route, req) => {
  43. if (req.method != 'GET') { return fail(405, 'GET only') }
  44. q = queryText(req, 'q')
  45. if (q == null) { return fail(400, 'q is needed') }
  46. if (q.length > 200) { return fail(400, 'q is too long') }
  47. return reply(200, searchAll(q, queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  48. }
  49. static apiShuffle = (route, req) => {
  50. if (req.method != 'GET') { return fail(405, 'GET only') }
  51. return reply(200, shuffle(queryNumber(req, 'minPlays'), queryNumber(req, 'count')))
  52. }
  53. static apiCover = (route, req) => {
  54. if (req.method != 'GET') { return fail(405, 'GET only') }
  55. id = route.params.id
  56. if (id == null || id.length > 64) { return fail(404, 'no such cover') }
  57. c = coverFile(id)
  58. if (c == null) { return fail(404, 'no such cover') }
  59. return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })
  60. }
  61. static apiRescan = (route, req) => {
  62. if (req.method != 'POST') { return fail(405, 'POST only') }
  63. started = requestScan()
  64. return reply(202, { started = started running = scanState.running })
  65. }
  66. static apiSettings = (route, req) => {
  67. if (req.method == 'GET') { return reply(200, loadSettings()) }
  68. if (req.method != 'POST') { return fail(405, 'GET or POST only') }
  69. if (req.body == null || req.body == '') { return fail(400, 'a JSON body is needed') }
  70. if (jsonErrorAt(req.body) >= 0) { return fail(400, 'the body is not valid JSON') }
  71. r = saveSettings(JSON.parse(req.body))
  72. if (r.error != null) { return fail(400, r.error) }
  73. return reply(200, r)
  74. }
  75. static apiNotFound = (route, req) => { return fail(404, 'no such endpoint') }

Branches

Latest commits

  • 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