Persist folders in DB

This commit is contained in:
Deluan 2023-12-26 19:10:18 -05:00
parent d6b82154c9
commit dc1fdd5dcd
3 changed files with 34 additions and 14 deletions

View File

@ -23,9 +23,7 @@ create table if not exists folder(
name varchar default '' not null,
updated_at timestamp default current_timestamp not null,
created_at timestamp default current_timestamp not null,
parent_id varchar default null
references folder (id)
on delete cascade
parent_id varchar default '' not null
);
alter table media_file

View File

@ -9,13 +9,13 @@ import (
)
type Folder struct {
ID string
LibraryID int
Path string
Name string
ParentID string
UpdateAt time.Time
CreatedAt time.Time
ID string `structs:"id"`
LibraryID int `structs:"library_id"`
Path string `structs:"path"`
Name string `structs:"name"`
ParentID string `structs:"parent_id"`
UpdateAt time.Time `structs:"updated_at"`
CreatedAt time.Time `structs:"created_at"`
}
func FolderID(lib Library, path string) string {
@ -26,10 +26,22 @@ func FolderID(lib Library, path string) string {
func NewFolder(lib Library, path string) *Folder {
id := FolderID(lib, path)
dir, name := filepath.Split(path)
dir = filepath.Clean(dir)
var parentID string
if dir == "." {
dir = ""
parentID = ""
} else {
parentID = FolderID(lib, dir)
}
return &Folder{
ID: id,
Path: dir,
Name: name,
LibraryID: lib.ID,
ID: id,
Path: dir,
Name: name,
ParentID: parentID,
UpdateAt: time.Now(),
CreatedAt: time.Now(),
}
}

View File

@ -31,12 +31,22 @@ func persistChanges(ctx context.Context) pipeline.StageFn[*folderEntry] {
track := chunk[i]
err = tx.MediaFile(ctx).Put(&track)
if err != nil {
log.Error(ctx, "Scanner: Error adding mediafile to DB", "folder", entry.path, "track", track, err)
log.Error(ctx, "Scanner: Error adding/updating mediafile to DB", "folder", entry.path, "track", track, err)
return err
}
}
return nil
})
if err != nil {
return err
}
// Save folder to DB
err = tx.Folder(ctx).Put(entry.scanCtx.lib, entry.path)
if err != nil {
log.Error(ctx, "Scanner: Error adding/updating folder to DB", "folder", entry.path, err)
return err
}
return err
})
if err != nil {