2025-02-19 17:35:17 -08:00
|
|
|
package nativeapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"maps"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/deluan/rest"
|
2025-11-08 20:11:00 -05:00
|
|
|
"github.com/navidrome/navidrome/core"
|
2025-02-19 17:35:17 -08:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
|
|
|
"github.com/navidrome/navidrome/utils/req"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type missingRepository struct {
|
2026-09-25 18:06:10 -04:00
|
|
|
rest.Repository[model.MediaFile]
|
2025-02-19 17:35:17 -08:00
|
|
|
mfRepo model.MediaFileRepository
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 18:06:10 -04:00
|
|
|
func newMissingRepository(ds model.DataStore) rest.Repository[model.MediaFile] {
|
|
|
|
|
mf := ds.MediaFile()
|
|
|
|
|
return &missingRepository{Repository: mf, mfRepo: mf}
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
|
|
|
|
|
2026-09-25 18:06:10 -04:00
|
|
|
func (r *missingRepository) Count(ctx context.Context, options ...rest.QueryOptions) (int64, error) {
|
|
|
|
|
return r.Repository.Count(ctx, r.parseOptions(options))
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
|
|
|
|
|
2026-09-25 18:06:10 -04:00
|
|
|
func (r *missingRepository) ReadAll(ctx context.Context, options ...rest.QueryOptions) ([]model.MediaFile, error) {
|
|
|
|
|
return r.Repository.ReadAll(ctx, r.parseOptions(options))
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *missingRepository) parseOptions(options []rest.QueryOptions) rest.QueryOptions {
|
|
|
|
|
var opt rest.QueryOptions
|
|
|
|
|
if len(options) > 0 {
|
|
|
|
|
opt = options[0]
|
|
|
|
|
opt.Filters = maps.Clone(opt.Filters)
|
|
|
|
|
}
|
|
|
|
|
opt.Filters["missing"] = "true"
|
|
|
|
|
return opt
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 18:06:10 -04:00
|
|
|
func (r *missingRepository) Read(ctx context.Context, id string) (*model.MediaFile, error) {
|
|
|
|
|
mf, err := r.mfRepo.Get(ctx, id)
|
2025-02-19 17:35:17 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-09-14 22:46:21 -04:00
|
|
|
if !mf.Missing {
|
2025-02-19 17:35:17 -08:00
|
|
|
return nil, model.ErrNotFound
|
|
|
|
|
}
|
2026-09-14 22:46:21 -04:00
|
|
|
return mf, nil
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func deleteMissingFiles(maintenance core.Maintenance) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
p := req.Params(r)
|
2026-07-18 19:30:04 -04:00
|
|
|
ids := p.Strings("id")
|
2025-11-08 20:11:00 -05:00
|
|
|
|
|
|
|
|
var err error
|
2025-05-22 22:28:10 -04:00
|
|
|
if len(ids) == 0 {
|
2025-11-08 20:11:00 -05:00
|
|
|
err = maintenance.DeleteAllMissingFiles(ctx)
|
2025-06-26 15:50:56 -04:00
|
|
|
} else {
|
2025-11-08 20:11:00 -05:00
|
|
|
err = maintenance.DeleteMissingFiles(ctx, ids)
|
2025-06-26 15:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
if len(ids) == 1 && errors.Is(err, model.ErrNotFound) {
|
|
|
|
|
log.Warn(ctx, "Missing file not found", "id", ids[0])
|
|
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "failed to delete missing files", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeDeleteManyResponse(w, r, ids)
|
|
|
|
|
}
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|