Watcher.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Preview;
  24. use OCP\Files\File;
  25. use OCP\Files\Node;
  26. use OCP\Files\Folder;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. /**
  30. * Class Watcher
  31. *
  32. * @package OC\Preview
  33. *
  34. * Class that will watch filesystem activity and remove previews as needed.
  35. */
  36. class Watcher {
  37. /** @var IAppData */
  38. private $appData;
  39. /** @var int[] */
  40. private $toDelete = [];
  41. /**
  42. * Watcher constructor.
  43. *
  44. * @param IAppData $appData
  45. */
  46. public function __construct(IAppData $appData) {
  47. $this->appData = $appData;
  48. }
  49. public function postWrite(Node $node) {
  50. // We only handle files
  51. if ($node instanceof Folder) {
  52. return;
  53. }
  54. try {
  55. $folder = $this->appData->getFolder($node->getId());
  56. $folder->delete();
  57. } catch (NotFoundException $e) {
  58. //Nothing to do
  59. }
  60. }
  61. public function preDelete(Node $node) {
  62. // To avoid cycles
  63. if ($this->toDelete !== []) {
  64. return;
  65. }
  66. if ($node instanceof File) {
  67. $this->toDelete[] = $node->getId();
  68. return;
  69. }
  70. /** @var Folder $node */
  71. $nodes = $node->search('');
  72. foreach ($nodes as $node) {
  73. if ($node instanceof File) {
  74. $this->toDelete[] = $node->getId();
  75. }
  76. }
  77. }
  78. public function postDelete(Node $node) {
  79. foreach ($this->toDelete as $fid) {
  80. try {
  81. $folder = $this->appData->getFolder($fid);
  82. $folder->delete();
  83. } catch (NotFoundException $e) {
  84. // continue
  85. }
  86. }
  87. }
  88. }