You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MoveFromCacheTrait.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Cache;
  23. use OCP\Files\Cache\ICache;
  24. use OCP\Files\Cache\ICacheEntry;
  25. /**
  26. * Fallback implementation for moveFromCache
  27. */
  28. trait MoveFromCacheTrait {
  29. /**
  30. * store meta data for a file or folder
  31. *
  32. * @param string $file
  33. * @param array $data
  34. *
  35. * @return int file id
  36. * @throws \RuntimeException
  37. */
  38. abstract public function put($file, array $data);
  39. /**
  40. * Move a file or folder in the cache
  41. *
  42. * @param \OCP\Files\Cache\ICache $sourceCache
  43. * @param string $sourcePath
  44. * @param string $targetPath
  45. */
  46. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  47. $sourceEntry = $sourceCache->get($sourcePath);
  48. $this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
  49. $sourceCache->remove($sourcePath);
  50. }
  51. /**
  52. * Copy a file or folder in the cache
  53. *
  54. * @param \OCP\Files\Cache\ICache $sourceCache
  55. * @param ICacheEntry $sourceEntry
  56. * @param string $targetPath
  57. */
  58. public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath) {
  59. $this->put($targetPath, $this->cacheEntryToArray($sourceEntry));
  60. if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
  61. $folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
  62. foreach ($folderContent as $subEntry) {
  63. $subTargetPath = $targetPath . '/' . $subEntry->getName();
  64. $this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
  65. }
  66. }
  67. }
  68. private function cacheEntryToArray(ICacheEntry $entry) {
  69. return [
  70. 'size' => $entry->getSize(),
  71. 'mtime' => $entry->getMTime(),
  72. 'storage_mtime' => $entry->getStorageMTime(),
  73. 'mimetype' => $entry->getMimeType(),
  74. 'mimepart' => $entry->getMimePart(),
  75. 'etag' => $entry->getEtag(),
  76. 'permissions' => $entry->getPermissions(),
  77. 'encrypted' => $entry->isEncrypted()
  78. ];
  79. }
  80. }