Add delete file from Filemanager #1384
This commit is contained in:
parent
32a0170d64
commit
da39fd6ce8
4 changed files with 1104 additions and 1014 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -94,6 +94,7 @@ class pluginAPI extends Plugin {
|
|||
$parmB = isset($parameters[1])?$parameters[1]:'';
|
||||
$parmC = isset($parameters[2])?$parameters[2]:'';
|
||||
$parmD = isset($parameters[3])?$parameters[3]:'';
|
||||
$parmE = isset($parameters[4])?$parameters[4]:'';
|
||||
|
||||
// API TOKEN
|
||||
// ------------------------------------------------------------
|
||||
|
@ -170,7 +171,7 @@ class pluginAPI extends Plugin {
|
|||
if (!empty($parmD)) {
|
||||
$key = $parmC.'/'.$parmD;
|
||||
}
|
||||
// Delete file function
|
||||
$data = $this->deletePageFile($key, $inputs);
|
||||
}
|
||||
// (GET) /api/pages/:key
|
||||
elseif ( ($method==='GET') && ($parmA==='pages') && !empty($parmB) ) {
|
||||
|
@ -907,6 +908,31 @@ class pluginAPI extends Plugin {
|
|||
);
|
||||
}
|
||||
|
||||
/* Delete a file from a page
|
||||
Referer to the function deletePageFile() from functions.php
|
||||
*/
|
||||
private function deletePageFile($pageKey, $args)
|
||||
{
|
||||
if (empty($args['file'])) {
|
||||
return array(
|
||||
'status'=>'1',
|
||||
'message'=>'The file not was specified.'
|
||||
);
|
||||
}
|
||||
|
||||
if (deletePageFile($pageKey, $args['file'])) {
|
||||
return array(
|
||||
'status'=>'0',
|
||||
'message'=>'File deleted.'
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
'status'=>'1',
|
||||
'message'=>'An error occurred while trying to delete the file.'
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
Generates unique slug text for the a page
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue