Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

Hooks::pre_renameOrCopy_hook()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 1
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Sam Tuke <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
/**
31
 * This class contains all hooks.
32
 */
33
34
namespace OCA\Files_Versions;
35
36
class Hooks {
37
38
	public static function connectHooks() {
39
		// Listen to write signals
40
		\OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
41
		// Listen to delete and rename signals
42
		\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
43
		\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
44
		\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
45
		\OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
46
		\OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
47
		\OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
48
49
		$eventDispatcher = \OC::$server->getEventDispatcher();
50
		$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Files_Versions\Hooks', 'onLoadFilesAppScripts']);
51
	}
52
53
	/**
54
	 * listen to write event.
55
	 */
56 View Code Duplication
	public static function write_hook( $params ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
58
		if (\OCP\App::isEnabled('files_versions')) {
59
			$path = $params[\OC\Files\Filesystem::signal_param_path];
60
			if($path<>'') {
61
				Storage::store($path);
62
			}
63
		}
64
	}
65
66
67
	/**
68
	 * Erase versions of deleted file
69
	 * @param array $params
70
	 *
71
	 * This function is connected to the delete signal of OC_Filesystem
72
	 * cleanup the versions directory if the actual file gets deleted
73
	 */
74 View Code Duplication
	public static function remove_hook($params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
76
		if (\OCP\App::isEnabled('files_versions')) {
77
			$path = $params[\OC\Files\Filesystem::signal_param_path];
78
			if($path<>'') {
79
				Storage::delete($path);
80
			}
81
		}
82
	}
83
84
	/**
85
	 * mark file as "deleted" so that we can clean up the versions if the file is gone
86
	 * @param array $params
87
	 */
88
	public static function pre_remove_hook($params) {
89
		$path = $params[\OC\Files\Filesystem::signal_param_path];
90
			if($path<>'') {
91
				Storage::markDeletedFile($path);
92
			}
93
	}
94
95
	/**
96
	 * rename/move versions of renamed/moved files
97
	 * @param array $params array with oldpath and newpath
98
	 *
99
	 * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
100
	 * of the stored versions along the actual file
101
	 */
102 View Code Duplication
	public static function rename_hook($params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
104
		if (\OCP\App::isEnabled('files_versions')) {
105
			$oldpath = $params['oldpath'];
106
			$newpath = $params['newpath'];
107
			if($oldpath<>'' && $newpath<>'') {
108
				Storage::renameOrCopy($oldpath, $newpath, 'rename');
109
			}
110
		}
111
	}
112
113
	/**
114
	 * copy versions of copied files
115
	 * @param array $params array with oldpath and newpath
116
	 *
117
	 * This function is connected to the copy signal of OC_Filesystem and copies the
118
	 * the stored versions to the new location
119
	 */
120 View Code Duplication
	public static function copy_hook($params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
122
		if (\OCP\App::isEnabled('files_versions')) {
123
			$oldpath = $params['oldpath'];
124
			$newpath = $params['newpath'];
125
			if($oldpath<>'' && $newpath<>'') {
126
				Storage::renameOrCopy($oldpath, $newpath, 'copy');
127
			}
128
		}
129
	}
130
131
	/**
132
	 * Remember owner and the owner path of the source file.
133
	 * If the file already exists, then it was a upload of a existing file
134
	 * over the web interface and we call Storage::store() directly
135
	 *
136
	 * @param array $params array with oldpath and newpath
137
	 *
138
	 */
139
	public static function pre_renameOrCopy_hook($params) {
140
		if (\OCP\App::isEnabled('files_versions')) {
141
142
			// if we rename a movable mount point, then the versions don't have
143
			// to be renamed
144
			$absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\User::getUser() has been deprecated with message: 8.0.0 Use \OC::$server->getUserSession()->getUser()->getUID()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
145
			$manager = \OC\Files\Filesystem::getMountManager();
146
			$mount = $manager->find($absOldPath);
147
			$internalPath = $mount->getInternalPath($absOldPath);
148
			if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) {
149
				return;
150
			}
151
152
			$view = new \OC\Files\View(\OCP\User::getUser() . '/files');
0 ignored issues
show
Deprecated Code introduced by
The method OCP\User::getUser() has been deprecated with message: 8.0.0 Use \OC::$server->getUserSession()->getUser()->getUID()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
			if ($view->file_exists($params['newpath'])) {
154
				Storage::store($params['newpath']);
155
			} else {
156
				Storage::setSourcePathAndUser($params['oldpath']);
157
			}
158
159
		}
160
	}
161
162
	/**
163
	 * Load additional scripts when the files app is visible
164
	 */
165
	public static function onLoadFilesAppScripts() {
166
		\OCP\Util::addScript('files_versions', 'versionmodel');
167
		\OCP\Util::addScript('files_versions', 'versioncollection');
168
		\OCP\Util::addScript('files_versions', 'versionstabview');
169
		\OCP\Util::addScript('files_versions', 'filesplugin');
170
	}
171
}
172