commit d6448e89ab4134495409586768b4db411252f769
parent ff6671be2053de92d58339a9773b8b203ead82fa
Author: breadcat <breadcat@users.noreply.github.com>
Date: Mon, 22 Sep 2025 19:47:27 +0100
Check for conflicts on file upload
Diffstat:
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/main.go b/main.go
@@ -392,8 +392,20 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()
- // Save uploaded file to a temporary path first
- tmpPath := filepath.Join(config.UploadDir, header.Filename+".tmp")
+ finalFilename := header.Filename
+ dstPath := filepath.Join(config.UploadDir, finalFilename)
+
+ // Check before creating anything
+ if _, err := os.Stat(dstPath); err == nil {
+ http.Error(w, "A file with that name already exists", http.StatusConflict)
+ return
+ } else if !os.IsNotExist(err) {
+ http.Error(w, "Failed to check for existing file", http.StatusInternalServerError)
+ return
+ }
+
+ // Save uploaded file to a temporary path
+ tmpPath := dstPath + ".tmp"
tmpFile, err := os.Create(tmpPath)
if err != nil {
http.Error(w, "Failed to save uploaded file", http.StatusInternalServerError)
@@ -407,8 +419,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
- dstPath := filepath.Join(config.UploadDir, header.Filename)
-
// --- Detect video codec using ffprobe ---
ffprobeCmd := exec.Command("ffprobe", "-v", "error", "-select_streams", "v:0",
"-show_entries", "stream=codec_name", "-of", "default=nokey=1:noprint_wrappers=1", tmpPath)