Hooks.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Sam Tuke <mail@samtuke.com>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  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. * This class contains all hooks.
  31. */
  32. namespace OCA\Files_Versions;
  33. class Hooks {
  34. public static function connectHooks() {
  35. // Listen to write signals
  36. \OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
  37. // Listen to delete and rename signals
  38. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
  39. \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
  40. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
  41. \OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
  42. \OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
  43. \OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
  44. $eventDispatcher = \OC::$server->getEventDispatcher();
  45. $eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Files_Versions\Hooks', 'onLoadFilesAppScripts']);
  46. }
  47. /**
  48. * listen to write event.
  49. */
  50. public static function write_hook( $params ) {
  51. if (\OCP\App::isEnabled('files_versions')) {
  52. $path = $params[\OC\Files\Filesystem::signal_param_path];
  53. if($path<>'') {
  54. Storage::store($path);
  55. }
  56. }
  57. }
  58. /**
  59. * Erase versions of deleted file
  60. * @param array $params
  61. *
  62. * This function is connected to the delete signal of OC_Filesystem
  63. * cleanup the versions directory if the actual file gets deleted
  64. */
  65. public static function remove_hook($params) {
  66. if (\OCP\App::isEnabled('files_versions')) {
  67. $path = $params[\OC\Files\Filesystem::signal_param_path];
  68. if($path<>'') {
  69. Storage::delete($path);
  70. }
  71. }
  72. }
  73. /**
  74. * mark file as "deleted" so that we can clean up the versions if the file is gone
  75. * @param array $params
  76. */
  77. public static function pre_remove_hook($params) {
  78. $path = $params[\OC\Files\Filesystem::signal_param_path];
  79. if($path<>'') {
  80. Storage::markDeletedFile($path);
  81. }
  82. }
  83. /**
  84. * rename/move versions of renamed/moved files
  85. * @param array $params array with oldpath and newpath
  86. *
  87. * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
  88. * of the stored versions along the actual file
  89. */
  90. public static function rename_hook($params) {
  91. if (\OCP\App::isEnabled('files_versions')) {
  92. $oldpath = $params['oldpath'];
  93. $newpath = $params['newpath'];
  94. if($oldpath<>'' && $newpath<>'') {
  95. Storage::renameOrCopy($oldpath, $newpath, 'rename');
  96. }
  97. }
  98. }
  99. /**
  100. * copy versions of copied files
  101. * @param array $params array with oldpath and newpath
  102. *
  103. * This function is connected to the copy signal of OC_Filesystem and copies the
  104. * the stored versions to the new location
  105. */
  106. public static function copy_hook($params) {
  107. if (\OCP\App::isEnabled('files_versions')) {
  108. $oldpath = $params['oldpath'];
  109. $newpath = $params['newpath'];
  110. if($oldpath<>'' && $newpath<>'') {
  111. Storage::renameOrCopy($oldpath, $newpath, 'copy');
  112. }
  113. }
  114. }
  115. /**
  116. * Remember owner and the owner path of the source file.
  117. * If the file already exists, then it was a upload of a existing file
  118. * over the web interface and we call Storage::store() directly
  119. *
  120. * @param array $params array with oldpath and newpath
  121. *
  122. */
  123. public static function pre_renameOrCopy_hook($params) {
  124. if (\OCP\App::isEnabled('files_versions')) {
  125. // if we rename a movable mount point, then the versions don't have
  126. // to be renamed
  127. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
  128. $manager = \OC\Files\Filesystem::getMountManager();
  129. $mount = $manager->find($absOldPath);
  130. $internalPath = $mount->getInternalPath($absOldPath);
  131. if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) {
  132. return;
  133. }
  134. $view = new \OC\Files\View(\OCP\User::getUser() . '/files');
  135. if ($view->file_exists($params['newpath'])) {
  136. Storage::store($params['newpath']);
  137. } else {
  138. Storage::setSourcePathAndUser($params['oldpath']);
  139. }
  140. }
  141. }
  142. /**
  143. * Load additional scripts when the files app is visible
  144. */
  145. public static function onLoadFilesAppScripts() {
  146. \OCP\Util::addScript('files_versions', 'merged');
  147. }
  148. }