Use database status and health, calculate md5
This commit is contained in:
parent
806f6e7e61
commit
78d818b8d1
7 changed files with 112 additions and 37 deletions
|
@ -3,17 +3,22 @@ package server
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var videoFileExtensions = []string{".mkv", ".mp4", ".webm", ".flv", ".avi"}
|
||||
|
||||
func handleScan(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
scanStatus(w, r)
|
||||
|
@ -110,7 +115,7 @@ func scan(ctx context.Context, id string) {
|
|||
slog.InfoContext(ctx, "Checking Files...", "id", id, "path", lpath)
|
||||
|
||||
// Mark all Files as Missing
|
||||
_, err = tx.Exec(ctx, "UPDATE files SET missing = true where library_id = $1", id)
|
||||
_, 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
|
||||
|
@ -129,16 +134,35 @@ func scan(ctx context.Context, id string) {
|
|||
// We don't care about folders
|
||||
return nil
|
||||
}
|
||||
slog.InfoContext(ctx, "Scanning File", "path", path, "size", info.Size())
|
||||
|
||||
if !slices.Contains(videoFileExtensions, filepath.Ext(path)) {
|
||||
slog.InfoContext(ctx, "Skipping non video file", "path", path)
|
||||
return nil
|
||||
}
|
||||
slog.InfoContext(ctx, "Hashing File", "path", path, "size", info.Size())
|
||||
|
||||
file, err := os.Open(path)
|
||||
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", path, "size", info.Size(), "md5", newMD5)
|
||||
|
||||
var fileID int
|
||||
err = tx.QueryRow(ctx, "SELECT id FROM files WHERE library_id = $1 AND path = $2", id, path).Scan(&fileID)
|
||||
var oldMD5 []byte
|
||||
var health constants.FileHealth
|
||||
err = tx.QueryRow(ctx, "SELECT id, md5, health FROM files WHERE library_id = $1 AND path = $2", id, path).Scan(&fileID, &oldMD5, &health)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
// File Does not Exist Yet
|
||||
|
||||
// TODO check file extension and exclude
|
||||
slog.InfoContext(ctx, "File is New", "path", path)
|
||||
_, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, missing) VALUES ($1, $2, $3, $4)", id, path, info.Size(), false)
|
||||
_, err = tx.Exec(ctx, "INSERT INTO files (library_id, path, size, status, health, md5) VALUES ($1, $2, $3, $4, $5, $6)", id, path, info.Size(), constants.FILE_STATUS_EXISTS, constants.FILE_HEALTH_UNKNOWN, newMD5)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Add New File to DB: %w", err)
|
||||
}
|
||||
|
@ -146,8 +170,13 @@ func scan(ctx context.Context, id string) {
|
|||
} else if err != nil {
|
||||
return fmt.Errorf("Getting File: %w", err)
|
||||
}
|
||||
// File Exists so update Size and missing Status
|
||||
_, err = tx.Exec(ctx, "UPDATE files SET size = $1, missing = $2 WHERE id = $3", info.Size(), false, fileID)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Updating File in DB: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue