2020-01-15 00:23:29 +01:00
|
|
|
package persistence
|
2020-01-13 00:36:19 +01:00
|
|
|
|
|
|
|
import (
|
2020-01-28 14:22:17 +01:00
|
|
|
"context"
|
2020-06-11 05:11:43 +02:00
|
|
|
"fmt"
|
2020-01-31 23:56:02 +01:00
|
|
|
"os"
|
2020-06-12 19:26:46 +02:00
|
|
|
"path/filepath"
|
2020-10-05 17:52:09 +02:00
|
|
|
"strings"
|
2020-07-22 15:36:22 +02:00
|
|
|
"unicode/utf8"
|
2020-01-13 00:36:19 +01:00
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
. "github.com/Masterminds/squirrel"
|
2020-01-13 00:36:19 +01:00
|
|
|
"github.com/astaxie/beego/orm"
|
2020-01-31 23:56:02 +01:00
|
|
|
"github.com/deluan/navidrome/log"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/model"
|
2020-01-28 14:22:17 +01:00
|
|
|
"github.com/deluan/rest"
|
2020-01-13 00:36:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mediaFileRepository struct {
|
2020-01-28 14:22:17 +01:00
|
|
|
sqlRepository
|
2020-03-22 01:00:46 +01:00
|
|
|
sqlRestful
|
2020-01-13 00:36:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func NewMediaFileRepository(ctx context.Context, o orm.Ormer) *mediaFileRepository {
|
2020-01-13 00:36:19 +01:00
|
|
|
r := &mediaFileRepository{}
|
2020-01-28 14:22:17 +01:00
|
|
|
r.ctx = ctx
|
2020-01-19 21:37:41 +01:00
|
|
|
r.ormer = o
|
2020-01-13 06:04:11 +01:00
|
|
|
r.tableName = "media_file"
|
2020-02-05 20:12:13 +01:00
|
|
|
r.sortMappings = map[string]string{
|
2020-04-24 23:37:28 +02:00
|
|
|
"artist": "order_artist_name asc, album asc, disc_number asc, track_number asc",
|
2020-12-18 16:48:36 +01:00
|
|
|
"album": "order_album_name asc, disc_number asc, track_number asc, artist asc, title asc",
|
2020-05-05 22:17:09 +02:00
|
|
|
"random": "RANDOM()",
|
2020-02-05 20:12:13 +01:00
|
|
|
}
|
2020-03-20 03:26:18 +01:00
|
|
|
r.filterMappings = map[string]filterFunc{
|
2020-05-23 05:10:58 +02:00
|
|
|
"title": fullTextFilter,
|
|
|
|
"starred": booleanFilter,
|
2020-03-20 03:26:18 +01:00
|
|
|
}
|
2020-01-13 00:36:19 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
2020-05-23 06:43:45 +02:00
|
|
|
return r.count(r.newSelectWithAnnotation("media_file.id"), options...)
|
2020-01-13 00:36:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) Exists(id string) (bool, error) {
|
|
|
|
return r.exists(Select().Where(Eq{"id": id}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) Put(m *model.MediaFile) error {
|
2020-04-24 16:13:59 +02:00
|
|
|
m.FullText = getFullText(m.Title, m.Album, m.Artist, m.AlbumArtist,
|
2020-05-12 17:17:22 +02:00
|
|
|
m.SortTitle, m.SortAlbumName, m.SortArtistName, m.SortAlbumArtistName, m.DiscSubtitle)
|
2020-01-31 21:35:06 +01:00
|
|
|
_, err := r.put(m.ID, m)
|
2020-03-20 02:43:30 +01:00
|
|
|
return err
|
2020-01-13 00:36:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) selectMediaFile(options ...model.QueryOptions) SelectBuilder {
|
2020-08-01 18:17:06 +02:00
|
|
|
sql := r.newSelectWithAnnotation("media_file.id", options...).Columns("media_file.*")
|
|
|
|
return r.withBookmark(sql, "media_file.id")
|
2020-01-13 00:36:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) Get(id string) (*model.MediaFile, error) {
|
|
|
|
sel := r.selectMediaFile().Where(Eq{"id": id})
|
2020-05-22 21:23:42 +02:00
|
|
|
var res model.MediaFiles
|
|
|
|
if err := r.queryAll(sel, &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return nil, model.ErrNotFound
|
|
|
|
}
|
|
|
|
return &res[0], nil
|
2020-01-13 00:36:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) GetAll(options ...model.QueryOptions) (model.MediaFiles, error) {
|
|
|
|
sq := r.selectMediaFile(options...)
|
2020-01-31 22:47:13 +01:00
|
|
|
res := model.MediaFiles{}
|
2020-01-28 14:22:17 +01:00
|
|
|
err := r.queryAll(sq, &res)
|
|
|
|
return res, err
|
2020-01-16 21:56:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) FindByAlbum(albumId string) (model.MediaFiles, error) {
|
2020-12-18 16:48:36 +01:00
|
|
|
sel := r.selectMediaFile().Where(Eq{"album_id": albumId}).OrderBy("disc_number", "track_number", "artist", "title")
|
2020-01-31 22:47:13 +01:00
|
|
|
res := model.MediaFiles{}
|
2020-01-28 14:22:17 +01:00
|
|
|
err := r.queryAll(sel, &res)
|
|
|
|
return res, err
|
2020-01-16 22:53:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:38:17 +02:00
|
|
|
func (r mediaFileRepository) FindByPath(path string) (*model.MediaFile, error) {
|
|
|
|
sel := r.selectMediaFile().Where(Eq{"path": path})
|
|
|
|
var res model.MediaFiles
|
|
|
|
if err := r.queryAll(sel, &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return nil, model.ErrNotFound
|
|
|
|
}
|
|
|
|
return &res[0], nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:52:09 +02:00
|
|
|
func cleanPath(path string) string {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
if !strings.HasSuffix(path, string(os.PathSeparator)) {
|
|
|
|
path = path + string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathStartsWith(path string) Eq {
|
|
|
|
substr := fmt.Sprintf("substr(path, 1, %d)", utf8.RuneCountInString(path))
|
|
|
|
return Eq{substr: path}
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:38:17 +02:00
|
|
|
// FindAllByPath only return mediafiles that are direct children of requested path
|
|
|
|
func (r mediaFileRepository) FindAllByPath(path string) (model.MediaFiles, error) {
|
2020-06-11 05:11:43 +02:00
|
|
|
// Query by path based on https://stackoverflow.com/a/13911906/653632
|
2020-10-05 17:52:09 +02:00
|
|
|
path = cleanPath(path)
|
2020-07-22 15:36:22 +02:00
|
|
|
pathLen := utf8.RuneCountInString(path)
|
|
|
|
sel0 := r.selectMediaFile().Columns(fmt.Sprintf("substr(path, %d) AS item", pathLen+2)).
|
2020-07-14 00:37:48 +02:00
|
|
|
Where(pathStartsWith(path))
|
2020-06-11 05:11:43 +02:00
|
|
|
sel := r.newSelect().Columns("*", "item NOT GLOB '*"+string(os.PathSeparator)+"*' AS isLast").
|
|
|
|
Where(Eq{"isLast": 1}).FromSelect(sel0, "sel0")
|
|
|
|
|
2020-01-31 22:47:13 +01:00
|
|
|
res := model.MediaFiles{}
|
2020-01-28 14:22:17 +01:00
|
|
|
err := r.queryAll(sel, &res)
|
2020-06-11 05:11:43 +02:00
|
|
|
return res, err
|
2020-01-28 14:22:17 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:26:46 +02:00
|
|
|
// FindPathsRecursively returns a list of all subfolders of basePath, recursively
|
|
|
|
func (r mediaFileRepository) FindPathsRecursively(basePath string) ([]string, error) {
|
2020-10-05 17:52:09 +02:00
|
|
|
path := cleanPath(basePath)
|
2020-06-12 19:26:46 +02:00
|
|
|
// Query based on https://stackoverflow.com/a/38330814/653632
|
2020-06-12 20:34:50 +02:00
|
|
|
sel := r.newSelect().Columns(fmt.Sprintf("distinct rtrim(path, replace(path, '%s', ''))", string(os.PathSeparator))).
|
2020-10-05 17:52:09 +02:00
|
|
|
Where(pathStartsWith(path))
|
2020-06-12 19:26:46 +02:00
|
|
|
var res []string
|
|
|
|
err := r.queryAll(sel, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 22:18:45 +02:00
|
|
|
func (r mediaFileRepository) deleteNotInPath(basePath string) error {
|
2020-10-05 17:52:09 +02:00
|
|
|
path := cleanPath(basePath)
|
|
|
|
sel := Delete(r.tableName).Where(NotEq(pathStartsWith(path)))
|
2020-10-02 22:18:45 +02:00
|
|
|
c, err := r.executeSQL(sel)
|
|
|
|
if err == nil {
|
|
|
|
if c > 0 {
|
|
|
|
log.Debug(r.ctx, "Deleted dangling tracks", "totalDeleted", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-31 04:07:02 +01:00
|
|
|
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
2020-01-28 14:22:17 +01:00
|
|
|
sq := r.selectMediaFile(options...).Where("starred = true")
|
2020-01-31 22:47:13 +01:00
|
|
|
starred := model.MediaFiles{}
|
2020-01-28 14:22:17 +01:00
|
|
|
err := r.queryAll(sq, &starred)
|
|
|
|
return starred, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Keep order when paginating
|
|
|
|
func (r mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.MediaFiles, error) {
|
|
|
|
sq := r.selectMediaFile(options...)
|
2020-02-28 22:09:27 +01:00
|
|
|
sq = sq.OrderBy("RANDOM()")
|
2020-01-31 22:47:13 +01:00
|
|
|
results := model.MediaFiles{}
|
2020-02-01 20:48:22 +01:00
|
|
|
err := r.queryAll(sq, &results)
|
2020-01-28 14:22:17 +01:00
|
|
|
return results, err
|
2020-01-21 14:49:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) Delete(id string) error {
|
|
|
|
return r.delete(Eq{"id": id})
|
|
|
|
}
|
|
|
|
|
2020-06-12 19:26:46 +02:00
|
|
|
// DeleteByPath delete from the DB all mediafiles that are direct children of path
|
2020-10-05 17:52:09 +02:00
|
|
|
func (r mediaFileRepository) DeleteByPath(basePath string) (int64, error) {
|
|
|
|
path := cleanPath(basePath)
|
2020-07-22 15:36:22 +02:00
|
|
|
pathLen := utf8.RuneCountInString(path)
|
2020-06-11 05:11:43 +02:00
|
|
|
del := Delete(r.tableName).
|
2020-07-14 00:37:48 +02:00
|
|
|
Where(And{pathStartsWith(path),
|
2020-07-22 15:36:22 +02:00
|
|
|
Eq{fmt.Sprintf("substr(path, %d) glob '*%s*'", pathLen+2, string(os.PathSeparator)): 0}})
|
2020-06-11 05:11:43 +02:00
|
|
|
log.Debug(r.ctx, "Deleting mediafiles by path", "path", path)
|
2020-07-12 17:48:57 +02:00
|
|
|
return r.executeSQL(del)
|
2020-01-19 02:59:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
|
2020-01-31 22:47:13 +01:00
|
|
|
results := model.MediaFiles{}
|
2020-01-31 21:35:06 +01:00
|
|
|
err := r.doSearch(q, offset, size, &results, "title")
|
2020-01-28 14:22:17 +01:00
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
|
|
|
return r.CountAll(r.parseRestOptions(options...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) Read(id string) (interface{}, error) {
|
|
|
|
return r.Get(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
|
|
|
return r.GetAll(r.parseRestOptions(options...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) EntityName() string {
|
|
|
|
return "mediafile"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) NewInstance() interface{} {
|
2020-05-22 21:23:42 +02:00
|
|
|
return &model.MediaFile{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) Save(entity interface{}) (string, error) {
|
|
|
|
mf := entity.(*model.MediaFile)
|
|
|
|
err := r.Put(mf)
|
|
|
|
return mf.ID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r mediaFileRepository) Update(entity interface{}, cols ...string) error {
|
|
|
|
mf := entity.(*model.MediaFile)
|
|
|
|
return r.Put(mf)
|
2020-01-13 16:44:35 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
var _ model.MediaFileRepository = (*mediaFileRepository)(nil)
|
2020-01-28 14:22:17 +01:00
|
|
|
var _ model.ResourceRepository = (*mediaFileRepository)(nil)
|
2020-05-22 21:23:42 +02:00
|
|
|
var _ rest.Persistable = (*mediaFileRepository)(nil)
|