package server import ( "encoding/base64" "errors" "io" "log/slog" "net/http" "os" "path/filepath" "git.lastassault.de/speatzle/morffix/constants" ) func handleUpload(w http.ResponseWriter, r *http.Request) { if r.Header.Get(constants.SHARED_SECRET_HEADER) != conf.SharedSecret { w.WriteHeader(http.StatusUnauthorized) return } id := r.PathValue("id") if id == "" { http.Error(w, "No id", http.StatusBadRequest) return } 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 { slog.ErrorContext(r.Context(), "Query Task Count", "err", err) http.Error(w, "Error Query Task Count: "+err.Error(), http.StatusInternalServerError) return } 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 { slog.ErrorContext(r.Context(), "Decode Hash", "err", err) http.Error(w, "Decode Hash: "+err.Error(), http.StatusBadRequest) return } var fPath string var libraryID int err = db.QueryRow(r.Context(), "SELECT path, library_id FROM files WHERE id = $1", id).Scan(&fPath, &libraryID) if err != nil { slog.ErrorContext(r.Context(), "Query File Path", "err", err) http.Error(w, "Error Query File Path: "+err.Error(), http.StatusInternalServerError) return } var lPath string var tPath string 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 { slog.ErrorContext(r.Context(), "Query Library Path", "err", err) http.Error(w, "Error Query Library Path: "+err.Error(), http.StatusInternalServerError) return } var path string if tReplace { //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) } slog.InfoContext(r.Context(), "Starting multipart Parsing for file", "id", id, "path", path) err = r.ParseMultipartForm(100 << 20) if err != nil { 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 { slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err) http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusInternalServerError) return } src := http.MaxBytesReader(w, srcFile, 1000000<<20) // MaxBytesReader closes the underlying io.Reader on its Close() is called defer src.Close() 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 { slog.ErrorContext(r.Context(), "Creating File", "err", err) http.Error(w, "Creating File: "+err.Error(), http.StatusInternalServerError) return } defer out.Close() written, err := io.Copy(out, src) if err != nil { var maxBytesError *http.MaxBytesError if errors.As(err, &maxBytesError) { slog.ErrorContext(r.Context(), "Parse Multipart Form", "err", err) http.Error(w, "Parse Multipart Form: "+err.Error(), http.StatusRequestEntityTooLarge) return } 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) }