From: PhiTux Date: Wed, 10 Sep 2025 13:00:09 +0000 (+0200) Subject: add rename file functionality X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=4c219ebd8dfd71aa8d0b2d22dfdebe248f733028;p=DailyTxT.git add rename file functionality --- diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index cacd90f..b6e72db 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go @@ -1004,3 +1004,134 @@ func DeleteDay(w http.ResponseWriter, r *http.Request) { utils.JSONResponse(w, http.StatusOK, map[string]bool{"success": true}) } + +// RenameFileRequest represents the rename file request body +type RenameFileRequest struct { + UUID string `json:"uuid"` + NewFilename string `json:"new_filename"` + Day int `json:"day"` + Month int `json:"month"` + Year int `json:"year"` +} + +// RenameFile handles renaming a file +func RenameFile(w http.ResponseWriter, r *http.Request) { + // Get user ID from context + userID, ok := r.Context().Value(utils.UserIDKey).(int) + if !ok { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + derivedKey, ok := r.Context().Value(utils.DerivedKeyKey).(string) + if !ok { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + // Parse request body + var req RenameFileRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "Invalid request body", http.StatusBadRequest) + return + } + + // Validate input + req.NewFilename = strings.TrimSpace(req.NewFilename) + if req.NewFilename == "" { + utils.JSONResponse(w, http.StatusBadRequest, map[string]any{ + "success": false, + "message": "New filename cannot be empty", + }) + return + } + + if req.UUID == "" { + utils.JSONResponse(w, http.StatusBadRequest, map[string]any{ + "success": false, + "message": "File UUID is required", + }) + return + } + + // Get month data + content, err := utils.GetMonth(userID, req.Year, req.Month) + if err != nil { + http.Error(w, fmt.Sprintf("Error retrieving month data: %v", err), http.StatusInternalServerError) + return + } + + encKey, err := utils.GetEncryptionKey(userID, derivedKey) + if err != nil { + http.Error(w, fmt.Sprintf("Error getting encryption key: %v", err), http.StatusInternalServerError) + return + } + + enc_filename, err := utils.EncryptText(req.NewFilename, encKey) + if err != nil { + http.Error(w, fmt.Sprintf("Error encrypting text: %v", err), http.StatusInternalServerError) + return + } + + // Find and update the file + days, ok := content["days"].([]any) + if !ok { + utils.JSONResponse(w, http.StatusNotFound, map[string]any{ + "success": false, + "message": "No days found", + }) + return + } + + found := false + for _, d := range days { + day, ok := d.(map[string]any) + if !ok { + continue + } + + dayNum, ok := day["day"].(float64) + if !ok || int(dayNum) != req.Day { + continue + } + + files, ok := day["files"].([]any) + if !ok { + continue + } + + // Find and rename the specific file + for _, f := range files { + file, ok := f.(map[string]any) + if !ok { + continue + } + + if uuid, ok := file["uuid_filename"].(string); ok && uuid == req.UUID { + file["enc_filename"] = enc_filename + found = true + break + } + } + + if found { + break + } + } + + if !found { + utils.JSONResponse(w, http.StatusNotFound, map[string]any{ + "success": false, + "message": "File not found", + }) + return + } + + // Save the updated month data + if err := utils.WriteMonth(userID, req.Year, req.Month, content); err != nil { + http.Error(w, fmt.Sprintf("Error writing month data: %v", err), http.StatusInternalServerError) + return + } + + utils.Logger.Printf("File renamed successfully for user %d: %s -> %s", userID, req.UUID, req.NewFilename) + utils.JSONResponse(w, http.StatusOK, map[string]bool{"success": true}) +} diff --git a/backend/main.go b/backend/main.go index 46ba822..caabbef 100644 --- a/backend/main.go +++ b/backend/main.go @@ -86,6 +86,7 @@ func main() { mux.HandleFunc("POST /logs/uploadFile", middleware.RequireAuth(handlers.UploadFile)) mux.HandleFunc("GET /logs/downloadFile", middleware.RequireAuth(handlers.DownloadFile)) mux.HandleFunc("GET /logs/deleteFile", middleware.RequireAuth(handlers.DeleteFile)) + mux.HandleFunc("POST /logs/renameFile", middleware.RequireAuth(handlers.RenameFile)) mux.HandleFunc("GET /logs/getHistory", middleware.RequireAuth(handlers.GetHistory)) mux.HandleFunc("GET /logs/bookmarkDay", middleware.RequireAuth(handlers.BookmarkDay)) mux.HandleFunc("GET /logs/deleteDay", middleware.RequireAuth(handlers.DeleteDay)) diff --git a/frontend/src/lib/FileList.svelte b/frontend/src/lib/FileList.svelte index 5503a52..da709c7 100644 --- a/frontend/src/lib/FileList.svelte +++ b/frontend/src/lib/FileList.svelte @@ -1,13 +1,17 @@ {#each files as file (file.uuid_filename)} @@ -49,12 +53,104 @@ {#if deleteAllowed} { + if (openOptionsMenu === file.uuid_filename) { + openOptionsMenu = null; + editingFilename = null; + } else { + openOptionsMenu = file.uuid_filename; + editingFilename = null; + } + }} > + + {/if} + + {#if deleteAllowed && openOptionsMenu === file.uuid_filename} +
+
+
+ + +
+ {#if editingFilename === file.uuid_filename} + { + if (e.key === 'Enter') { + if (renameFile) { + renameFile(file.uuid_filename, newFilename); + } + editingFilename = null; + openOptionsMenu = null; + } else if (e.key === 'Escape') { + editingFilename = null; + } + }} + /> + + + {:else} + + + {/if} +
+
+ +
+ +
+ +
+
+
+ {/if} {/each} diff --git a/frontend/src/routes/(authed)/+layout.svelte b/frontend/src/routes/(authed)/+layout.svelte index f873263..ad8c698 100644 --- a/frontend/src/routes/(authed)/+layout.svelte +++ b/frontend/src/routes/(authed)/+layout.svelte @@ -1442,44 +1442,49 @@ {/if}
{#each $tags as tag} - - {#if deleteTagId === tag.id} -
@@ -2233,6 +2238,10 @@