commit 5b02b2e98a7e85e0cd9031feda1389187bd5bc8f
parent db6f2bba9276b0bcf46804747ca6ad52e19db826
Author: breadcat <breadcat@users.noreply.github.com>
Date: Mon, 23 Feb 2026 14:50:10 +0000
On upload conflict, redirect to existing file
Diffstat:
2 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/include-files.go b/include-files.go
@@ -143,12 +143,22 @@ func fileRenameHandler(w http.ResponseWriter, r *http.Request, parts []string) {
http.Redirect(w, r, "/file/"+fileID, http.StatusSeeOther)
}
-func checkFileConflictStrict(filename string) (string, string, error) {
+func checkFileConflictStrict(filename string) (string, string, int64, error) {
finalPath := filepath.Join(config.UploadDir, filename)
if _, err := os.Stat(finalPath); err == nil {
- return "", "", fmt.Errorf("a file with that name already exists")
+ existingID, dbErr := getFileIDByName(filename)
+ if dbErr != nil {
+ return "", "", 0, fmt.Errorf("a file with that name already exists")
+ }
+ return "", "", existingID, nil
} else if !os.IsNotExist(err) {
- return "", "", fmt.Errorf("failed to check for existing file: %v", err)
+ return "", "", 0, fmt.Errorf("failed to check for existing file: %v", err)
}
- return filename, finalPath, nil
+ return filename, finalPath, 0, nil
+}
+
+func getFileIDByName(filename string) (int64, error) {
+ var id int64
+ err := db.QueryRow("SELECT id FROM files WHERE filename = ?", filename).Scan(&id)
+ return id, err
}
\ No newline at end of file
diff --git a/include-uploads.go b/include-uploads.go
@@ -72,10 +72,13 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
}
func processUpload(src io.Reader, filename string) (int64, string, error) {
- finalFilename, finalPath, err := checkFileConflictStrict(filename)
+ finalFilename, finalPath, conflictID, err := checkFileConflictStrict(filename)
if err != nil {
return 0, "", err
}
+ if conflictID != 0 {
+ return conflictID, "", nil
+ }
tempPath := finalPath + ".tmp"
tempFile, err := os.Create(tempPath)
@@ -196,11 +199,15 @@ func ytdlpHandler(w http.ResponseWriter, r *http.Request) {
expectedFullPath := strings.TrimSpace(string(filenameBytes))
expectedFilename := filepath.Base(expectedFullPath)
- finalFilename, finalPath, err := checkFileConflictStrict(expectedFilename)
+ finalFilename, finalPath, conflictID, err := checkFileConflictStrict(expectedFilename)
if err != nil {
renderError(w, err.Error(), http.StatusConflict)
return
}
+ if conflictID != 0 {
+ redirectWithWarning(w, r, fmt.Sprintf("/file/%d", conflictID), "")
+ return
+ }
downloadCmd := exec.Command("yt-dlp", "--playlist-items", "1", "-f", "mp4", "-o", outTemplate, videoURL)
downloadCmd.Stdout = os.Stdout