commit f32568d1a2384824c64910a7bc3565c9be9a0432
parent eaec34402ea1f954bfda4df67166ba67f40eb6ca
Author: breadcat <breadcat@users.noreply.github.com>
Date: Tue, 7 Oct 2025 16:49:53 +0100
Allow upload of multiple files
Diffstat:
2 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/main.go b/main.go
@@ -70,8 +70,6 @@ type Pagination struct {
PerPage int
}
-package main
-
func getOrCreateCategoryAndTag(category, value string) (int, int, error) {
var catID int
err := db.QueryRow("SELECT id FROM categories WHERE name=?", category).Scan(&catID)
@@ -564,20 +562,47 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
- file, header, err := r.FormFile("file")
+ // Parse the multipart form (with max memory limit, e.g., 32MB)
+ err := r.ParseMultipartForm(32 << 20)
if err != nil {
- renderError(w, "Failed to read uploaded file", http.StatusBadRequest)
+ renderError(w, "Failed to parse form", http.StatusBadRequest)
return
}
- defer file.Close()
- id, warningMsg, err := processUpload(file, header.Filename)
- if err != nil {
- renderError(w, err.Error(), http.StatusInternalServerError)
+ files := r.MultipartForm.File["file"]
+ if len(files) == 0 {
+ renderError(w, "No files uploaded", http.StatusBadRequest)
return
}
- redirectWithWarning(w, r, fmt.Sprintf("/file/%d", id), warningMsg)
+ var warnings []string
+
+ // Process each file
+ for _, fileHeader := range files {
+ file, err := fileHeader.Open()
+ if err != nil {
+ renderError(w, "Failed to open uploaded file", http.StatusInternalServerError)
+ return
+ }
+ defer file.Close()
+
+ _, warningMsg, err := processUpload(file, fileHeader.Filename)
+ if err != nil {
+ renderError(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ if warningMsg != "" {
+ warnings = append(warnings, warningMsg)
+ }
+ }
+
+ var warningMsg string
+ if len(warnings) > 0 {
+ warningMsg = strings.Join(warnings, "; ")
+ }
+
+ redirectWithWarning(w, r, "/untagged", warningMsg)
}
func redirectWithWarning(w http.ResponseWriter, r *http.Request, baseURL, warningMsg string) {
diff --git a/templates/add.html b/templates/add.html
@@ -1,7 +1,7 @@
{{template "_header" .}}
-<h2>Upload File</h2>
+<h2>Upload File(s)</h2>
<form method="post" enctype="multipart/form-data">
- <input type="file" name="file">
+ <input type="file" name="file" multiple>
<button type="submit">Upload</button>
</form>