2025-03-19 23:28:29 +01:00
/**
* OxiCloud - File Operations Module
* This file handles file and folder operations (create, move, delete, rename, upload)
*/
2026-02-08 22:44:42 +01:00
/**
* Get authorization headers for API requests
* @returns {Object} Headers object with Authorization bearer token
*/
function getAuthHeaders () {
const token = localStorage . getItem ( 'oxicloud_token' );
const headers = {};
if ( token ) {
headers [ 'Authorization' ] = `Bearer ${ token } ` ;
}
return headers ;
}
2025-03-19 23:28:29 +01:00
// File Operations Module
const fileOps = {
2026-02-13 22:08:36 +01:00
// ========================================================================
2026-02-14 10:46:23 +01:00
// Upload progress — notification bell integration
2026-02-13 22:08:36 +01:00
// ========================================================================
2026-02-14 10:46:23 +01:00
_currentBatchId : null ,
2026-02-13 22:08:36 +01:00
2026-02-14 10:46:23 +01:00
/** Start a new upload batch in the notification bell */
2026-02-13 22:08:36 +01:00
_initUploadToast ( totalFiles ) {
2026-02-14 10:46:23 +01:00
this . _currentBatchId = window . notifications
? window . notifications . addUploadBatch ( totalFiles )
: null ;
2026-02-13 22:08:36 +01:00
},
2026-02-14 10:46:23 +01:00
/** Finalise the batch in the notification bell */
2026-02-13 22:08:36 +01:00
_finishUploadToast ( successCount , totalFiles ) {
2026-02-14 10:46:23 +01:00
if ( window . notifications && this . _currentBatchId ) {
window . notifications . finishBatch ( this . _currentBatchId , successCount , totalFiles );
}
2026-02-13 22:08:36 +01:00
},
2025-03-19 23:28:29 +01:00
/**
2026-02-13 22:08:36 +01:00
* Upload a single file via XMLHttpRequest with progress events.
2026-02-14 10:46:23 +01:00
* Progress is reported to the notification bell via batchId + fileName.
* Returns a promise that resolves with { ok, data?, errorMsg?, isQuotaError? }.
2026-02-13 22:08:36 +01:00
*/
2026-02-14 10:46:23 +01:00
_uploadFileXHR ( formData , batchId , fileName ) {
2026-02-13 22:08:36 +01:00
return new Promise (( resolve ) => {
const xhr = new XMLHttpRequest ();
2026-02-14 10:46:23 +01:00
const notif = window . notifications ;
2026-02-13 22:08:36 +01:00
xhr . upload . addEventListener ( 'progress' , ( e ) => {
2026-02-14 10:46:23 +01:00
if ( e . lengthComputable && notif && batchId ) {
2026-02-13 22:08:36 +01:00
const pct = Math . round (( e . loaded / e . total ) * 100 );
2026-02-14 10:46:23 +01:00
notif . updateFile ( batchId , fileName , pct , 'uploading' );
2026-02-13 22:08:36 +01:00
}
});
xhr . addEventListener ( 'load' , () => {
if ( xhr . status >= 200 && xhr . status < 300 ) {
2026-02-14 10:46:23 +01:00
if ( notif && batchId ) notif . updateFile ( batchId , fileName , 100 , 'done' );
2026-02-13 22:08:36 +01:00
let data = null ;
try { data = JSON . parse ( xhr . responseText ); } catch ( _ ) {}
resolve ({ ok : true , data });
} else {
2026-02-14 10:46:23 +01:00
if ( notif && batchId ) notif . updateFile ( batchId , fileName , 0 , 'error' );
2026-02-14 10:34:07 +01:00
// Parse error body for quota-exceeded or other messages
let errorMsg = null ;
let isQuotaError = false ;
try {
const errBody = JSON . parse ( xhr . responseText );
errorMsg = errBody . error || null ;
isQuotaError = errBody . error_type === 'QuotaExceeded' || xhr . status === 507 ;
} catch ( _ ) {}
resolve ({ ok : false , errorMsg , isQuotaError });
2026-02-13 22:08:36 +01:00
}
});
xhr . addEventListener ( 'error' , () => {
2026-02-14 10:46:23 +01:00
if ( notif && batchId ) notif . updateFile ( batchId , fileName , 0 , 'error' );
2026-02-13 22:08:36 +01:00
resolve ({ ok : false });
});
xhr . open ( 'POST' , '/api/files/upload' );
// Set auth header
const token = localStorage . getItem ( 'oxicloud_token' );
if ( token ) xhr . setRequestHeader ( 'Authorization' , `Bearer ${ token } ` );
xhr . setRequestHeader ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' );
xhr . send ( formData );
});
},
// ========================================================================
// Upload files (via button or drag-and-drop)
// ========================================================================
/**
* Upload files to the server with real-time progress indication
2025-03-19 23:28:29 +01:00
* @param {FileList} files - Files to upload
*/
async uploadFiles ( files ) {
2026-02-13 22:08:36 +01:00
const totalFiles = files . length ;
if ( totalFiles === 0 ) return ;
// Legacy progress bar (inside dropzone) — keep working for drag-drop
2025-03-19 23:28:29 +01:00
const progressBar = document . querySelector ( '.progress-fill' );
const uploadProgressDiv = document . querySelector ( '.upload-progress' );
2026-02-13 22:08:36 +01:00
if ( uploadProgressDiv ) { uploadProgressDiv . style . display = 'block' ; }
if ( progressBar ) { progressBar . style . width = '0%' ; }
2026-02-14 10:46:23 +01:00
// Show upload notification
2026-02-13 22:08:36 +01:00
this . _initUploadToast ( totalFiles );
2026-02-14 10:46:23 +01:00
const batchId = this . _currentBatchId ;
2025-03-19 23:28:29 +01:00
let uploadedCount = 0 ;
2026-02-13 22:08:36 +01:00
let successCount = 0 ;
2025-03-19 23:28:29 +01:00
for ( let i = 0 ; i < totalFiles ; i ++ ) {
const file = files [ i ];
const formData = new FormData ();
2026-02-13 22:08:36 +01:00
2026-02-03 17:59:04 +01:00
const targetFolderId = window . app . currentPath || window . app . userHomeFolderId ;
2026-02-13 22:08:36 +01:00
if ( targetFolderId ) formData . append ( 'folder_id' , targetFolderId );
2026-02-03 17:59:04 +01:00
formData . append ( 'file' , file );
2025-03-19 23:28:29 +01:00
2026-02-13 22:08:36 +01:00
console . log ( `Uploading file to folder: ${ targetFolderId || 'root' } ` , {
file : file . name , size : file . size
});
2025-03-19 23:28:29 +01:00
2026-02-14 10:46:23 +01:00
const result = await this . _uploadFileXHR ( formData , batchId , file . name );
2026-02-13 22:08:36 +01:00
uploadedCount ++ ;
// Legacy dropzone bar
if ( progressBar ) {
progressBar . style . width = (( uploadedCount / totalFiles ) * 100 ) + '%' ;
}
2026-02-14 10:46:23 +01:00
// Notify bell of per-file completion
if ( window . notifications && batchId ) {
window . notifications . fileCompleted ( batchId , result . ok );
}
2026-02-13 22:08:36 +01:00
if ( result . ok ) {
successCount ++ ;
console . log ( `Successfully uploaded ${ file . name } ` , result . data );
} else {
console . error ( `Upload error for ${ file . name } ` );
2026-02-14 10:34:07 +01:00
if ( result . isQuotaError ) {
const msg = result . errorMsg || window . i18n ? . t ( 'storage_quota_exceeded' ) || 'Storage quota exceeded' ;
2026-02-14 10:46:23 +01:00
if ( window . notifications ) {
window . notifications . addNotification ({
icon : 'fa-exclamation-triangle' ,
iconClass : 'error' ,
title : file . name ,
text : msg
});
}
2026-02-14 10:34:07 +01:00
break ;
}
2025-03-19 23:28:29 +01:00
}
}
2026-02-13 22:08:36 +01:00
// All done
this . _finishUploadToast ( successCount , totalFiles );
// Wait for backend to persist, then reload
await new Promise ( resolve => setTimeout ( resolve , 800 ));
2026-02-14 10:34:07 +01:00
// Refresh storage usage display
if ( typeof window . refreshUserData === 'function' ) {
try { await window . refreshUserData (); } catch ( _ ) {}
}
2026-02-13 22:08:36 +01:00
try {
await window . loadFiles ({ forceRefresh : true });
} catch ( reloadError ) {
console . error ( 'Error reloading files:' , reloadError );
}
setTimeout (() => {
const dropzone = document . getElementById ( 'dropzone' );
if ( dropzone ) dropzone . style . display = 'none' ;
if ( uploadProgressDiv ) uploadProgressDiv . style . display = 'none' ;
}, 500 );
2025-03-19 23:28:29 +01:00
},
2026-02-08 22:44:42 +01:00
/**
* Upload folder files maintaining directory structure
* Creates subfolders as needed, then uploads files into them
* @param {FileList} files - Files from folder input (with webkitRelativePath)
*/
async uploadFolderFiles ( files ) {
if ( ! files || files . length === 0 ) return ;
const progressBar = document . querySelector ( '.progress-fill' );
const uploadProgressDiv = document . querySelector ( '.upload-progress' );
2026-02-13 22:08:36 +01:00
if ( uploadProgressDiv ) { uploadProgressDiv . style . display = 'block' ; }
if ( progressBar ) { progressBar . style . width = '0%' ; }
2026-02-08 22:44:42 +01:00
const currentFolderId = window . app . currentPath || window . app . userHomeFolderId ;
// Build folder structure from relative paths
2026-02-13 22:08:36 +01:00
const folderMap = new Map ();
folderMap . set ( '' , currentFolderId );
2026-02-08 22:44:42 +01:00
const folderPaths = new Set ();
for ( const file of files ) {
const parts = file . webkitRelativePath . split ( '/' );
for ( let i = 1 ; i < parts . length ; i ++ ) {
const path = parts . slice ( 0 , i ). join ( '/' );
folderPaths . add ( path );
}
}
const sortedPaths = [... folderPaths ]. sort (( a , b ) =>
a . split ( '/' ). length - b . split ( '/' ). length
);
2026-02-13 22:08:36 +01:00
// Create folders first (no progress toast for folder creation)
2026-02-08 22:44:42 +01:00
for ( const folderPath of sortedPaths ) {
const parts = folderPath . split ( '/' );
const folderName = parts [ parts . length - 1 ];
const parentPath = parts . slice ( 0 , - 1 ). join ( '/' );
const parentId = folderMap . get ( parentPath ) || currentFolderId ;
try {
const response = await fetch ( '/api/folders' , {
method : 'POST' ,
headers : {
... getAuthHeaders (),
'Content-Type' : 'application/json' ,
'Cache-Control' : 'no-cache, no-store, must-revalidate'
},
body : JSON . stringify ({
name : folderName ,
parent_id : parentId
})
});
if ( response . ok ) {
const folder = await response . json ();
folderMap . set ( folderPath , folder . id );
console . log ( `Created folder: ${ folderPath } -> ${ folder . id } ` );
} else {
console . error ( `Error creating folder ${ folderPath } :` , await response . text ());
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , `Error creating folder: ${ folderName } ` );
2026-02-08 22:44:42 +01:00
}
} catch ( error ) {
console . error ( `Network error creating folder ${ folderPath } :` , error );
}
}
2026-02-14 10:46:23 +01:00
// Upload files with notification bell
2026-02-08 22:44:42 +01:00
const totalFiles = files . length ;
2026-02-13 22:08:36 +01:00
this . _initUploadToast ( totalFiles );
2026-02-14 10:46:23 +01:00
const batchId = this . _currentBatchId ;
2026-02-13 22:08:36 +01:00
let uploadedCount = 0 ;
let successCount = 0 ;
2026-02-08 22:44:42 +01:00
for ( let i = 0 ; i < totalFiles ; i ++ ) {
const file = files [ i ];
const parts = file . webkitRelativePath . split ( '/' );
const parentPath = parts . slice ( 0 , - 1 ). join ( '/' );
const targetFolderId = folderMap . get ( parentPath ) || currentFolderId ;
const formData = new FormData ();
formData . append ( 'folder_id' , targetFolderId );
formData . append ( 'file' , file );
2026-02-13 22:08:36 +01:00
const displayName = file . webkitRelativePath || file . name ;
2026-02-14 10:46:23 +01:00
const result = await this . _uploadFileXHR ( formData , batchId , displayName );
2026-02-08 22:44:42 +01:00
2026-02-13 22:08:36 +01:00
uploadedCount ++ ;
if ( progressBar ) {
progressBar . style . width = (( uploadedCount / totalFiles ) * 100 ) + '%' ;
}
2026-02-14 10:46:23 +01:00
if ( window . notifications && batchId ) {
window . notifications . fileCompleted ( batchId , result . ok );
}
2026-02-08 22:44:42 +01:00
2026-02-13 22:08:36 +01:00
if ( result . ok ) {
successCount ++ ;
console . log ( `Uploaded: ${ file . webkitRelativePath } ` );
} else {
console . error ( `Error uploading ${ file . webkitRelativePath } ` );
2026-02-14 10:34:07 +01:00
if ( result . isQuotaError ) {
const msg = result . errorMsg || window . i18n ? . t ( 'storage_quota_exceeded' ) || 'Storage quota exceeded' ;
2026-02-14 10:46:23 +01:00
if ( window . notifications ) {
window . notifications . addNotification ({
icon : 'fa-exclamation-triangle' ,
iconClass : 'error' ,
title : file . name ,
text : msg
});
}
2026-02-14 10:34:07 +01:00
break ;
}
2026-02-08 22:44:42 +01:00
}
}
2026-02-13 22:08:36 +01:00
// Finish
this . _finishUploadToast ( successCount , totalFiles );
2026-02-08 22:44:42 +01:00
await new Promise ( resolve => setTimeout ( resolve , 800 ));
2026-02-14 10:34:07 +01:00
// Refresh storage usage display
if ( typeof window . refreshUserData === 'function' ) {
try { await window . refreshUserData (); } catch ( _ ) {}
}
2026-02-08 22:44:42 +01:00
try {
await window . loadFiles ({ forceRefresh : true });
} catch ( reloadError ) {
console . error ( 'Error reloading files:' , reloadError );
}
setTimeout (() => {
const dropzone = document . getElementById ( 'dropzone' );
if ( dropzone ) dropzone . style . display = 'none' ;
2026-02-13 22:08:36 +01:00
if ( uploadProgressDiv ) uploadProgressDiv . style . display = 'none' ;
2026-02-08 22:44:42 +01:00
}, 500 );
},
2025-03-19 23:28:29 +01:00
/**
* Create a new folder
* @param {string} name - Folder name
*/
async createFolder ( name ) {
try {
2025-04-01 21:14:09 +02:00
console . log ( 'Creating folder with name:' , name );
2026-02-12 09:41:25 +01:00
// Send the actual request to the backend to create the folder
2025-03-19 23:28:29 +01:00
const response = await fetch ( '/api/folders' , {
method : 'POST' ,
headers : {
2026-02-08 22:44:42 +01:00
... getAuthHeaders (),
2025-04-12 12:37:12 +02:00
'Content-Type' : 'application/json' ,
'Cache-Control' : 'no-cache, no-store, must-revalidate'
2025-03-19 23:28:29 +01:00
},
body : JSON . stringify ({
name : name ,
2026-02-14 12:45:46 +01:00
parent_id : window . app . currentPath || window . app . userHomeFolderId || null
2025-03-19 23:28:29 +01:00
})
});
if ( response . ok ) {
2026-02-12 09:41:25 +01:00
// Get the created folder from the backend
2025-04-12 12:37:12 +02:00
const folder = await response . json ();
console . log ( 'Folder created successfully:' , folder );
2026-02-12 09:41:25 +01:00
// Add the folder to the view immediately for instant feedback
2025-04-12 12:37:12 +02:00
window . ui . addFolderToView ( folder );
2026-02-12 09:41:25 +01:00
// Wait to allow the backend to save the changes
2025-04-12 12:37:12 +02:00
await new Promise ( resolve => setTimeout ( resolve , 1000 ));
2026-02-12 09:41:25 +01:00
// Reload files to refresh the view
2025-04-12 12:37:12 +02:00
await window . loadFiles ({ forceRefresh : true });
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Folder created' , `" ${ name } " created successfully` );
2025-03-19 23:28:29 +01:00
} else {
const errorData = await response . text ();
console . error ( 'Create folder error:' , errorData );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error creating the folder' );
2025-03-19 23:28:29 +01:00
}
} catch ( error ) {
console . error ( 'Error creating folder:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error creating the folder' );
2025-03-19 23:28:29 +01:00
}
},
/**
* Move a file to another folder
* @param {string} fileId - File ID
* @param {string} targetFolderId - Target folder ID
* @returns {Promise<boolean>} - Success status
*/
async moveFile ( fileId , targetFolderId ) {
try {
const response = await fetch ( `/api/files/ ${ fileId } /move` , {
method : 'PUT' ,
headers : {
2026-02-08 22:44:42 +01:00
... getAuthHeaders (),
2025-03-19 23:28:29 +01:00
'Content-Type' : 'application/json'
},
body : JSON . stringify ({
folder_id : targetFolderId === "" ? null : targetFolderId
})
});
if ( response . ok ) {
// Reload files after moving
await window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'File moved' , 'File moved successfully' );
2025-03-19 23:28:29 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
let errorMessage = 'Unknown error' ;
2025-03-19 23:28:29 +01:00
try {
const errorData = await response . json ();
2026-02-12 09:41:25 +01:00
errorMessage = errorData . error || 'Unknown error' ;
2025-03-19 23:28:29 +01:00
} catch ( e ) {
2026-02-12 09:41:25 +01:00
errorMessage = 'Error processing server response' ;
2025-03-19 23:28:29 +01:00
}
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , `Error moving the file: ${ errorMessage } ` );
2025-03-19 23:28:29 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error moving file:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error moving the file' );
2025-03-19 23:28:29 +01:00
return false ;
}
},
/**
* Move a folder to another folder
* @param {string} folderId - Folder ID
* @param {string} targetFolderId - Target folder ID
* @returns {Promise<boolean>} - Success status
*/
async moveFolder ( folderId , targetFolderId ) {
try {
const response = await fetch ( `/api/folders/ ${ folderId } /move` , {
method : 'PUT' ,
headers : {
2026-02-08 22:44:42 +01:00
... getAuthHeaders (),
2025-03-19 23:28:29 +01:00
'Content-Type' : 'application/json'
},
body : JSON . stringify ({
parent_id : targetFolderId === "" ? null : targetFolderId
})
});
if ( response . ok ) {
// Reload files after moving
await window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Folder moved' , 'Folder moved successfully' );
2025-03-19 23:28:29 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
let errorMessage = 'Unknown error' ;
2025-03-19 23:28:29 +01:00
try {
const errorData = await response . json ();
2026-02-12 09:41:25 +01:00
errorMessage = errorData . error || 'Unknown error' ;
2025-03-19 23:28:29 +01:00
} catch ( e ) {
2026-02-12 09:41:25 +01:00
errorMessage = 'Error processing server response' ;
2025-03-19 23:28:29 +01:00
}
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , `Error moving the folder: ${ errorMessage } ` );
2025-03-19 23:28:29 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error moving folder:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error moving the folder' );
2025-03-19 23:28:29 +01:00
return false ;
}
},
2026-02-08 22:44:42 +01:00
/**
* Rename a file
* @param {string} fileId - File ID
* @param {string} newName - New file name
* @returns {Promise<boolean>} - Success status
*/
async renameFile ( fileId , newName ) {
try {
console . log ( `Renaming file ${ fileId } to " ${ newName } "` );
const response = await fetch ( `/api/files/ ${ fileId } /rename` , {
method : 'PUT' ,
headers : {
... getAuthHeaders (),
'Content-Type' : 'application/json'
},
body : JSON . stringify ({ name : newName })
});
console . log ( 'Response status:' , response . status );
if ( response . ok ) {
window . ui . showNotification (
2026-02-12 09:41:25 +01:00
window . i18n ? window . i18n . t ( 'notifications.file_renamed' ) : 'File renamed' ,
window . i18n ? window . i18n . t ( 'notifications.file_renamed_to' , { name : newName }) : `File renamed to " ${ newName } "`
2026-02-08 22:44:42 +01:00
);
return true ;
} else {
const errorText = await response . text ();
console . error ( 'Error response:' , errorText );
2026-02-12 09:41:25 +01:00
let errorMessage = 'Unknown error' ;
2026-02-08 22:44:42 +01:00
try {
const errorData = JSON . parse ( errorText );
errorMessage = errorData . error || response . statusText ;
} catch ( e ) {
errorMessage = errorText || response . statusText ;
}
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , `Error renaming the file: ${ errorMessage } ` );
2026-02-08 22:44:42 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error renaming file:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error renaming the file' );
2026-02-08 22:44:42 +01:00
return false ;
}
},
2025-03-19 23:28:29 +01:00
/**
* Rename a folder
* @param {string} folderId - Folder ID
* @param {string} newName - New folder name
* @returns {Promise<boolean>} - Success status
*/
async renameFolder ( folderId , newName ) {
try {
console . log ( `Renaming folder ${ folderId } to " ${ newName } "` );
const response = await fetch ( `/api/folders/ ${ folderId } /rename` , {
method : 'PUT' ,
headers : {
2026-02-08 22:44:42 +01:00
... getAuthHeaders (),
2025-03-19 23:28:29 +01:00
'Content-Type' : 'application/json'
},
body : JSON . stringify ({ name : newName })
});
console . log ( 'Response status:' , response . status );
if ( response . ok ) {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Folder renamed' , `Folder renamed to " ${ newName } "` );
2025-03-19 23:28:29 +01:00
return true ;
} else {
const errorText = await response . text ();
console . error ( 'Error response:' , errorText );
2026-02-12 09:41:25 +01:00
let errorMessage = 'Unknown error' ;
2025-03-19 23:28:29 +01:00
try {
// Try to parse as JSON
const errorData = JSON . parse ( errorText );
errorMessage = errorData . error || response . statusText ;
} catch ( e ) {
// If not JSON, use text as is
errorMessage = errorText || response . statusText ;
}
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , `Error renaming the folder: ${ errorMessage } ` );
2025-03-19 23:28:29 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error renaming folder:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error renaming the folder' );
2025-03-19 23:28:29 +01:00
return false ;
}
},
/**
2025-03-24 17:49:53 +01:00
* Move a file to trash
2025-03-19 23:28:29 +01:00
* @param {string} fileId - File ID
2025-03-24 17:49:53 +01:00
* @param {string} fileName - File name
2025-03-19 23:28:29 +01:00
* @returns {Promise<boolean>} - Success status
*/
async deleteFile ( fileId , fileName ) {
2026-02-08 22:44:42 +01:00
const confirmed = await showConfirmDialog ({
2026-02-12 09:41:25 +01:00
title : window . i18n ? window . i18n . t ( 'dialogs.confirm_delete' ) : 'Move to trash' ,
message : window . i18n ? window . i18n . t ( 'dialogs.confirm_delete_file' , { name : fileName }) : `Are you sure you want to move the file " ${ fileName } " to trash?` ,
confirmText : window . i18n ? window . i18n . t ( 'actions.delete' ) : 'Delete' ,
2026-02-08 22:44:42 +01:00
});
if ( ! confirmed ) return false ;
2025-03-19 23:28:29 +01:00
try {
2025-03-24 17:49:53 +01:00
// Use the trash API endpoint
const response = await fetch ( `/api/trash/files/ ${ fileId } ` , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-19 23:28:29 +01:00
});
if ( response . ok ) {
window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'File moved to trash' , `" ${ fileName } " moved to trash` );
2025-03-19 23:28:29 +01:00
return true ;
} else {
2025-03-24 17:49:53 +01:00
// Fallback to direct deletion if trash fails
const fallbackResponse = await fetch ( `/api/files/ ${ fileId } ` , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-24 17:49:53 +01:00
});
if ( fallbackResponse . ok ) {
window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'File deleted' , `" ${ fileName } " deleted successfully` );
2025-03-24 17:49:53 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the file' );
2025-03-24 17:49:53 +01:00
return false ;
}
2025-03-19 23:28:29 +01:00
}
} catch ( error ) {
console . error ( 'Error deleting file:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the file' );
2025-03-19 23:28:29 +01:00
return false ;
}
},
/**
2025-03-24 17:49:53 +01:00
* Move a folder to trash
2025-03-19 23:28:29 +01:00
* @param {string} folderId - Folder ID
* @param {string} folderName - Folder name
* @returns {Promise<boolean>} - Success status
*/
async deleteFolder ( folderId , folderName ) {
2026-02-08 22:44:42 +01:00
const confirmed = await showConfirmDialog ({
2026-02-12 09:41:25 +01:00
title : window . i18n ? window . i18n . t ( 'dialogs.confirm_delete' ) : 'Move to trash' ,
message : window . i18n ? window . i18n . t ( 'dialogs.confirm_delete_folder' , { name : folderName }) : `Are you sure you want to move the folder " ${ folderName } " and all its contents to trash?` ,
confirmText : window . i18n ? window . i18n . t ( 'actions.delete' ) : 'Delete' ,
2026-02-08 22:44:42 +01:00
});
if ( ! confirmed ) return false ;
2025-03-19 23:28:29 +01:00
try {
2025-03-24 17:49:53 +01:00
// Use the trash API endpoint
const response = await fetch ( `/api/trash/folders/ ${ folderId } ` , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-19 23:28:29 +01:00
});
if ( response . ok ) {
// If we're inside the folder we just deleted, go back up
if ( window . app . currentPath === folderId ) {
window . app . currentPath = '' ;
window . ui . updateBreadcrumb ( '' );
}
window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Folder moved to trash' , `" ${ folderName } " moved to trash` );
2025-03-19 23:28:29 +01:00
return true ;
} else {
2025-03-24 17:49:53 +01:00
// Fallback to direct deletion if trash fails
const fallbackResponse = await fetch ( `/api/folders/ ${ folderId } ` , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-24 17:49:53 +01:00
});
if ( fallbackResponse . ok ) {
// If we're inside the folder we just deleted, go back up
if ( window . app . currentPath === folderId ) {
window . app . currentPath = '' ;
window . ui . updateBreadcrumb ( '' );
}
window . loadFiles ();
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Folder deleted' , `" ${ folderName } " deleted successfully` );
2025-03-24 17:49:53 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the folder' );
2025-03-24 17:49:53 +01:00
return false ;
}
2025-03-19 23:28:29 +01:00
}
} catch ( error ) {
console . error ( 'Error deleting folder:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the folder' );
2025-03-19 23:28:29 +01:00
return false ;
}
2025-03-24 17:49:53 +01:00
},
/**
2026-02-12 09:41:25 +01:00
* Get trash items
* @returns {Promise<Array>} - List of trash items
2025-03-24 17:49:53 +01:00
*/
async getTrashItems () {
try {
2026-02-08 22:44:42 +01:00
const response = await fetch ( '/api/trash' , {
headers : getAuthHeaders ()
});
2025-03-24 17:49:53 +01:00
if ( response . ok ) {
return await response . json ();
} else {
console . error ( 'Error fetching trash items:' , response . statusText );
return [];
}
} catch ( error ) {
console . error ( 'Error fetching trash items:' , error );
return [];
}
},
/**
2026-02-12 09:41:25 +01:00
* Restore an item from trash
* @param {string} trashId - Trash item ID
* @returns {Promise<boolean>} - Operation success
2025-03-24 17:49:53 +01:00
*/
async restoreFromTrash ( trashId ) {
try {
const response = await fetch ( `/api/trash/ ${ trashId } /restore` , {
method : 'POST' ,
headers : {
2026-02-08 22:44:42 +01:00
... getAuthHeaders (),
2025-03-24 17:49:53 +01:00
'Content-Type' : 'application/json'
},
body : JSON . stringify ({})
});
if ( response . ok ) {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Item restored' , 'Item restored successfully' );
2025-03-24 17:49:53 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error restoring the item' );
2025-03-24 17:49:53 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error restoring item from trash:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error restoring the item' );
2025-03-24 17:49:53 +01:00
return false ;
}
},
/**
2026-02-12 09:41:25 +01:00
* Permanently delete a trash item
* @param {string} trashId - Trash item ID
* @returns {Promise<boolean>} - Operation success
2025-03-24 17:49:53 +01:00
*/
async deletePermanently ( trashId ) {
2026-02-08 22:44:42 +01:00
const confirmed = await showConfirmDialog ({
2026-02-12 09:41:25 +01:00
title : window . i18n ? window . i18n . t ( 'dialogs.confirm_permanent_delete' ) : 'Delete permanently' ,
message : window . i18n ? window . i18n . t ( 'dialogs.confirm_permanent_delete_msg' ) : 'Are you sure you want to permanently delete this item? This action cannot be undone.' ,
confirmText : window . i18n ? window . i18n . t ( 'actions.delete_permanently' ) : 'Delete permanently' ,
2026-02-08 22:44:42 +01:00
});
if ( ! confirmed ) return false ;
2025-03-24 17:49:53 +01:00
try {
const response = await fetch ( `/api/trash/ ${ trashId } ` , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-24 17:49:53 +01:00
});
if ( response . ok ) {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Item deleted' , 'Item permanently deleted' );
2025-03-24 17:49:53 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the item' );
2025-03-24 17:49:53 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error deleting item permanently:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error deleting the item' );
2025-03-24 17:49:53 +01:00
return false ;
}
},
/**
2026-02-12 09:41:25 +01:00
* Empty the trash
* @returns {Promise<boolean>} - Operation success
2025-03-24 17:49:53 +01:00
*/
async emptyTrash () {
2026-02-08 22:44:42 +01:00
const confirmed = await showConfirmDialog ({
2026-02-12 09:41:25 +01:00
title : window . i18n ? window . i18n . t ( 'dialogs.confirm_empty_trash' ) : 'Empty trash' ,
message : window . i18n ? window . i18n . t ( 'trash.empty_confirm' ) : 'Are you sure you want to empty the trash? This action will permanently delete all items.' ,
confirmText : window . i18n ? window . i18n . t ( 'actions.empty_trash' ) : 'Empty trash' ,
2026-02-08 22:44:42 +01:00
});
if ( ! confirmed ) return false ;
2025-03-24 17:49:53 +01:00
try {
const response = await fetch ( '/api/trash/empty' , {
2026-02-08 22:44:42 +01:00
method : 'DELETE' ,
headers : getAuthHeaders ()
2025-03-24 17:49:53 +01:00
});
if ( response . ok ) {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Trash emptied' , 'The trash has been emptied successfully' );
2025-03-24 17:49:53 +01:00
return true ;
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error emptying the trash' );
2025-03-24 17:49:53 +01:00
return false ;
}
} catch ( error ) {
console . error ( 'Error emptying trash:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error emptying the trash' );
2025-03-24 17:49:53 +01:00
return false ;
}
2025-04-02 01:22:05 +02:00
},
/**
2026-02-12 09:41:25 +01:00
* Download a file
* @param {string} fileId - File ID
* @param {string} fileName - File name
2025-04-02 01:22:05 +02:00
*/
2026-02-08 22:44:42 +01:00
async downloadFile ( fileId , fileName ) {
try {
const response = await fetch ( `/api/files/ ${ fileId } ` , {
headers : getAuthHeaders ()
});
if ( response . ok ) {
const blob = await response . blob ();
const url = URL . createObjectURL ( blob );
const link = document . createElement ( 'a' );
link . href = url ;
link . download = fileName ;
document . body . appendChild ( link );
link . click ();
document . body . removeChild ( link );
URL . revokeObjectURL ( url );
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error downloading the file' );
2026-02-08 22:44:42 +01:00
}
} catch ( error ) {
console . error ( 'Error downloading file:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error downloading the file' );
2026-02-08 22:44:42 +01:00
}
2025-04-02 01:22:05 +02:00
},
/**
2026-02-12 09:41:25 +01:00
* Download a folder as ZIP
* @param {string} folderId - Folder ID
* @param {string} folderName - Folder name
2025-04-02 01:22:05 +02:00
*/
async downloadFolder ( folderId , folderName ) {
try {
// Show notification to user
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Preparing download' , 'Preparing the folder for download...' );
2025-04-02 01:22:05 +02:00
2026-02-08 22:44:42 +01:00
const response = await fetch ( `/api/folders/ ${ folderId } /download?format=zip` , {
headers : getAuthHeaders ()
});
if ( response . ok ) {
const blob = await response . blob ();
const url = URL . createObjectURL ( blob );
const link = document . createElement ( 'a' );
link . href = url ;
link . download = ` ${ folderName } .zip` ;
document . body . appendChild ( link );
link . click ();
document . body . removeChild ( link );
URL . revokeObjectURL ( url );
} else {
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error downloading the folder' );
2026-02-08 22:44:42 +01:00
}
2025-04-02 01:22:05 +02:00
} catch ( error ) {
console . error ( 'Error downloading folder:' , error );
2026-02-12 09:41:25 +01:00
window . ui . showNotification ( 'Error' , 'Error downloading the folder' );
2025-04-02 01:22:05 +02:00
}
2025-03-19 23:28:29 +01:00
}
};
// Expose file operations module globally
window . fileOps = fileOps ;