fix sql, improve errors, run ffprobe
All checks were successful
/ release (push) Successful in 47s

This commit is contained in:
Samuel Lorch 2025-03-19 17:47:40 +01:00
parent 64fd3da925
commit 0ce5b46449
2 changed files with 15 additions and 14 deletions

View file

@ -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)
}

View file

@ -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