From 9d1be0bbaf7d8af442fc8d908baff743cc823a98 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 28 Oct 2014 15:57:08 +0100 Subject: get the source path and owner in a pre hook and the target path and owner in a post hook --- apps/files_versions/lib/hooks.php | 33 ++++++++++++++++++++++---- apps/files_versions/lib/versions.php | 46 +++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 11 deletions(-) (limited to 'apps/files_versions/lib') diff --git a/apps/files_versions/lib/hooks.php b/apps/files_versions/lib/hooks.php index 1a584232ba7..024cb6a3c39 100644 --- a/apps/files_versions/lib/hooks.php +++ b/apps/files_versions/lib/hooks.php @@ -16,12 +16,14 @@ class Hooks { public static function connectHooks() { // Listen to write signals - \OCP\Util::connectHook('OC_Filesystem', 'write', "OCA\Files_Versions\Hooks", "write_hook"); + \OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook'); // Listen to delete and rename signals - \OCP\Util::connectHook('OC_Filesystem', 'post_delete', "OCA\Files_Versions\Hooks", "remove_hook"); - \OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Files_Versions\Hooks", "pre_remove_hook"); - \OCP\Util::connectHook('OC_Filesystem', 'rename', "OCA\Files_Versions\Hooks", "rename_hook"); - \OCP\Util::connectHook('OC_Filesystem', 'copy', "OCA\Files_Versions\Hooks", "copy_hook"); + \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook'); + \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook'); + \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook'); + \OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook'); + \OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook'); + \OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook'); } /** @@ -102,4 +104,25 @@ class Hooks { } } + /** + * Remember owner and the owner path of the source file. + * If the file already exists, then it was a upload of a existing file + * over the web interface and we call Storage::store() directly + * + * @param array $params array with oldpath and newpath + * + */ + public static function pre_renameOrCopy_hook($params) { + if (\OCP\App::isEnabled('files_versions')) { + + $view = new \OC\Files\View(\OCP\User::getUser() . '/files'); + if ($view->file_exists($params['newpath'])) { + Storage::store($params['newpath']); + } else { + Storage::setSourcePathAndUser($params['oldpath']); + } + + } + } + } diff --git a/apps/files_versions/lib/versions.php b/apps/files_versions/lib/versions.php index bdb26896948..35e79569cda 100644 --- a/apps/files_versions/lib/versions.php +++ b/apps/files_versions/lib/versions.php @@ -24,6 +24,8 @@ class Storage { // files for which we can remove the versions after the delete operation was successful private static $deletedFiles = array(); + private static $sourcePathAndUser = array(); + private static $max_versions_per_interval = array( //first 10sec, one version every 2sec 1 => array('intervalEndsAfter' => 10, 'step' => 2), @@ -50,6 +52,34 @@ class Storage { return array($uid, $filename); } + /** + * remeber the owner and the owner path of the source file + * + * @param string $source source path + */ + public static function setSourcePathAndUser($source) { + list($uid, $path) = self::getUidAndFilename($source); + self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path); + } + + /** + * gets the owner and the owner path from the source path + * + * @param string $source source path + * @return array with user id and path + */ + public static function getSourcePathAndUser($source) { + + if (isset(self::$sourcePathAndUser[$source])) { + $uid = self::$sourcePathAndUser[$source]['uid']; + $path = self::$sourcePathAndUser[$source]['path']; + unset(self::$sourcePathAndUser[$source]); + } else { + $uid = $path = false; + } + return array($uid, $path); + } + /** * get current size of all versions from a given user * @@ -180,16 +210,20 @@ class Storage { * @param string $operation can be 'copy' or 'rename' */ public static function renameOrCopy($old_path, $new_path, $operation) { - list($uid, $oldpath) = self::getUidAndFilename($old_path); + list($uid, $oldpath) = self::getSourcePathAndUser($old_path); + + // it was a upload of a existing file if no old path exists + // in this case the pre-hook already called the store method and we can + // stop here + if ($oldpath === false) { + return true; + } + list($uidn, $newpath) = self::getUidAndFilename($new_path); $versions_view = new \OC\Files\View('/'.$uid .'/files_versions'); $files_view = new \OC\Files\View('/'.$uid .'/files'); - // if the file already exists than it was a upload of a existing file - // over the web interface -> store() is the right function we need here - if ($files_view->file_exists($newpath)) { - return self::store($new_path); - } + if ( $files_view->is_dir($oldpath) && $versions_view->is_dir($oldpath) ) { $versions_view->$operation($oldpath, $newpath); -- cgit v1.2.3