parent
b03e85db0b
commit
cd3c06d7d0
4 changed files with 32 additions and 26 deletions
|
@ -87,6 +87,7 @@ const (
|
||||||
FILE_STATUS_MISSING
|
FILE_STATUS_MISSING
|
||||||
FILE_STATUS_EXISTS
|
FILE_STATUS_EXISTS
|
||||||
FILE_STATUS_CHANGED
|
FILE_STATUS_CHANGED
|
||||||
|
FILE_STATUS_NEW
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s FileStatus) String() string {
|
func (s FileStatus) String() string {
|
||||||
|
@ -99,6 +100,8 @@ func (s FileStatus) String() string {
|
||||||
return "Exists"
|
return "Exists"
|
||||||
case FILE_STATUS_CHANGED:
|
case FILE_STATUS_CHANGED:
|
||||||
return "Changed"
|
return "Changed"
|
||||||
|
case FILE_STATUS_NEW:
|
||||||
|
return "New"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d", int(s))
|
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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -31,8 +29,6 @@ func handleScan(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
full := r.FormValue("full") == "on"
|
|
||||||
|
|
||||||
var name string
|
var name string
|
||||||
var path string
|
var path string
|
||||||
var enabled bool
|
var enabled bool
|
||||||
|
@ -44,7 +40,7 @@ func handleScan(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
scanCtx := context.Background()
|
scanCtx := context.Background()
|
||||||
go scan(scanCtx, id, full)
|
go scan(scanCtx, id)
|
||||||
|
|
||||||
message := "Scan Started"
|
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)
|
slog.InfoContext(ctx, "Starting Scan", "id", id)
|
||||||
|
|
||||||
// TODO Scan settings:
|
// 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)
|
slog.InfoContext(ctx, "Skipping non video file", "path", fullPath)
|
||||||
return nil
|
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)
|
file, err := os.Open(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Opening File: %w", err)
|
return fmt.Errorf("Opening File: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash := md5.New()
|
hash := md5.New()
|
||||||
if _, err := io.Copy(hash, file); err != nil {
|
if _, err := io.Copy(hash, file); err != nil {
|
||||||
return fmt.Errorf("Reading File: %w", err)
|
return fmt.Errorf("Reading File: %w", err)
|
||||||
}
|
}
|
||||||
newMD5 := hash.Sum(nil)
|
newMD5 := hash.Sum(nil)
|
||||||
|
|
||||||
slog.InfoContext(ctx, "File MD5", "path", fullPath, "size", info.Size(), "md5", newMD5)
|
|
||||||
|
|
||||||
|
slog.InfoContext(ctx, "File MD5", "path", fullPath, "size", info.Size(), "md5", newMD5)
|
||||||
|
*/
|
||||||
fPath, err := filepath.Rel(lpath, fullPath)
|
fPath, err := filepath.Rel(lpath, fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Getting Relative Path: %w", err)
|
return fmt.Errorf("Getting Relative Path: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileID int
|
var fileID int
|
||||||
var oldMD5 []byte
|
var size uint
|
||||||
var health constants.FileHealth
|
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) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
// File Does not Exist Yet
|
// File Does not Exist Yet
|
||||||
|
|
||||||
slog.InfoContext(ctx, "File is New", "path", fullPath)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Add New File to DB: %w", err)
|
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)
|
return fmt.Errorf("Getting File: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if slices.Compare[[]byte](newMD5, oldMD5) != 0 {
|
/*
|
||||||
// File has changed on disk so reset health
|
if slices.Compare[[]byte](newMD5, oldMD5) != 0 {
|
||||||
health = constants.FILE_HEALTH_UNKNOWN
|
// File has changed on disk so reset health
|
||||||
}
|
health = constants.FILE_HEALTH_UNKNOWN
|
||||||
|
}
|
||||||
|
*/
|
||||||
// File Exists so update Size, status and hash
|
// 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Updating File in DB: %w", err)
|
return fmt.Errorf("Updating File in DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue