sonda/internal/api/http/handlers_libraries.go
tgiachi eb23ee426a fix(library): make the libraries upgrade and validation robust
- fix(migration): roots that differ only in case (/Music and /music are two
  directories on Linux) used to make migration 0005 fail on the unique name;
  the later ones now get a numeric suffix
- feat(library): at startup, log a warning for every library whose directory
  cannot be read or that overlaps another, because migrated rows skip
  validation and may still hold roots that were removed from the old config
- fix(library): creating or updating a library is now serialised, so two
  concurrent requests cannot both pass the overlap check
- fix(library): changing a library's directory or deleting it is refused with
  409 scan_running while a scan runs; the scan works from the list it started
  with and would write under a stale path
- fix(library): library names clash ignoring case and accents the same way on
  SQLite and PostgreSQL, checked in Go
- fix(library): changing only the path no longer revalidates an untouched
  name, which can be longer than the limit after a migration
- test: roots differing in case on both databases; concurrent creates with a
  slow store, verified to fail without the lock; the busy guard; Problems
2026-11-11 13:34:44 +01:00

110 lines
2.7 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())
case errors.Is(err, library.ErrScanRunning):
writeError(c, http.StatusConflict, "scan_running", "wait for the running scan to finish")
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)
}