fix file rename
This commit is contained in:
@@ -127,21 +127,35 @@ export class FileManager {
|
|||||||
/**
|
/**
|
||||||
* Renames a file on the server.
|
* Renames a file on the server.
|
||||||
* @async
|
* @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
|
* @param {string} newName - The new name for the file
|
||||||
* @returns {Promise<Object>} A promise that resolves with the result of the rename operation
|
* @returns {Promise<Object>} A promise that resolves with the result of the rename operation
|
||||||
* @throws {Error} If the rename operation fails
|
* @throws {Error} If the rename operation fails
|
||||||
*/
|
*/
|
||||||
async renameFile(fileId, newName) {
|
async renameFile(filename, newName) {
|
||||||
console.log('[FileManager] Renaming file:', { fileId, 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 {
|
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',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
old_name: filename,
|
||||||
new_name: newName,
|
new_name: newName,
|
||||||
path: this.fileToRenamePath
|
path: this.fileToRenamePath
|
||||||
})
|
})
|
||||||
@@ -156,7 +170,8 @@ export class FileManager {
|
|||||||
console.log('[FileManager] Rename result:', result);
|
console.log('[FileManager] Rename result:', result);
|
||||||
|
|
||||||
if (result.success) {
|
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) {
|
if (fileIndex !== -1) {
|
||||||
this.currentFiles[fileIndex].name = newName;
|
this.currentFiles[fileIndex].name = newName;
|
||||||
await this.roomManager.viewManager.renderFiles(this.currentFiles);
|
await this.roomManager.viewManager.renderFiles(this.currentFiles);
|
||||||
|
|||||||
Reference in New Issue
Block a user