Hooks.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Sam Tuke <mail@samtuke.com>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /**
  32. * This class contains all hooks.
  33. */
  34. namespace OCA\Files_Versions;
  35. class Hooks {
  36. public static function connectHooks() {
  37. // Listen to write signals
  38. \OCP\Util::connectHook('OC_Filesystem', 'write', Hooks::class, 'write_hook');
  39. // Listen to delete and rename signals
  40. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', Hooks::class, 'remove_hook');
  41. \OCP\Util::connectHook('OC_Filesystem', 'delete', Hooks::class, 'pre_remove_hook');
  42. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', Hooks::class, 'rename_hook');
  43. \OCP\Util::connectHook('OC_Filesystem', 'post_copy', Hooks::class, 'copy_hook');
  44. \OCP\Util::connectHook('OC_Filesystem', 'rename', Hooks::class, 'pre_renameOrCopy_hook');
  45. \OCP\Util::connectHook('OC_Filesystem', 'copy', Hooks::class, 'pre_renameOrCopy_hook');
  46. $eventDispatcher = \OC::$server->getEventDispatcher();
  47. $eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', [Hooks::class, 'onLoadFilesAppScripts']);
  48. }
  49. /**
  50. * listen to write event.
  51. */
  52. public static function write_hook( $params ) {
  53. $path = $params[\OC\Files\Filesystem::signal_param_path];
  54. if($path !== '') {
  55. Storage::store($path);
  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. $path = $params[\OC\Files\Filesystem::signal_param_path];
  67. if($path !== '') {
  68. Storage::delete($path);
  69. }
  70. }
  71. /**
  72. * mark file as "deleted" so that we can clean up the versions if the file is gone
  73. * @param array $params
  74. */
  75. public static function pre_remove_hook($params) {
  76. $path = $params[\OC\Files\Filesystem::signal_param_path];
  77. if($path !== '') {
  78. Storage::markDeletedFile($path);
  79. }
  80. }
  81. /**
  82. * rename/move versions of renamed/moved files
  83. * @param array $params array with oldpath and newpath
  84. *
  85. * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
  86. * of the stored versions along the actual file
  87. */
  88. public static function rename_hook($params) {
  89. $oldpath = $params['oldpath'];
  90. $newpath = $params['newpath'];
  91. if($oldpath !== '' && $newpath !== '') {
  92. Storage::renameOrCopy($oldpath, $newpath, 'rename');
  93. }
  94. }
  95. /**
  96. * copy versions of copied files
  97. * @param array $params array with oldpath and newpath
  98. *
  99. * This function is connected to the copy signal of OC_Filesystem and copies the
  100. * the stored versions to the new location
  101. */
  102. public static function copy_hook($params) {
  103. $oldpath = $params['oldpath'];
  104. $newpath = $params['newpath'];
  105. if($oldpath !== '' && $newpath !== '') {
  106. Storage::renameOrCopy($oldpath, $newpath, 'copy');
  107. }
  108. }
  109. /**
  110. * Remember owner and the owner path of the source file.
  111. * If the file already exists, then it was a upload of a existing file
  112. * over the web interface and we call Storage::store() directly
  113. *
  114. * @param array $params array with oldpath and newpath
  115. *
  116. */
  117. public static function pre_renameOrCopy_hook($params) {
  118. // if we rename a movable mount point, then the versions don't have
  119. // to be renamed
  120. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
  121. $manager = \OC\Files\Filesystem::getMountManager();
  122. $mount = $manager->find($absOldPath);
  123. $internalPath = $mount->getInternalPath($absOldPath);
  124. if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) {
  125. return;
  126. }
  127. $view = new \OC\Files\View(\OCP\User::getUser() . '/files');
  128. if ($view->file_exists($params['newpath'])) {
  129. Storage::store($params['newpath']);
  130. } else {
  131. Storage::setSourcePathAndUser($params['oldpath']);
  132. }
  133. }
  134. /**
  135. * Load additional scripts when the files app is visible
  136. */
  137. public static function onLoadFilesAppScripts() {
  138. \OCP\Util::addScript('files_versions', 'files_versions');
  139. }
  140. }