This commit is contained in:
parent
a6e10369bb
commit
a2efbdea4c
1 changed files with 103 additions and 37 deletions
140
server/upload.go
140
server/upload.go
|
@ -1,13 +1,17 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.lastassault.de/speatzle/morffix/constants"
|
"git.lastassault.de/speatzle/morffix/constants"
|
||||||
)
|
)
|
||||||
|
@ -18,30 +22,56 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := r.PathValue("id")
|
strid := r.PathValue("id")
|
||||||
if id == "" {
|
if strid == "" {
|
||||||
http.Error(w, "No id", http.StatusBadRequest)
|
http.Error(w, "No id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var count int
|
id, err := strconv.Atoi(strid)
|
||||||
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 {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Query Task Count", "err", err)
|
errorUpload(r, w, 0, "Convert File ID to int", err)
|
||||||
http.Error(w, "Error Query Task Count: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if count < 1 {
|
if r.Header.Get(constants.WORKER_VERSION_HEADER) != constants.WORKER_VERSION {
|
||||||
slog.ErrorContext(r.Context(), "No Running Task for file", "id", id)
|
http.Error(w, "Wrong Worker Version", http.StatusBadRequest)
|
||||||
http.Error(w, "No Running Task for file: "+id, 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := base64.StdEncoding.DecodeString(r.Header.Get(constants.HASH_HEADER))
|
hash, err := base64.StdEncoding.DecodeString(r.Header.Get(constants.HASH_HEADER))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Decode Hash", "err", err)
|
errorUpload(r, w, taskid, "Decode Hash", err)
|
||||||
http.Error(w, "Decode Hash: "+err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +79,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
var libraryID int
|
var libraryID int
|
||||||
err = db.QueryRow(r.Context(), "SELECT path, library_id FROM files WHERE id = $1", id).Scan(&fPath, &libraryID)
|
err = db.QueryRow(r.Context(), "SELECT path, library_id FROM files WHERE id = $1", id).Scan(&fPath, &libraryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Query File Path", "err", err)
|
errorUpload(r, w, taskid, "Query File Path", err)
|
||||||
http.Error(w, "Error Query File Path: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,17 +88,14 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
var tReplace bool
|
var tReplace bool
|
||||||
err = db.QueryRow(r.Context(), "SELECT path, transcode_path, transcode_replace FROM libraries WHERE id = $1", libraryID).Scan(&lPath, &tPath, &tReplace)
|
err = db.QueryRow(r.Context(), "SELECT path, transcode_path, transcode_replace FROM libraries WHERE id = $1", libraryID).Scan(&lPath, &tPath, &tReplace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Query Library Path", "err", err)
|
errorUpload(r, w, taskid, "Query Library Path", err)
|
||||||
http.Error(w, "Error Query Library Path: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When replacing, send to temp, otherwise write directly
|
||||||
var path string
|
var path string
|
||||||
if tReplace {
|
if tReplace {
|
||||||
//path = filepath.Join(lPath, fPath)
|
path = filepath.Join(lPath, fPath+constants.TEMP_FILE_EXTENSION)
|
||||||
slog.ErrorContext(r.Context(), "Replace mode is not implemented")
|
|
||||||
http.Error(w, "Replace mode is not implemented", http.StatusNotImplemented)
|
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
path = filepath.Join(tPath, fPath)
|
path = filepath.Join(tPath, fPath)
|
||||||
}
|
}
|
||||||
|
@ -78,15 +104,13 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = r.ParseMultipartForm(100 << 20)
|
err = r.ParseMultipartForm(100 << 20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err)
|
errorUpload(r, w, taskid, "Parse Multipart Form", err)
|
||||||
http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
srcFile, _, err := r.FormFile(constants.FORM_FILE_KEY)
|
srcFile, _, err := r.FormFile(constants.FORM_FILE_KEY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err)
|
errorUpload(r, w, taskid, "Form File", err)
|
||||||
http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,17 +118,18 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
// MaxBytesReader closes the underlying io.Reader on its Close() is called
|
// MaxBytesReader closes the underlying io.Reader on its Close() is called
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
err = os.MkdirAll(filepath.Dir(path), 0775)
|
// if we are not replacing then we dont know if the destination folder exists
|
||||||
if err != nil {
|
if !tReplace {
|
||||||
slog.ErrorContext(r.Context(), "Creating Folder", "err", err)
|
err = os.MkdirAll(filepath.Dir(path), 0775)
|
||||||
http.Error(w, "Creating Folder: "+err.Error(), http.StatusInternalServerError)
|
if err != nil {
|
||||||
return
|
errorUpload(r, w, taskid, "Creating Folder", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := os.Create(path)
|
out, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(r.Context(), "Creating File", "err", err)
|
errorUpload(r, w, taskid, "Creating File", err)
|
||||||
http.Error(w, "Creating File: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
@ -113,17 +138,58 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var maxBytesError *http.MaxBytesError
|
var maxBytesError *http.MaxBytesError
|
||||||
if errors.As(err, &maxBytesError) {
|
if errors.As(err, &maxBytesError) {
|
||||||
slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err)
|
slog.ErrorContext(r.Context(), "File to Large", "err", err)
|
||||||
http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusRequestEntityTooLarge)
|
http.Error(w, "File to Large: "+err.Error(), http.StatusRequestEntityTooLarge)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
slog.ErrorContext(r.Context(), "Failed to write the uploaded content", "err", err)
|
errorUpload(r, w, taskid, "Failed to write the uploaded content", err)
|
||||||
http.Error(w, "Failed to write the uploaded content: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check file integrity
|
|
||||||
_ = hash
|
|
||||||
|
|
||||||
slog.InfoContext(r.Context(), "upload done", "written", written)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue