- fix: on PostgreSQL a purge could delete a discovered artist while a scan was adopting it. The scan then failed on the missing artist for the whole file, and the two could also deadlock. Purge now takes the same artist lock as the scanner and discovery before it removes anything. Saving a list or tags does too, and an artist that is gone by then is reported as not found instead of a foreign key error, so a user who opened an artist that was removed meanwhile gets a 404 - fix: a discovered artist that was only the source of a list stayed forever and kept the artists of its own list alive, so chains built up. Cleanup now removes such an artist and then follows the chain until nothing more goes - fix: in a full rescan a file that came back but could not be read stayed marked missing. It is restored first, as in a normal scan, and read as well - fix: ?full given twice is refused, so a typo cannot start a scan of another kind - fix: genre cleaning lives in one place, used by the reader and the store - docs: the files genres count the tracks the artist is credited on, and the spec says every files tag is a genre - test: a purge colliding with a scan on both databases (it fails without the lock), a chain of discovered artists, saving for an artist that is gone, a file that returns unreadable during a full scan
110 lines
3.1 KiB
Go
110 lines
3.1 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"`
|
|
// 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
|
|
}
|