sonda/internal/api/http/handlers_library.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

110 lines
3.1 KiB
Go

package httpapi
import (
"errors"
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.kmalloc.org/squid/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"`
// Unsupported counts audio files in formats Sonda cannot read yet.
Unsupported int `json:"unsupported"`
}
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"`
}
// fullParam reads ?full=. Only "true" and "false" are accepted, and it answers
// 400 itself otherwise: a typo must not quietly start a scan of another kind.
func fullParam(c *gin.Context) (full, ok bool) {
values := c.QueryArray("full")
switch {
case len(values) == 0, len(values) == 1 && values[0] == "false":
return false, true
case len(values) == 1 && values[0] == "true":
return true, true
}
writeError(c, http.StatusBadRequest, "invalid_input", "full must be true or false")
return false, false
}
// startScan launches a background scan of the configured roots.
func (h *libraryHandlers) startScan(c *gin.Context) {
full, ok := fullParam(c)
if !ok {
return
}
writeScanStarted(c, h.scan.Start(full))
}
// startLibraryScan launches a background scan of one library.
func (h *libraryHandlers) startLibraryScan(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
full, ok := fullParam(c)
if !ok {
return
}
writeScanStarted(c, h.scan.StartLibrary(id, full))
}
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
}