diff --git a/static/js/rooms/fileManager.js b/static/js/rooms/fileManager.js index e46d469..297b872 100644 --- a/static/js/rooms/fileManager.js +++ b/static/js/rooms/fileManager.js @@ -127,21 +127,35 @@ export class FileManager { /** * Renames a file on the server. * @async - * @param {string} fileId - The ID of the file to rename + * @param {string} filename - The name of the file to rename * @param {string} newName - The new name for the file * @returns {Promise} A promise that resolves with the result of the rename operation * @throws {Error} If the rename operation fails */ - async renameFile(fileId, newName) { - console.log('[FileManager] Renaming file:', { fileId, newName }); + async renameFile(filename, newName) { + console.log('[FileManager] Renaming file:', { filename, newName }); + + // Check if the file has an extension + const hasExtension = filename.includes('.'); + if (hasExtension) { + const oldExt = filename.substring(filename.lastIndexOf('.')); + const newExt = newName.includes('.') ? newName.substring(newName.lastIndexOf('.')) : ''; + + // If the new name doesn't have the same extension, append the old extension + if (newExt !== oldExt) { + newName = newName + oldExt; + } + } + try { - const response = await fetch(`/api/rooms/${this.roomManager.roomId}/files/${fileId}/rename`, { + const response = await fetch(`/api/rooms/${this.roomManager.roomId}/rename`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }, body: JSON.stringify({ + old_name: filename, new_name: newName, path: this.fileToRenamePath }) @@ -156,7 +170,8 @@ export class FileManager { console.log('[FileManager] Rename result:', result); if (result.success) { - const fileIndex = this.currentFiles.findIndex(file => file.id === fileId); + // Update the file's name in currentFiles + const fileIndex = this.currentFiles.findIndex(file => file.name === filename && file.path === this.fileToRenamePath); if (fileIndex !== -1) { this.currentFiles[fileIndex].name = newName; await this.roomManager.viewManager.renderFiles(this.currentFiles);