Hooks.php 4.6 KB

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