Hooks.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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@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. namespace OCA\Files_Versions;
  31. use OC\Files\Filesystem;
  32. use OC\Files\Mount\MoveableMount;
  33. use OC\Files\View;
  34. use OCP\Util;
  35. class Hooks {
  36. public static function connectHooks() {
  37. // Listen to write signals
  38. Util::connectHook('OC_Filesystem', 'write', Hooks::class, 'write_hook');
  39. // Listen to delete and rename signals
  40. Util::connectHook('OC_Filesystem', 'post_delete', Hooks::class, 'remove_hook');
  41. Util::connectHook('OC_Filesystem', 'delete', Hooks::class, 'pre_remove_hook');
  42. Util::connectHook('OC_Filesystem', 'post_rename', Hooks::class, 'rename_hook');
  43. Util::connectHook('OC_Filesystem', 'post_copy', Hooks::class, 'copy_hook');
  44. Util::connectHook('OC_Filesystem', 'rename', Hooks::class, 'pre_renameOrCopy_hook');
  45. Util::connectHook('OC_Filesystem', 'copy', Hooks::class, 'pre_renameOrCopy_hook');
  46. }
  47. /**
  48. * listen to write event.
  49. */
  50. public static function write_hook(array $params): void {
  51. $path = $params[Filesystem::signal_param_path];
  52. if ($path !== '') {
  53. Storage::store($path);
  54. }
  55. }
  56. /**
  57. * Erase versions of deleted file
  58. * @param array $params
  59. *
  60. * This function is connected to the delete signal of OC_Filesystem
  61. * cleanup the versions directory if the actual file gets deleted
  62. */
  63. public static function remove_hook(array $params): void {
  64. $path = $params[Filesystem::signal_param_path];
  65. if ($path !== '') {
  66. Storage::delete($path);
  67. }
  68. }
  69. /**
  70. * mark file as "deleted" so that we can clean up the versions if the file is gone
  71. * @param array $params
  72. */
  73. public static function pre_remove_hook(array $params): void {
  74. $path = $params[Filesystem::signal_param_path];
  75. if ($path !== '') {
  76. Storage::markDeletedFile($path);
  77. }
  78. }
  79. /**
  80. * rename/move versions of renamed/moved files
  81. * @param array $params array with oldpath and newpath
  82. *
  83. * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
  84. * of the stored versions along the actual file
  85. */
  86. public static function rename_hook(array $params): void {
  87. $oldpath = $params['oldpath'];
  88. $newpath = $params['newpath'];
  89. if ($oldpath !== '' && $newpath !== '') {
  90. Storage::renameOrCopy($oldpath, $newpath, 'rename');
  91. }
  92. }
  93. /**
  94. * copy versions of copied files
  95. * @param array $params array with oldpath and newpath
  96. *
  97. * This function is connected to the copy signal of OC_Filesystem and copies the
  98. * the stored versions to the new location
  99. */
  100. public static function copy_hook(array $params): void {
  101. $oldpath = $params['oldpath'];
  102. $newpath = $params['newpath'];
  103. if ($oldpath !== '' && $newpath !== '') {
  104. Storage::renameOrCopy($oldpath, $newpath, 'copy');
  105. }
  106. }
  107. /**
  108. * Remember owner and the owner path of the source file.
  109. * If the file already exists, then it was a upload of a existing file
  110. * over the web interface and we call Storage::store() directly
  111. *
  112. * @param array $params array with oldpath and newpath
  113. *
  114. */
  115. public static function pre_renameOrCopy_hook(array $params): void {
  116. // if we rename a movable mount point, then the versions don't have
  117. // to be renamed
  118. $absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files' . $params['oldpath']);
  119. $manager = Filesystem::getMountManager();
  120. $mount = $manager->find($absOldPath);
  121. $internalPath = $mount->getInternalPath($absOldPath);
  122. if ($internalPath === '' and $mount instanceof MoveableMount) {
  123. return;
  124. }
  125. $view = new View(\OC_User::getUser() . '/files');
  126. if ($view->file_exists($params['newpath'])) {
  127. Storage::store($params['newpath']);
  128. } else {
  129. Storage::setSourcePathAndUser($params['oldpath']);
  130. }
  131. }
  132. }