Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias <ilovemilk@wusa.io>2020-04-13 16:08:50 +0300
committerMatthias <ilovemilk@wusa.io>2020-04-13 16:08:50 +0300
commit3fb91cdaae51b0096a1c21bc254e7b3e478b8509 (patch)
treeb9798e2f3e094b310a3776f3ee7e1cb5839ce6f6 /lib/Controller
parent158a1c41dd41fe6b602dc29aa31ab95beb8b6793 (diff)
add notification to history page
Diffstat (limited to 'lib/Controller')
-rw-r--r--lib/Controller/FileOperationController.php140
1 files changed, 80 insertions, 60 deletions
diff --git a/lib/Controller/FileOperationController.php b/lib/Controller/FileOperationController.php
index d38ff29..f47d301 100644
--- a/lib/Controller/FileOperationController.php
+++ b/lib/Controller/FileOperationController.php
@@ -123,81 +123,101 @@ class FileOperationController extends Controller
* @NoAdminRequired
* @NoCSRFRequired
*
- * @param int $id file operation id
+ * @param array $ids file operation id
*
* @return JSONResponse
*/
- public function recover($id)
+ public function recover($ids)
{
- try {
- $file = $this->service->find($id);
- switch ($file->getCommand()) {
- case Monitor::WRITE:
- // Recover new created files by deleting them
- $filePath = $file->getPath().'/'.$file->getOriginalName();
- if ($this->deleteFromStorage($filePath)) {
+ $deleted = 0;
+ $recovered = 0;
+ $filesRecovered = array();
+ $error = false;
+ $badRequest = false;
+
+ foreach ($ids as $id) {
+ try {
+ $file = $this->service->find($id);
+ switch ($file->getCommand()) {
+ case Monitor::WRITE:
+ // Recover new created files by deleting them
+ $filePath = $file->getPath().'/'.$file->getOriginalName();
+ if ($this->deleteFromStorage($filePath)) {
+ $this->service->deleteById($id);
+
+ $deleted++;
+ array_push($filesRecovered, $id);
+ } else {
+ // File cannot be deleted
+ $error = true;
+ }
+ break;
+ case Monitor::DELETE:
+ // Recover deleted files by restoring them from the trashbin
+ // It's not necessary to use the real path
+ $dir = '/';
+ $candidate = $this->findCandidateToRestore($dir, $file->getOriginalName());
+ if ($candidate !== null) {
+ $path = $dir.'/'.$candidate['name'].'.d'.$candidate['mtime'];
+ if (Trashbin::restore($path, $candidate['name'], $candidate['mtime']) !== false) {
+ $this->service->deleteById($id);
+
+ $recovered++;
+ array_push($filesRecovered, $id);
+ }
+ // File does not exist
+ $badRequest = false;
+ } else {
+ // No candidate found
+ $badRequest = false;
+ }
+ break;
+ case Monitor::RENAME:
$this->service->deleteById($id);
- return new JSONResponse(null, Http::STATUS_NO_CONTENT);
- } else {
- // File cannot be deleted
- return new JSONResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR);
- }
- break;
- case Monitor::DELETE:
- // Recover deleted files by restoring them from the trashbin
- // It's not necessary to use the real path
- $dir = '/';
- $candidate = $this->findCandidateToRestore($dir, $file->getOriginalName());
- if ($candidate !== null) {
- $path = $dir.'/'.$candidate['name'].'.d'.$candidate['mtime'];
- if (Trashbin::restore($path, $candidate['name'], $candidate['mtime']) !== false) {
+ $deleted++;
+ array_push($filesRecovered, $id);
+ break;
+ case Monitor::CREATE:
+ // Recover new created files/folders
+ $filePath = $file->getPath().'/'.$file->getOriginalName();
+ if ($this->deleteFromStorage($filePath)) {
$this->service->deleteById($id);
- return new JSONResponse(null, Http::STATUS_NO_CONTENT);
+ $deleted++;
+ array_push($filesRecovered, $id);
+ } else {
+ // File cannot be deleted
+ $error = true;
}
- // File does not exist
- return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
- } else {
- // No candidate found
- return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
- }
- break;
- case Monitor::RENAME:
- $this->service->deleteById($id);
-
- return new JSONResponse(null, Http::STATUS_NO_CONTENT);
- break;
- case Monitor::CREATE:
- // Recover new created folders
- $filePath = $file->getPath().'/'.$file->getOriginalName();
- if ($this->deleteFromStorage($filePath)) {
+ break;
+ default:
+ // All other commands need no recovery
$this->service->deleteById($id);
- return new JSONResponse(null, Http::STATUS_NO_CONTENT);
- } else {
- // File cannot be deleted
- return new JSONResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR);
+ $deleted++;
+ array_push($filesRecovered, $id);
+ break;
}
- break;
- default:
- // All other commands need no recovery
- $this->service->deleteById($id);
-
- return new JSONResponse(null, Http::STATUS_NO_CONTENT);
- break;
- }
- } catch (\OCP\AppFramework\Db\MultipleObjectsReturnedException $exception) {
- // Found more than one with the same file name
- $this->logger->debug('recover: Found more than one with the same file name.', array('app' => Application::APP_ID));
+ } catch (\OCP\AppFramework\Db\MultipleObjectsReturnedException $exception) {
+ // Found more than one with the same file name
+ $this->logger->debug('recover: Found more than one with the same file name.', array('app' => Application::APP_ID));
- return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
- } catch (\OCP\AppFramework\Db\DoesNotExistException $exception) {
- // Nothing found
- $this->logger->debug('recover: Files does not exist.', array('app' => Application::APP_ID));
+ $badRequest = false;
+ } catch (\OCP\AppFramework\Db\DoesNotExistException $exception) {
+ // Nothing found
+ $this->logger->debug('recover: Files does not exist.', array('app' => Application::APP_ID));
- return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
+ $badRequest = false;
+ }
+ }
+ if ($error) {
+ return new JSONResponse(array('recovered' => $recovered, 'deleted' => $deleted, 'filesRecovered' => $filesRecovered), Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ if ($badRequest) {
+ return new JSONResponse(array('recovered' => $recovered, 'deleted' => $deleted, 'filesRecovered' => $filesRecovered), Http::STATUS_BAD_REQUEST);
}
+ return new JSONResponse(array('recovered' => $recovered, 'deleted' => $deleted, 'filesRecovered' => $filesRecovered), Http::STATUS_OK);
}
/**