fix: Find/DeleteByPath
This commit is contained in:
@@ -2,9 +2,12 @@ package persistence
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
. "github.com/Masterminds/squirrel"
|
. "github.com/Masterminds/squirrel"
|
||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
|
"github.com/deluan/navidrome/log"
|
||||||
"github.com/deluan/navidrome/model"
|
"github.com/deluan/navidrome/model"
|
||||||
"github.com/deluan/rest"
|
"github.com/deluan/rest"
|
||||||
)
|
)
|
||||||
@@ -66,7 +69,21 @@ func (r mediaFileRepository) FindByPath(path string) (model.MediaFiles, error) {
|
|||||||
sel := r.selectMediaFile().Where(Like{"path": path + "%"})
|
sel := r.selectMediaFile().Where(Like{"path": path + "%"})
|
||||||
res := model.MediaFiles{}
|
res := model.MediaFiles{}
|
||||||
err := r.queryAll(sel, &res)
|
err := r.queryAll(sel, &res)
|
||||||
return res, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only return mediafiles that are direct child of requested path
|
||||||
|
filtered := model.MediaFiles{}
|
||||||
|
path = strings.ToLower(path) + string(os.PathSeparator)
|
||||||
|
for _, mf := range res {
|
||||||
|
filename := strings.TrimPrefix(strings.ToLower(mf.Path), path)
|
||||||
|
if len(strings.Split(filename, string(os.PathSeparator))) > 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filtered = append(filtered, mf)
|
||||||
|
}
|
||||||
|
return filtered, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||||
@@ -99,8 +116,20 @@ func (r mediaFileRepository) Delete(id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r mediaFileRepository) DeleteByPath(path string) error {
|
func (r mediaFileRepository) DeleteByPath(path string) error {
|
||||||
del := Delete(r.tableName).Where(Like{"path": path + "%"})
|
filtered, err := r.FindByPath(path)
|
||||||
_, err := r.executeSQL(del)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(filtered) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ids := make([]string, len(filtered))
|
||||||
|
for i, mf := range filtered {
|
||||||
|
ids[i] = mf.ID
|
||||||
|
}
|
||||||
|
log.Debug(r.ctx, "Deleting mediafiles by path", "path", path, "totalDeleted", len(ids))
|
||||||
|
del := Delete(r.tableName).Where(Eq{"id": ids})
|
||||||
|
_, err = r.executeSQL(del)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,12 +76,14 @@ var _ = Describe("MediaRepository", func() {
|
|||||||
Expect(mr.Put(&model.MediaFile{ID: id1, Path: P("/abc/123/" + id1 + ".mp3")})).To(BeNil())
|
Expect(mr.Put(&model.MediaFile{ID: id1, Path: P("/abc/123/" + id1 + ".mp3")})).To(BeNil())
|
||||||
id2 := "2222"
|
id2 := "2222"
|
||||||
Expect(mr.Put(&model.MediaFile{ID: id2, Path: P("/abc/123/" + id2 + ".mp3")})).To(BeNil())
|
Expect(mr.Put(&model.MediaFile{ID: id2, Path: P("/abc/123/" + id2 + ".mp3")})).To(BeNil())
|
||||||
|
id3 := "3333"
|
||||||
|
Expect(mr.Put(&model.MediaFile{ID: id3, Path: P("/abc/" + id3 + ".mp3")})).To(BeNil())
|
||||||
|
|
||||||
Expect(mr.DeleteByPath(P("/abc"))).To(BeNil())
|
Expect(mr.DeleteByPath(P("/abc"))).To(BeNil())
|
||||||
|
|
||||||
_, err := mr.Get(id1)
|
Expect(mr.Get(id1)).ToNot(BeNil())
|
||||||
Expect(err).To(MatchError(model.ErrNotFound))
|
Expect(mr.Get(id2)).ToNot(BeNil())
|
||||||
_, err = mr.Get(id2)
|
_, err := mr.Get(id3)
|
||||||
Expect(err).To(MatchError(model.ErrNotFound))
|
Expect(err).To(MatchError(model.ErrNotFound))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user