diff --git a/constants/constants.go b/constants/constants.go index 56cb06c..1ddd216 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -2,14 +2,13 @@ package constants import "fmt" -const WORKER_VERSION = "v2" +const WORKER_VERSION = "v1" const WORKER_VERSION_HEADER = "morffix-version" const SHARED_SECRET_HEADER = "morffix-secret" const NAME_HEADER = "morffix-name" const UUID_HEADER = "morffix-uuid" const HASH_HEADER = "morffix-hash" -const TASK_ID_HEADER = "morffix-task-id" const INDEX_TEMPLATE_NAME = "index.tmpl" const LIBRARIES_TEMPLATE_NAME = "libraries.tmpl" @@ -27,8 +26,6 @@ const SORT_ORDER_ASC_PARAM = "order_asc" const QUERY_LIMIT_PARAM = "limit" const QUERY_PAGE_PARAM = "page" -const TEMP_FILE_EXTENSION = ".morffix" - type TaskType int const ( diff --git a/server/upload.go b/server/upload.go index 3028070..437528b 100644 --- a/server/upload.go +++ b/server/upload.go @@ -1,17 +1,13 @@ package server import ( - "context" "encoding/base64" "errors" - "fmt" "io" "log/slog" "net/http" "os" "path/filepath" - "strconv" - "time" "git.lastassault.de/speatzle/morffix/constants" ) @@ -22,56 +18,30 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { return } - strid := r.PathValue("id") - if strid == "" { + id := r.PathValue("id") + if id == "" { http.Error(w, "No id", http.StatusBadRequest) return } - id, err := strconv.Atoi(strid) + var count int + err := db.QueryRow(r.Context(), "SELECT COUNT(*) FROM tasks WHERE file_id = $1 AND worker_id = $2", id, r.Header.Get(constants.UUID_HEADER)).Scan(&count) if err != nil { - errorUpload(r, w, 0, "Convert File ID to int", err) + slog.ErrorContext(r.Context(), "Query Task Count", "err", err) + http.Error(w, "Error Query Task Count: "+err.Error(), http.StatusInternalServerError) return } - if r.Header.Get(constants.WORKER_VERSION_HEADER) != constants.WORKER_VERSION { - http.Error(w, "Wrong Worker Version", http.StatusBadRequest) - return - } - - taskid, err := strconv.Atoi(r.Header.Get(constants.TASK_ID_HEADER)) - if err != nil { - errorUpload(r, w, 0, "Convert Task ID to int", err) - return - } - - var fileid int - var workerid string - var status constants.TaskStatus - err = db.QueryRow(r.Context(), "SELECT file_id, worker_id, status FROM files WHERE id = $1", taskid).Scan(&fileid, &workerid, &status) - if err != nil { - errorUpload(r, w, 0, "Getting Task for Upload", err) - return - } - - if workerid != r.Header.Get(constants.UUID_HEADER) { - errorUpload(r, w, taskid, "Worker is not Assigned to Task", err) - return - } - - if fileid != id { - errorUpload(r, w, taskid, "File is not for this Task", err) - return - } - - if !(status == constants.TASK_STATUS_RUNNING || status == constants.TASK_STATUS_UNKNOWN) { - errorUpload(r, w, taskid, "File can only be uploaded when task status is running or unknown", err) + if count < 1 { + slog.ErrorContext(r.Context(), "No Running Task for file", "id", id) + http.Error(w, "No Running Task for file: "+id, http.StatusBadRequest) return } hash, err := base64.StdEncoding.DecodeString(r.Header.Get(constants.HASH_HEADER)) if err != nil { - errorUpload(r, w, taskid, "Decode Hash", err) + slog.ErrorContext(r.Context(), "Decode Hash", "err", err) + http.Error(w, "Decode Hash: "+err.Error(), http.StatusBadRequest) return } @@ -79,7 +49,8 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { var libraryID int err = db.QueryRow(r.Context(), "SELECT path, library_id FROM files WHERE id = $1", id).Scan(&fPath, &libraryID) if err != nil { - errorUpload(r, w, taskid, "Query File Path", err) + slog.ErrorContext(r.Context(), "Query File Path", "err", err) + http.Error(w, "Error Query File Path: "+err.Error(), http.StatusInternalServerError) return } @@ -88,14 +59,17 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { var tReplace bool err = db.QueryRow(r.Context(), "SELECT path, transcode_path, transcode_replace FROM libraries WHERE id = $1", libraryID).Scan(&lPath, &tPath, &tReplace) if err != nil { - errorUpload(r, w, taskid, "Query Library Path", err) + slog.ErrorContext(r.Context(), "Query Library Path", "err", err) + http.Error(w, "Error Query Library Path: "+err.Error(), http.StatusInternalServerError) return } - // When replacing, send to temp, otherwise write directly var path string if tReplace { - path = filepath.Join(lPath, fPath+constants.TEMP_FILE_EXTENSION) + //path = filepath.Join(lPath, fPath) + slog.ErrorContext(r.Context(), "Replace mode is not implemented") + http.Error(w, "Replace mode is not implemented", http.StatusNotImplemented) + return } else { path = filepath.Join(tPath, fPath) } @@ -104,13 +78,15 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { err = r.ParseMultipartForm(100 << 20) if err != nil { - errorUpload(r, w, taskid, "Parse Multipart Form", err) + slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err) + http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusInternalServerError) return } srcFile, _, err := r.FormFile(constants.FORM_FILE_KEY) if err != nil { - errorUpload(r, w, taskid, "Form File", err) + slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err) + http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusInternalServerError) return } @@ -118,18 +94,17 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { // MaxBytesReader closes the underlying io.Reader on its Close() is called defer src.Close() - // if we are not replacing then we dont know if the destination folder exists - if !tReplace { - err = os.MkdirAll(filepath.Dir(path), 0775) - if err != nil { - errorUpload(r, w, taskid, "Creating Folder", err) - return - } + err = os.MkdirAll(filepath.Dir(path), 0775) + if err != nil { + slog.ErrorContext(r.Context(), "Creating Folder", "err", err) + http.Error(w, "Creating Folder: "+err.Error(), http.StatusInternalServerError) + return } out, err := os.Create(path) if err != nil { - errorUpload(r, w, taskid, "Creating File", err) + slog.ErrorContext(r.Context(), "Creating File", "err", err) + http.Error(w, "Creating File: "+err.Error(), http.StatusInternalServerError) return } defer out.Close() @@ -138,58 +113,17 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { if err != nil { var maxBytesError *http.MaxBytesError if errors.As(err, &maxBytesError) { - slog.ErrorContext(r.Context(), "File to Large", "err", err) - http.Error(w, "File to Large: "+err.Error(), http.StatusRequestEntityTooLarge) + slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err) + http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusRequestEntityTooLarge) return } - errorUpload(r, w, taskid, "Failed to write the uploaded content", err) + slog.ErrorContext(r.Context(), "Failed to write the uploaded content", "err", err) + http.Error(w, "Failed to write the uploaded content: "+err.Error(), http.StatusInternalServerError) return } + // TODO check file integrity + _ = hash + slog.InfoContext(r.Context(), "upload done", "written", written) - - out.Close() - - // if we are replacing, then we need to change the original file to the uploaded file and update the hash in the database - if tReplace { - tx, err := db.Begin(r.Context()) - if err != nil { - errorUpload(r, w, taskid, "Begin Transaction", err) - return - } - - _, err = tx.Exec(r.Context(), "UPDATE files SET hash = $2 WHERE id = $1", fileid, hash) - if err != nil { - errorUpload(r, w, taskid, "Set File Hash", err) - return - } - - dstPath := filepath.Join(lPath, fPath) - slog.InfoContext(r.Context(), "Replacing Original file With Uploaded File", "fileid", id, "path", path, "dstPath", dstPath) - - err = os.Rename(path, dstPath) - if err != nil { - errorUpload(r, w, taskid, "Replace File", err) - return - } - - err = tx.Commit(r.Context()) - if err != nil { - errorUpload(r, w, taskid, "Commit File Hash", err) - return - } - - slog.InfoContext(r.Context(), "Original file Replaced with Uploaded File", "fileid", id, "dstPath", dstPath) - } -} - -func errorUpload(r *http.Request, w http.ResponseWriter, taskID int, msg string, err error) { - 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())}) - 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 38c0f94..ec988ae 100644 --- a/task/upload.go +++ b/task/upload.go @@ -9,7 +9,6 @@ import ( "mime/multipart" "net/http" "os" - "strconv" "time" "git.lastassault.de/speatzle/morffix/config" @@ -83,7 +82,6 @@ func uploadFile(ctx context.Context, l *slog.Logger, conf config.Config, path st req.Header.Add(constants.UUID_HEADER, uuid.String()) req.Header.Add(constants.WORKER_VERSION_HEADER, constants.WORKER_VERSION) req.Header.Add(constants.HASH_HEADER, base64.StdEncoding.EncodeToString(hash)) - req.Header.Add(constants.TASK_ID_HEADER, strconv.Itoa(t.ID)) req.Header.Add("Content-Type", "multipart/form-data; boundary=\""+m.Boundary()+"\"") var client = &http.Client{