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.

92 lines
2.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\Cache;
  24. use OCP\Files\Cache\ICache;
  25. use OCP\Files\Cache\ICacheEntry;
  26. /**
  27. * Fallback implementation for moveFromCache
  28. */
  29. trait MoveFromCacheTrait {
  30. /**
  31. * store meta data for a file or folder
  32. *
  33. * @param string $file
  34. * @param array $data
  35. *
  36. * @return int file id
  37. * @throws \RuntimeException
  38. */
  39. abstract public function put($file, array $data);
  40. /**
  41. * Move a file or folder in the cache
  42. *
  43. * @param \OCP\Files\Cache\ICache $sourceCache
  44. * @param string $sourcePath
  45. * @param string $targetPath
  46. */
  47. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  48. $sourceEntry = $sourceCache->get($sourcePath);
  49. $this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
  50. $sourceCache->remove($sourcePath);
  51. }
  52. /**
  53. * Copy a file or folder in the cache
  54. *
  55. * @param \OCP\Files\Cache\ICache $sourceCache
  56. * @param ICacheEntry $sourceEntry
  57. * @param string $targetPath
  58. */
  59. public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath) {
  60. $this->put($targetPath, $this->cacheEntryToArray($sourceEntry));
  61. if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
  62. $folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
  63. foreach ($folderContent as $subEntry) {
  64. $subTargetPath = $targetPath . '/' . $subEntry->getName();
  65. $this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
  66. }
  67. }
  68. }
  69. private function cacheEntryToArray(ICacheEntry $entry) {
  70. return [
  71. 'size' => $entry->getSize(),
  72. 'mtime' => $entry->getMTime(),
  73. 'storage_mtime' => $entry->getStorageMTime(),
  74. 'mimetype' => $entry->getMimeType(),
  75. 'mimepart' => $entry->getMimePart(),
  76. 'etag' => $entry->getEtag(),
  77. 'permissions' => $entry->getPermissions(),
  78. 'encrypted' => $entry->isEncrypted(),
  79. 'creation_time' => $entry->getCreationTime(),
  80. 'upload_time' => $entry->getUploadTime(),
  81. 'metadata_etag' => $entry->getMetadataEtag(),
  82. ];
  83. }
  84. }