diff --git a/server/scan.go b/server/scan.go index 8fb29c8..1ff9d29 100644 --- a/server/scan.go +++ b/server/scan.go @@ -180,22 +180,17 @@ func scanLibrary(ctx context.Context, id int) { 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) - // Mark all Files as Missing - _, err = tx.Exec(ctx, "UPDATE files SET status = $2 where library_id = $1", id, constants.FILE_STATUS_MISSING) - if err != nil { - slog.ErrorContext(ctx, "Setting Missing Status", "err", err) - return - } - + // TODO Work on getting missing status again + /* + // Mark all Files as Missing + _, err = tx.Exec(ctx, "UPDATE files SET status = $2 where library_id = $1", id, constants.FILE_STATUS_MISSING) + if err != nil { + slog.ErrorContext(ctx, "Setting Missing Status", "err", err) + return + } + */ err = filepath.Walk(lpath, func(fullPath string, info os.FileInfo, err error) error { if errors.Is(err, os.ErrPermission) { @@ -223,22 +218,23 @@ func scanLibrary(ctx context.Context, id int) { var fileID int var oldSize uint 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) { // 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) + if err != nil { + slog.ErrorContext(ctx, "ffprobe New File", "err", err) + } + slog.InfoContext(ctx, "ffprobe Done", "path", fullPath) - ffprobeData, err := ffprobe.ProbeURL(ctx, fullPath) - if err != nil { - slog.ErrorContext(ctx, "ffprobe New File", "err", err) - } - 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) - if err != nil { - return fmt.Errorf("Add New File to DB: %w", err) - } + _, 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 { + slog.ErrorContext(ctx, "Add New File to DB", "err", err) + } + }() return nil } else if err != nil { 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 if newModTime.Equal(oldModTime) && uint(info.Size()) == oldSize { 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 { 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) - _, 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 { return fmt.Errorf("Updating Changed File in DB: %w", err) }