taggart

Simple golang tagging filesystem webapp
Log | Files | Refs

commit 74d5e723f9a0da2fd5021fe5a158204a227032b3
parent 6f5cdb6aa8ba81005a1105d11218ac9ac8f657d8
Author: breadcat <breadcat@users.noreply.github.com>
Date:   Sun, 21 Sep 2025 09:25:03 +0100

Allow saving with custom filename

Diffstat:
Mmain.go | 32+++++++++++++++++++++++++-------
Mtemplates/upload.html | 1+
2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/main.go b/main.go @@ -113,6 +113,8 @@ func uploadFromURLHandler(w http.ResponseWriter, r *http.Request) { return } + customFilename := strings.TrimSpace(r.FormValue("filename")) + // Validate URL parsedURL, err := url.ParseRequestURI(fileURL) if err != nil || !(parsedURL.Scheme == "http" || parsedURL.Scheme == "https") { @@ -128,9 +130,23 @@ func uploadFromURLHandler(w http.ResponseWriter, r *http.Request) { } defer resp.Body.Close() - // Determine filename from URL - parts := strings.Split(parsedURL.Path, "/") - filename := parts[len(parts)-1] + // Determine filename + var filename string + if customFilename != "" { + filename = customFilename + } else { + // Use basename from URL as before + parts := strings.Split(parsedURL.Path, "/") + filename = parts[len(parts)-1] + if filename == "" { + filename = "file_from_url" + } + } + + // Sanitize filename (remove potentially dangerous characters) + filename = strings.ReplaceAll(filename, "/", "_") + filename = strings.ReplaceAll(filename, "\\", "_") + filename = strings.ReplaceAll(filename, "..", "_") if filename == "" { filename = "file_from_url" } @@ -138,13 +154,15 @@ func uploadFromURLHandler(w http.ResponseWriter, r *http.Request) { dstPath := filepath.Join("uploads", filename) // Avoid overwriting existing files + originalFilename := filename for i := 1; ; i++ { if _, err := os.Stat(dstPath); os.IsNotExist(err) { break } - ext := filepath.Ext(filename) - name := strings.TrimSuffix(filename, ext) - dstPath = filepath.Join("uploads", fmt.Sprintf("%s_%d%s", name, i, ext)) + ext := filepath.Ext(originalFilename) + name := strings.TrimSuffix(originalFilename, ext) + filename = fmt.Sprintf("%s_%d%s", name, i, ext) + dstPath = filepath.Join("uploads", filename) } dst, err := os.Create(dstPath) @@ -161,7 +179,7 @@ func uploadFromURLHandler(w http.ResponseWriter, r *http.Request) { } // Add to database - res, err := db.Exec("INSERT INTO files (filename, path) VALUES (?, ?)", filepath.Base(dstPath), dstPath) + res, err := db.Exec("INSERT INTO files (filename, path) VALUES (?, ?)", filename, dstPath) if err != nil { http.Error(w, "Failed to record file", http.StatusInternalServerError) return diff --git a/templates/upload.html b/templates/upload.html @@ -8,6 +8,7 @@ <h2>Upload from URL</h2> <form method="post" action="/upload-url"> File URL: <input type="url" name="fileurl" required style="width:400px"><br> + Custom filename: <input type="text" name="filename" placeholder="Optional: my-image.jpg" style="width:300px"><br> <button type="submit">Add File</button> </form>