From 3ec0e28272f429d818e98833840848c160da8519 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Fri, 5 Jul 2024 19:37:23 +0200 Subject: [PATCH] wip --- constants/constants.go | 3 ++ .../000015_alter_files_table_hash.down.sql | 2 + .../000015_alter_files_table_hash.up.sql | 2 + server/scan.go | 51 +++++++++---------- 4 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 migrations/000015_alter_files_table_hash.down.sql create mode 100644 migrations/000015_alter_files_table_hash.up.sql diff --git a/constants/constants.go b/constants/constants.go index 99736a3..dfcb9ab 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -87,6 +87,7 @@ const ( FILE_STATUS_MISSING FILE_STATUS_EXISTS FILE_STATUS_CHANGED + FILE_STATUS_NEW ) func (s FileStatus) String() string { @@ -99,6 +100,8 @@ func (s FileStatus) String() string { return "Exists" case FILE_STATUS_CHANGED: return "Changed" + case FILE_STATUS_NEW: + return "New" default: return fmt.Sprintf("%d", int(s)) } diff --git a/migrations/000015_alter_files_table_hash.down.sql b/migrations/000015_alter_files_table_hash.down.sql new file mode 100644 index 0000000..ee61bdc --- /dev/null +++ b/migrations/000015_alter_files_table_hash.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE files +ALTER COLUMN hash bigint SET NOT NULL; \ No newline at end of file diff --git a/migrations/000015_alter_files_table_hash.up.sql b/migrations/000015_alter_files_table_hash.up.sql new file mode 100644 index 0000000..c282260 --- /dev/null +++ b/migrations/000015_alter_files_table_hash.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE files +ALTER COLUMN hash bigint DROP NOT NULL; \ No newline at end of file diff --git a/server/scan.go b/server/scan.go index 4e9d49e..5d3bb1a 100644 --- a/server/scan.go +++ b/server/scan.go @@ -3,10 +3,8 @@ package server import ( "bytes" "context" - "crypto/md5" "errors" "fmt" - "io" "log/slog" "net/http" "os" @@ -31,8 +29,6 @@ func handleScan(w http.ResponseWriter, r *http.Request) { return } - full := r.FormValue("full") == "on" - var name string var path string var enabled bool @@ -44,7 +40,7 @@ func handleScan(w http.ResponseWriter, r *http.Request) { } scanCtx := context.Background() - go scan(scanCtx, id, full) + go scan(scanCtx, id) message := "Scan Started" @@ -79,7 +75,7 @@ func scanStatus(w http.ResponseWriter, r *http.Request) { } } -func scan(ctx context.Context, id string, full bool) { +func scan(ctx context.Context, id string) { slog.InfoContext(ctx, "Starting Scan", "id", id) // TODO Scan settings: @@ -150,35 +146,36 @@ func scan(ctx context.Context, id string, full bool) { slog.InfoContext(ctx, "Skipping non video file", "path", fullPath) return nil } - slog.InfoContext(ctx, "Hashing File", "path", fullPath, "size", info.Size()) + /* + slog.InfoContext(ctx, "Hashing File", "path", fullPath, "size", info.Size()) - file, err := os.Open(fullPath) - if err != nil { - return fmt.Errorf("Opening File: %w", err) - } + file, err := os.Open(fullPath) + if err != nil { + return fmt.Errorf("Opening File: %w", err) + } - hash := md5.New() - if _, err := io.Copy(hash, file); err != nil { - return fmt.Errorf("Reading File: %w", err) - } - newMD5 := hash.Sum(nil) - - slog.InfoContext(ctx, "File MD5", "path", fullPath, "size", info.Size(), "md5", newMD5) + hash := md5.New() + if _, err := io.Copy(hash, file); err != nil { + return fmt.Errorf("Reading File: %w", err) + } + newMD5 := hash.Sum(nil) + slog.InfoContext(ctx, "File MD5", "path", fullPath, "size", info.Size(), "md5", newMD5) + */ fPath, err := filepath.Rel(lpath, fullPath) if err != nil { return fmt.Errorf("Getting Relative Path: %w", err) } var fileID int - var oldMD5 []byte + var size uint var health constants.FileHealth - err = tx.QueryRow(ctx, "SELECT id, md5, health FROM files WHERE library_id = $1 AND path = $2", id, fPath).Scan(&fileID, &oldMD5, &health) + err = tx.QueryRow(ctx, "SELECT id, size, health FROM files WHERE library_id = $1 AND path = $2", id, fPath).Scan(&fileID, &size, &health) if errors.Is(err, pgx.ErrNoRows) { // File Does not Exist Yet slog.InfoContext(ctx, "File is New", "path", fullPath) - _, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health, md5) VALUES ($1, $2, $3, $4, $5, $6)", id, fPath, info.Size(), constants.FILE_STATUS_EXISTS, constants.FILE_HEALTH_UNKNOWN, newMD5) + _, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health) VALUES ($1, $2, $3, $4, $5)", id, fPath, info.Size(), constants.FILE_STATUS_NEW, constants.FILE_HEALTH_UNKNOWN) if err != nil { return fmt.Errorf("Add New File to DB: %w", err) } @@ -187,12 +184,14 @@ func scan(ctx context.Context, id string, full bool) { return fmt.Errorf("Getting File: %w", err) } - if slices.Compare[[]byte](newMD5, oldMD5) != 0 { - // File has changed on disk so reset health - health = constants.FILE_HEALTH_UNKNOWN - } + /* + if slices.Compare[[]byte](newMD5, oldMD5) != 0 { + // File has changed on disk so reset health + health = constants.FILE_HEALTH_UNKNOWN + } + */ // File Exists so update Size, status and hash - _, err = tx.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4, md5 = $5 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_EXISTS, health, newMD5) + _, err = tx.Exec(ctx, "UPDATE files SET size = $2, status = $3, health = $4 WHERE id = $1", fileID, info.Size(), constants.FILE_STATUS_EXISTS, health) if err != nil { return fmt.Errorf("Updating File in DB: %w", err) }