sonda/internal/api/http/handlers_libraries.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

108 lines
2.6 KiB
Go

package httpapi
import (
"errors"
"net/http"
"time"
"github.com/gin-gonic/gin"
"codeberg.org/squidd/sonda/internal/library"
)
// librariesHandlers are the admin-only routes that manage libraries.
type librariesHandlers struct{ svc libraryService }
type libraryJSON struct {
ID int64 `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
CreatedAt string `json:"created_at"`
}
func toLibraryJSON(l library.Library) libraryJSON {
return libraryJSON{ID: l.ID, Name: l.Name, Path: l.Path, CreatedAt: l.CreatedAt.UTC().Format(time.RFC3339)}
}
func writeLibraryError(c *gin.Context, err error) {
switch {
case errors.Is(err, library.ErrInvalidLibrary):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
case errors.Is(err, library.ErrLibraryExists):
writeError(c, http.StatusConflict, "library_exists", err.Error())
case errors.Is(err, library.ErrLibraryNotFound):
writeError(c, http.StatusNotFound, "library_not_found", err.Error())
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}
func (h *librariesHandlers) list(c *gin.Context) {
libs, err := h.svc.List(c.Request.Context())
if err != nil {
writeLibraryError(c, err)
return
}
out := make([]libraryJSON, len(libs))
for i, l := range libs {
out[i] = toLibraryJSON(l)
}
c.JSON(http.StatusOK, gin.H{"libraries": out})
}
type createLibraryRequest struct {
Name string `json:"name"`
Path string `json:"path"`
}
func (h *librariesHandlers) create(c *gin.Context) {
var req createLibraryRequest
if !bindJSON(c, &req) {
return
}
lib, err := h.svc.Create(c.Request.Context(), req.Name, req.Path)
if err != nil {
writeLibraryError(c, err)
return
}
c.JSON(http.StatusCreated, toLibraryJSON(lib))
}
type updateLibraryRequest struct {
Name *string `json:"name"` // pointers: an absent field keeps the current value
Path *string `json:"path"`
}
func (h *librariesHandlers) update(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
var req updateLibraryRequest
if !bindJSON(c, &req) {
return
}
if req.Name == nil && req.Path == nil {
writeError(c, http.StatusBadRequest, "invalid_input", "send name, path or both")
return
}
lib, err := h.svc.Update(c.Request.Context(), id, req.Name, req.Path)
if err != nil {
writeLibraryError(c, err)
return
}
c.JSON(http.StatusOK, toLibraryJSON(lib))
}
func (h *librariesHandlers) remove(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
writeLibraryError(c, err)
return
}
c.Status(http.StatusNoContent)
}