package server import ( "context" "crypto/md5" "fmt" "io" "log/slog" "os" "path/filepath" "slices" "git.lastassault.de/speatzle/morffix/constants" ) func hashFile(ctx context.Context, id uint) error { slog.InfoContext(ctx, "Hashing File", "id", id) var filePath string var libraryPath string var oldHash []byte err := db.QueryRow(ctx, "SELECT f.path, l.path, md5 FROM files f INNER JOIN libraries l ON l.id = f.library_id WHERE f.id = $1", id).Scan(&filePath, &libraryPath, &oldHash) if err != nil { return fmt.Errorf("Get File: %w", err) } fullPath := filepath.Join(libraryPath, filePath) 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) } newHash := hash.Sum(nil) if slices.Compare[[]byte](newHash, oldHash) != 0 { // File has changed // TODO Queue healthcheck / transcode if enabled } tx, err := db.Begin(ctx) if err != nil { return fmt.Errorf("Begin Transaction: %w", err) } defer tx.Rollback(ctx) _, err = tx.Exec(ctx, "UPDATE files SET status = $2, md5 = $3 WHERE id = $1", id, constants.FILE_STATUS_EXISTS, newHash) if err != nil { return fmt.Errorf("Updating File in DB: %w", err) } err = tx.Commit(ctx) if err != nil { return fmt.Errorf("Committing Changes: %w", err) } slog.InfoContext(ctx, "Hashing File Done", "id", id) return nil }