Fix Subseqent scan chaning all New and Changed File to Exists. Brick Missing for now. Make new file async
Some checks failed
/ release (push) Failing after 11s
Some checks failed
/ release (push) Failing after 11s
This commit is contained in:
parent
4019d1764e
commit
5d6a29407d
1 changed files with 23 additions and 31 deletions
|
@ -180,22 +180,17 @@ func scanLibrary(ctx context.Context, id int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, err := db.Begin(ctx)
|
|
||||||
if err != nil {
|
|
||||||
slog.ErrorContext(ctx, "Begin Transaction", "err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
slog.InfoContext(ctx, "Checking Files...", "id", id, "path", lpath)
|
slog.InfoContext(ctx, "Checking Files...", "id", id, "path", lpath)
|
||||||
|
|
||||||
|
// TODO Work on getting missing status again
|
||||||
|
/*
|
||||||
// Mark all Files as Missing
|
// Mark all Files as Missing
|
||||||
_, err = tx.Exec(ctx, "UPDATE files SET status = $2 where library_id = $1", id, constants.FILE_STATUS_MISSING)
|
_, err = tx.Exec(ctx, "UPDATE files SET status = $2 where library_id = $1", id, constants.FILE_STATUS_MISSING)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "Setting Missing Status", "err", err)
|
slog.ErrorContext(ctx, "Setting Missing Status", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
err = filepath.Walk(lpath,
|
err = filepath.Walk(lpath,
|
||||||
func(fullPath string, info os.FileInfo, err error) error {
|
func(fullPath string, info os.FileInfo, err error) error {
|
||||||
if errors.Is(err, os.ErrPermission) {
|
if errors.Is(err, os.ErrPermission) {
|
||||||
|
@ -223,10 +218,10 @@ func scanLibrary(ctx context.Context, id int) {
|
||||||
var fileID int
|
var fileID int
|
||||||
var oldSize uint
|
var oldSize uint
|
||||||
var oldModTime time.Time
|
var oldModTime time.Time
|
||||||
err = tx.QueryRow(ctx, "SELECT id, size, mod_time FROM files WHERE library_id = $1 AND path = $2", id, fPath).Scan(&fileID, &oldSize, &oldModTime)
|
err = db.QueryRow(ctx, "SELECT id, size, mod_time FROM files WHERE library_id = $1 AND path = $2", id, fPath).Scan(&fileID, &oldSize, &oldModTime)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
// File Does not Exist Yet
|
// File Does not Exist Yet
|
||||||
|
go func() {
|
||||||
slog.InfoContext(ctx, "File is New, Running FFProbe...", "path", fullPath)
|
slog.InfoContext(ctx, "File is New, Running FFProbe...", "path", fullPath)
|
||||||
|
|
||||||
ffprobeData, err := ffprobe.ProbeURL(ctx, fullPath)
|
ffprobeData, err := ffprobe.ProbeURL(ctx, fullPath)
|
||||||
|
@ -235,10 +230,11 @@ func scanLibrary(ctx context.Context, id int) {
|
||||||
}
|
}
|
||||||
slog.InfoContext(ctx, "ffprobe Done", "path", fullPath)
|
slog.InfoContext(ctx, "ffprobe Done", "path", fullPath)
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health, ffprobe_data) VALUES ($1, $2, $3, $4, $5, $6)", id, fPath, info.Size(), constants.FILE_STATUS_NEW, constants.FILE_HEALTH_UNKNOWN, ffprobeData)
|
_, err = db.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health, ffprobe_data) VALUES ($1, $2, $3, $4, $5, $6)", id, fPath, info.Size(), constants.FILE_STATUS_NEW, constants.FILE_HEALTH_UNKNOWN, ffprobeData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Add New File to DB: %w", err)
|
slog.ErrorContext(ctx, "Add New File to DB", "err", err)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("Getting File: %w", err)
|
return fmt.Errorf("Getting File: %w", err)
|
||||||
|
@ -250,10 +246,6 @@ func scanLibrary(ctx context.Context, id int) {
|
||||||
// File Already Exists, Check if it has been changed
|
// File Already Exists, Check if it has been changed
|
||||||
if newModTime.Equal(oldModTime) && uint(info.Size()) == oldSize {
|
if newModTime.Equal(oldModTime) && uint(info.Size()) == oldSize {
|
||||||
slog.Debug("File stayed the same", "id", fileID)
|
slog.Debug("File stayed the same", "id", fileID)
|
||||||
_, err = tx.Exec(ctx, "UPDATE files SET status = $2 WHERE id = $1", fileID, constants.FILE_STATUS_EXISTS)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Updating Non Changed File in DB: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
slog.InfoContext(ctx, "File Has Changed", "path", fullPath, "old_mod_time", oldModTime, "new_mod_time", newModTime, "old_size", oldSize, "new_size", info.Size())
|
slog.InfoContext(ctx, "File Has Changed", "path", fullPath, "old_mod_time", oldModTime, "new_mod_time", newModTime, "old_size", oldSize, "new_size", info.Size())
|
||||||
|
|
||||||
|
@ -263,7 +255,7 @@ func scanLibrary(ctx context.Context, id int) {
|
||||||
}
|
}
|
||||||
slog.InfoContext(ctx, "ffprobe Done", "path", fullPath)
|
slog.InfoContext(ctx, "ffprobe Done", "path", fullPath)
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4, mod_time = $5, ffprobe_data = $6 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_CHANGED, constants.FILE_HEALTH_UNKNOWN, newModTime, ffprobeData)
|
_, err = db.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4, mod_time = $5, ffprobe_data = $6 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_CHANGED, constants.FILE_HEALTH_UNKNOWN, newModTime, ffprobeData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Updating Changed File in DB: %w", err)
|
return fmt.Errorf("Updating Changed File in DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue