parent
b03e85db0b
commit
cd3c06d7d0
4 changed files with 32 additions and 26 deletions
|
@ -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))
|
||||
}
|
||||
|
|
2
migrations/000015_alter_files_table_hash.down.sql
Normal file
2
migrations/000015_alter_files_table_hash.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE files
|
||||
ALTER COLUMN hash bigint SET NOT NULL;
|
2
migrations/000015_alter_files_table_hash.up.sql
Normal file
2
migrations/000015_alter_files_table_hash.up.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE files
|
||||
ALTER COLUMN hash bigint DROP NOT NULL;
|
|
@ -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,6 +146,7 @@ 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())
|
||||
|
||||
file, err := os.Open(fullPath)
|
||||
|
@ -164,21 +161,21 @@ func scan(ctx context.Context, id string, full bool) {
|
|||
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
|
||||
}
|
||||
*/
|
||||
// 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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue