sonda/internal/api/http/handlers_library.go
tgiachi 46463846b3 feat(library): named media libraries managed through the API
- feat(storage): libraries table (name, path); audio_files points at a library
  by id instead of repeating the root path; migration 0005 for both drivers
  turns every existing root into a library and keeps file ids, tracks and the
  missing flag
- feat(library): Libraries service validates names and paths: absolute, an
  existing directory, never the filesystem root, never inside or around
  another library (checked on real paths, so symlinks cannot hide an overlap)
- feat(api): admin-only GET/POST /libraries, PATCH/DELETE /libraries/{id} and
  POST /libraries/{id}/scan; deleting a library drops its index entries and
  keeps tracks and listening history
- feat(library): the scanner and the scan manager work on libraries; scanning
  with no libraries answers 409 no_libraries
- feat(streaming): the library directory comes from the database instead of
  a configured list of roots
- breaking(config): library.roots is removed; an old config fails with a hint
  to use POST /api/v1/libraries
- test: CRUD and duplicates on both databases, path change keeps files,
  upgrade of a database written before libraries existed
2026-11-11 13:30:25 +01:00

86 lines
2.3 KiB
Go

package httpapi
import (
"errors"
"net/http"
"time"
"github.com/gin-gonic/gin"
"codeberg.org/squidd/sonda/internal/library"
)
type libraryHandlers struct{ scan scanService }
type summaryJSON struct {
Libraries int `json:"libraries"`
Found int `json:"found"`
Added int `json:"added"`
Updated int `json:"updated"`
Unchanged int `json:"unchanged"`
Renamed int `json:"renamed"`
Restored int `json:"restored"`
Missing int `json:"missing"`
Failed int `json:"failed"`
Skipped int `json:"skipped"`
}
type scanStatusJSON struct {
Running bool `json:"running"`
StartedAt *string `json:"started_at,omitempty"`
LastFinishedAt *string `json:"last_finished_at,omitempty"`
Last *summaryJSON `json:"last,omitempty"`
LastError string `json:"last_error,omitempty"`
}
// startScan launches a background scan of the configured roots.
func (h *libraryHandlers) startScan(c *gin.Context) {
writeScanStarted(c, h.scan.Start())
}
// startLibraryScan launches a background scan of one library.
func (h *libraryHandlers) startLibraryScan(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
writeScanStarted(c, h.scan.StartLibrary(id))
}
func writeScanStarted(c *gin.Context, err error) {
switch {
case errors.Is(err, library.ErrScanRunning):
writeError(c, http.StatusConflict, "scan_running", err.Error())
case errors.Is(err, library.ErrNoLibraries):
writeError(c, http.StatusConflict, "no_libraries", "add a library before scanning")
case errors.Is(err, library.ErrLibraryNotFound):
writeError(c, http.StatusNotFound, "library_not_found", err.Error())
case err != nil:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
default:
c.JSON(http.StatusAccepted, gin.H{"status": "started"})
}
}
// scanStatus reports whether a scan is running and how the last one ended.
func (h *libraryHandlers) scanStatus(c *gin.Context) {
st := h.scan.Status()
out := scanStatusJSON{Running: st.Running, LastError: st.LastError}
if !st.StartedAt.IsZero() {
out.StartedAt = rfc3339(st.StartedAt)
}
if !st.LastFinishedAt.IsZero() {
out.LastFinishedAt = rfc3339(st.LastFinishedAt)
}
if st.Last != nil {
s := summaryJSON(*st.Last)
out.Last = &s
}
c.JSON(http.StatusOK, out)
}
func rfc3339(t time.Time) *string {
s := t.UTC().Format(time.RFC3339)
return &s
}