Add delete file from Filemanager #1384

This commit is contained in:
Diego Najar 2022-02-22 14:26:54 +01:00
parent 32a0170d64
commit da39fd6ce8
4 changed files with 1104 additions and 1014 deletions

View file

@ -134,7 +134,7 @@
}
row += '<li><hr class="dropdown-divider"></li>' +
'<li><a class="dropdown-item" href="#"><i class="bi bi-trash"></i><?php $L->p('Delete') ?></a></li>' +
'<li><a class="dropdown-item" href="#" onClick="fmDeleteFile(\'' + file.filename + '\');"><i class="bi bi-trash"></i><?php $L->p('Delete') ?></a></li>' +
'</ul>' +
'</div>' +
'</td>' +
@ -211,6 +211,24 @@
});
}
// Delete a file for the current page
function fmDeleteFile(file) {
logs('File Manager. Deleting file.');
api.deletePageFile({
'key': _pageKey,
'file': file
}).then(function(response) {
if (response.status == 0) {
logs("File Manager. File deleted.");
// Get current files
fmGetFiles();
} else {
logs("File Manager. An error occurred while trying to delete the file for the current page.");
showAlertError('File Manager. ' + response.message);
}
});
}
// Initlization and events for the File Manager
$(document).ready(function() {
// Input file change event

View file

@ -13,7 +13,7 @@
/**
* Create a new page. === bludit v4
* @param array $args All supported parameters are defined in the class pages.class.php, variable $dbFields
* @param array $args All supported parameters are defined in the class pages.class.php, variable $dbFields
* @return string|bool Returns the page key on successful create, FALSE otherwise
*/
function createPage($args) {
@ -608,6 +608,29 @@ function uploadPageFile($pageKey) {
return false;
}
/**
* Delete a file from a page.
* @param string $pageKey Page key
* @param string $file Filename to delete, filename and extension without path.
* @return bool Returns the page key on successful create, FALSE otherwise
*/
function deletePageFile($pageKey, $file) {
if (Text::stringContains($pageKey, DS, false)) {
Log::set(__FUNCTION__.LOG_SEP.'Path traversal detected.', LOG_TYPE_ERROR);
return false;
}
$fileName = Filesystem::filename($file);
$fileExtension = Filesystem::extension($file);
$fileExtension = Text::lowercase($fileExtension);
Filesystem::rmfile(PATH_UPLOADS_PAGES.$pageKey.DS.$fileName.'.'.$fileExtension);
Filesystem::rmfile(PATH_UPLOADS_PAGES.$pageKey.DS.$fileName.'-thumbnail-s.'.$fileExtension);
Filesystem::rmfile(PATH_UPLOADS_PAGES.$pageKey.DS.$fileName.'-thumbnail-m.'.$fileExtension);
return true;
}
/* Install and activate a plugin === Bludit v4
@className string The plugin PHP class name

View file

@ -302,6 +302,29 @@ class API {
}
}
/* Delete file from a page
*/
async deletePageFile(args) {
var url = this.apiURL + "pages/files/" + args['key']
var body = Object.assign({}, this.body, args);
try {
var response = await fetch(url, {
credentials: "same-origin",
method: "DELETE",
body: JSON.stringify(body),
headers: new Headers({
"Content-Type": "application/json"
})
});
var json = await response.json();
return json;
} catch (err) {
console.log(response);
console.log(err);
return {'message': 'Error from API. Open the inspector from the browser for more details.'};
}
}
/* Create a new user
@args array Arguments can be any of the fields from a user

File diff suppressed because it is too large Load diff