diff options
author | Sergey Linnik <sergey.linnik@onlyoffice.com> | 2020-07-17 16:47:53 +0300 |
---|---|---|
committer | Sergey Linnik <sergey.linnik@onlyoffice.com> | 2020-07-21 10:31:04 +0300 |
commit | e4ba1fceec601b6262261c48bc7059b1ba0867e2 (patch) | |
tree | 271f2fe1806696896f8c24596aa21f2d2df09bbd | |
parent | a1fc9f88196aafd77168838421c1251808808872 (diff) |
delete file hook
-rw-r--r-- | appinfo/application.php | 4 | ||||
-rw-r--r-- | lib/fileversions.php | 23 | ||||
-rw-r--r-- | lib/hooks.php | 77 |
3 files changed, 104 insertions, 0 deletions
diff --git a/appinfo/application.php b/appinfo/application.php index 0e06449..e83a0e9 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -42,6 +42,7 @@ use OCA\Onlyoffice\Controller\EditorController; use OCA\Onlyoffice\Controller\SettingsController; use OCA\Onlyoffice\Crypt; use OCA\Onlyoffice\DirectEditor; +use OCA\Onlyoffice\Hooks; class Application extends App { @@ -216,5 +217,8 @@ class Application extends App { $c->query("IManager") ); }); + + + Hooks::connectHooks(); } } diff --git a/lib/fileversions.php b/lib/fileversions.php index cc327e0..1c3a9c0 100644 --- a/lib/fileversions.php +++ b/lib/fileversions.php @@ -254,4 +254,27 @@ class FileVersions { $logger->logException($e, ["message" => "saveHistory: save $fileId history error", "app" => self::$appName]); } } + + /** + * Delete all versions of file + * + * @param string $ownerId - file owner id + * @param string $fileId - file id + */ + public static function deleteAllVersions($ownerId, $fileId) { + $logger = \OC::$server->getLogger(); + + $logger->debug("deleteAllVersions $ownerId $fileId", ["app" => self::$appName]); + + if ($ownerId === null || $fileId === null) { + return; + } + + list ($view, $path) = self::getView($ownerId, $fileId); + if ($view === null) { + return; + } + + $view->unlink($path); + } } diff --git a/lib/hooks.php b/lib/hooks.php new file mode 100644 index 0000000..b0ab0cc --- /dev/null +++ b/lib/hooks.php @@ -0,0 +1,77 @@ +<?php +/** + * + * (c) Copyright Ascensio System SIA 2020 + * + * This program is a free software product. + * You can redistribute it and/or modify it under the terms of the GNU Affero General Public License + * (AGPL) version 3 as published by the Free Software Foundation. + * In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * For details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions of the Program + * must display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product logo when distributing the program. + * Pursuant to Section 7(e) we decline to grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as well as technical + * writing content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International. + * See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +namespace OCA\Onlyoffice; + +use OC\Files\Filesystem; + +use OCP\Util; + +/** + * The class to handle the filesystem hooks + * + * @package OCA\Onlyoffice + */ +class Hooks { + + /** + * Application name + * + * @var string + */ + private static $appName = "onlyoffice"; + + public static function connectHooks() { + // Listen file deletion + Util::connectHook("OC_Filesystem", "delete", Hooks::class, "fileDelete"); + } + + /** + * Erase versions of deleted file + * + * @param array $params - hook params + */ + public static function fileDelete($params) { + $filePath = $params[Filesystem::signal_param_path]; + if (empty($filePath)) { + return; + } + + try { + $ownerId = Filesystem::getOwner($filePath); + + $fileInfo = Filesystem::getFileInfo($filePath); + $fileId = $fileInfo->getId(); + + FileVersions::deleteAllVersions($ownerId, $fileId); + } catch (\Exception $e) { + \OC::$server->getLogger()->logException($e, ["message" => "Hook: fileDelete " . json_encode($params), "app" => self::$appName]); + } + } +} |