From 0ce5b464498222a4d01a9291f94a4c8e75de8302 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Wed, 19 Mar 2025 17:47:40 +0100 Subject: [PATCH] fix sql, improve errors, run ffprobe --- server/upload.go | 27 ++++++++++++++------------- task/upload.go | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/server/upload.go b/server/upload.go index 2d70515..f811edc 100644 --- a/server/upload.go +++ b/server/upload.go @@ -14,6 +14,7 @@ import ( "time" "git.lastassault.de/speatzle/morffix/constants" + "gopkg.in/vansante/go-ffprobe.v2" ) func handleUpload(w http.ResponseWriter, r *http.Request) { @@ -96,7 +97,10 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { var path string if tReplace { path = filepath.Join(lPath, fPath+constants.TEMP_FILE_EXTENSION) + // if replace then this is a temp file and should be cleaned up on error + defer os.Remove(path) } else { + // todo write this to a temp file first also to be able to run cleanup on error and unify the rename logic path = filepath.Join(tPath, fPath) } @@ -175,24 +179,21 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { modTime := info.ModTime().UTC().Round(time.Second) size := info.Size() + ffprobeData, err := ffprobe.ProbeURL(r.Context(), dstPath) + if err != nil { + errorUpload(r, w, taskid, "ffProbe File", err) + return + } + _, err = tx.Exec(r.Context(), "UPDATE files SET old_md5 = md5, old_size = size, old_ffprobe_data = ffprobe_data WHERE id = $1", fileid) if err != nil { - errorUpload(r, w, taskid, "Set File Hash", err) + errorUpload(r, w, taskid, "Copy Filed Current Values to Old Values", err) return } - // TODO insert new ffprobe data, or schedule new scan somehow? - // maybe let the scan to all of this? - - _, err = tx.Exec(r.Context(), "UPDATE files SET md5 = $2, size = $3, mod_time = $3 WHERE id = $1", fileid, hash, size, modTime) + _, err = tx.Exec(r.Context(), "UPDATE files SET md5 = $2, size = $3, mod_time = $4, ffprobe_data = $5 WHERE id = $1", fileid, hash, size, modTime, ffprobeData) if err != nil { - errorUpload(r, w, taskid, "Set File Hash", err) - return - } - - _, err = tx.Exec(r.Context(), "UPDATE files SET md5 = $2, size = $3, mod_time = $3 WHERE id = $1", fileid, hash, size, modTime) - if err != nil { - errorUpload(r, w, taskid, "Set File Hash", err) + errorUpload(r, w, taskid, "Set New File Values", err) return } @@ -212,7 +213,7 @@ func errorUpload(r *http.Request, w http.ResponseWriter, taskID int, msg string, slog.ErrorContext(r.Context(), msg, "err", err) http.Error(w, msg+": "+err.Error(), http.StatusInternalServerError) if taskID != 0 { - _, err2 := db.Exec(context.TODO(), "UPDATE tasks SET log = log || $2 WHERE id = $1", taskID, []string{fmt.Sprintf("%v MASTER: upload error:"+msg+":"+err.Error(), time.Now())}) + _, err2 := db.Exec(context.TODO(), "UPDATE tasks SET log = log || $2 WHERE id = $1", taskID, []string{fmt.Sprintf("%v MASTER: upload error: "+msg+": "+err.Error(), time.Now())}) if err != nil { slog.ErrorContext(r.Context(), "Updating task log with upload error", "err", err2) } diff --git a/task/upload.go b/task/upload.go index a2338e8..dda30c6 100644 --- a/task/upload.go +++ b/task/upload.go @@ -100,7 +100,7 @@ func uploadFile(ctx context.Context, l *slog.Logger, conf config.Config, path st if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("Got HTTP Status Code: %v Body: %v", resp.StatusCode, body) + return fmt.Errorf("Got HTTP Status Code: %v Body: %v", resp.StatusCode, string(body)) } return nil