summaryrefslogtreecommitdiffstats
path: root/apps/files_versions/lib
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2014-10-28 15:57:08 +0100
committerBjoern Schiessle <schiessle@owncloud.com>2014-10-29 10:11:01 +0100
commit9d1be0bbaf7d8af442fc8d908baff743cc823a98 (patch)
treea550320233b0a36a9bd839d92727848b70992a85 /apps/files_versions/lib
parentc864f5e20c5ecd1bca55c6ac26c7a5d0721e85e9 (diff)
downloadnextcloud-server-9d1be0bbaf7d8af442fc8d908baff743cc823a98.tar.gz
nextcloud-server-9d1be0bbaf7d8af442fc8d908baff743cc823a98.zip
get the source path and owner in a pre hook and the target path and owner in a
post hook
Diffstat (limited to 'apps/files_versions/lib')
-rw-r--r--apps/files_versions/lib/hooks.php33
-rw-r--r--apps/files_versions/lib/versions.php46
2 files changed, 68 insertions, 11 deletions
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),
@@ -51,6 +53,34 @@ class Storage {
}
/**
+ * 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
*
* @param string $user user who owns the versions
@@ -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);