From 570f0865870d416ca1f30fc37a885bbbb48033f0 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Fri, 30 May 2025 08:48:44 +0200 Subject: [PATCH] fff --- main.go | 56 ++++++-------------------------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/main.go b/main.go index bdeffb8..030b0f5 100644 --- a/main.go +++ b/main.go @@ -257,6 +257,12 @@ func handleFileUpload(r *http.Request, fieldName string) (string, error) { _, err = io.Copy(out, file) if err != nil { return "", fmt.Errorf("failed to save file: %v", err) + } + + return filename, nil +} + +func CreateAppHandler(w http.ResponseWriter, r *http.Request) { // Parse form data err := r.ParseMultipartForm(10 << 20) // 10 MB max file size if err != nil { @@ -302,56 +308,6 @@ func handleFileUpload(r *http.Request, fieldName string) (string, error) { w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(app) } - - // Handle file upload - var iconPath string - file, handler, err := r.FormFile("icon") - if err == nil { - defer file.Close() - - // Create uploads directory if it doesn't exist - if _, err := os.Stat("uploads"); os.IsNotExist(err) { - os.Mkdir("uploads", 0755) - } - - // Generate a unique filename - ext := "" - if parts := strings.Split(handler.Filename, "."); len(parts) > 1 { - ext = "." + parts[len(parts)-1] - } - iconPath = fmt.Sprintf("icon_%d%s", time.Now().UnixNano(), ext) - - // Create the file - f, err := os.Create(filepath.Join("uploads", iconPath)) - if err != nil { - http.Error(w, "Error saving file", http.StatusInternalServerError) - return - } - defer f.Close() - - // Copy the uploaded file to the created file - _, err = io.Copy(f, file) - if err != nil { - http.Error(w, "Error saving file", http.StatusInternalServerError) - return - } - } - - // In a real app, this would save to a database - app := App{ - ID: fmt.Sprintf("%d", time.Now().UnixNano()), - Name: name, - URL: url, - Description: description, - Icon: iconPath, - CreatedAt: time.Now().Format(time.RFC3339), - UpdatedAt: time.Now().Format(time.RFC3339), - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - json.NewEncoder(w).Encode(app) -} func UpdateAppHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r)