|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | +) |
| 10 | + |
| 11 | +func FileUpload(w http.ResponseWriter, r *http.Request) { |
| 12 | + // Limit the size of the incoming request body to prevent abuse (e.g. 100MB) |
| 13 | + const maxUploadSize = 100 << 20 // 100 MB |
| 14 | + r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize) |
| 15 | + |
| 16 | + // Parse the form data |
| 17 | + if err := r.ParseForm(); err != nil { |
| 18 | + http.Error(w, "Error parsing form", http.StatusBadRequest) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Get the filename |
| 23 | + filename := r.FormValue("file") |
| 24 | + if filename == "" { |
| 25 | + http.Error(w, "Filename query parameter is required", http.StatusBadRequest) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + // Define the destination path |
| 30 | + uploadDir := "./uploads" |
| 31 | + if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil { |
| 32 | + http.Error(w, "Failed to create upload directory: "+err.Error(), http.StatusInternalServerError) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + destPath := filepath.Join(uploadDir, filename) |
| 37 | + // Create the destination file |
| 38 | + destFile, err := os.Create(destPath) |
| 39 | + if err != nil { |
| 40 | + http.Error(w, "failed to create destination file: "+err.Error(), http.StatusInternalServerError) |
| 41 | + return |
| 42 | + } |
| 43 | + defer destFile.Close() |
| 44 | + |
| 45 | + // Copy the raw body to the destination file |
| 46 | + if _, err := io.Copy(destFile, r.Body); err != nil { |
| 47 | + http.Error(w, "failed to save file:"+err.Error(), http.StatusInternalServerError) |
| 48 | + return |
| 49 | + } |
| 50 | + // Respond with a success message |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + fmt.Fprintf(w, "File uploaded successfully: %s\n", filename) |
| 53 | +} |
| 54 | + |
| 55 | +func FileUploadMultipart(w http.ResponseWriter, r *http.Request) { |
| 56 | + // Limit the size of the incoming request body to prevent abuse (e.g. 100MB) |
| 57 | + const maxUploadSize = 100 << 20 // 100 MB |
| 58 | + r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize) |
| 59 | + |
| 60 | + // Parse the multipart form |
| 61 | + if err := r.ParseMultipartForm(10 << 20); err != nil { |
| 62 | + http.Error(w, "failed to parse multipart form: "+err.Error(), http.StatusBadRequest) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Retrieve the file from the form data |
| 67 | + file, fileHeader, err := r.FormFile("file") |
| 68 | + if err != nil { |
| 69 | + http.Error(w, "unable to retrieve file from form:"+err.Error(), http.StatusBadRequest) |
| 70 | + return |
| 71 | + } |
| 72 | + defer file.Close() |
| 73 | + |
| 74 | + fmt.Printf("Uploaded File: %+v\n", fileHeader.Filename) |
| 75 | + fmt.Printf("File Size: %+v\n", fileHeader.Size) |
| 76 | + fmt.Printf("MIME Header: %+v\n", fileHeader.Header) |
| 77 | + |
| 78 | + // Define the destination path |
| 79 | + uploadDir := "./uploads" |
| 80 | + if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil { |
| 81 | + http.Error(w, "failed to create upload directory: "+err.Error(), http.StatusInternalServerError) |
| 82 | + return |
| 83 | + } |
| 84 | + destPath := filepath.Join(uploadDir, fileHeader.Filename) |
| 85 | + |
| 86 | + // Create the destination file |
| 87 | + destFile, err := os.Create(destPath) |
| 88 | + if err != nil { |
| 89 | + http.Error(w, "failed to create destination file: "+err.Error(), http.StatusInternalServerError) |
| 90 | + return |
| 91 | + } |
| 92 | + defer destFile.Close() |
| 93 | + |
| 94 | + // Copy the file's content to the destination file |
| 95 | + if _, err := io.Copy(destFile, file); err != nil { |
| 96 | + http.Error(w, "Failed to save file:"+err.Error(), http.StatusInternalServerError) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // Respond with a success message |
| 101 | + w.WriteHeader(http.StatusOK) |
| 102 | + fmt.Fprintf(w, "File uploaded successfully: %s\n", fileHeader.Filename) |
| 103 | +} |
0 commit comments