]> source.dussan.org Git - nextcloud-server.git/commitdiff
add ICopyFromCache trait to expose existing implementation
authorRobin Appelman <robin@icewind.nl>
Mon, 8 Mar 2021 17:49:08 +0000 (18:49 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Tue, 16 Mar 2021 16:17:56 +0000 (16:17 +0000)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/Files/Cache/Cache.php
lib/private/Files/Cache/MoveFromCacheTrait.php
lib/public/Files/Cache/ICache.php

index 840523c1890c1955f391f6e25c5118ec540139ed..1f640ac5ff176838ad5d4d51b0f594b9eb3d36f1 100644 (file)
@@ -629,7 +629,7 @@ class Cache implements ICache {
        /**
         * Move a file or folder in the cache
         *
-        * @param \OCP\Files\Cache\ICache $sourceCache
+        * @param ICache $sourceCache
         * @param string $sourcePath
         * @param string $targetPath
         * @throws \OC\DatabaseException
@@ -1076,4 +1076,44 @@ class Cache implements ICache {
        public function normalize($path) {
                return trim(\OC_Util::normalizeUnicode($path), '/');
        }
+
+       /**
+        * Copy a file or folder in the cache
+        *
+        * @param ICache $sourceCache
+        * @param ICacheEntry $sourceEntry
+        * @param string $targetPath
+        * @return int fileid of copied entry
+        */
+       public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
+               $data = $this->cacheEntryToArray($sourceEntry);
+               $fileId = $this->put($targetPath, $data);
+               if ($fileId <= 0) {
+                       throw new \RuntimeException("Failed to copy to " . $targetPath . " from cache with source data " . json_encode($data) . " ");
+               }
+               if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
+                       $folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
+                       foreach ($folderContent as $subEntry) {
+                               $subTargetPath = $targetPath . '/' . $subEntry->getName();
+                               $this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
+                       }
+               }
+               return $fileId;
+       }
+
+       private function cacheEntryToArray(ICacheEntry $entry): array {
+               return [
+                       'size' => $entry->getSize(),
+                       'mtime' => $entry->getMTime(),
+                       'storage_mtime' => $entry->getStorageMTime(),
+                       'mimetype' => $entry->getMimeType(),
+                       'mimepart' => $entry->getMimePart(),
+                       'etag' => $entry->getEtag(),
+                       'permissions' => $entry->getPermissions(),
+                       'encrypted' => $entry->isEncrypted(),
+                       'creation_time' => $entry->getCreationTime(),
+                       'upload_time' => $entry->getUploadTime(),
+                       'metadata_etag' => $entry->getMetadataEtag(),
+               ];
+       }
 }
index 0f42b00df1c3cc1cc88a4bafad10f9f4d16fc1b5..2f5bbfb7eeaaac426252429683a56947b0b7bc97 100644 (file)
@@ -41,6 +41,8 @@ trait MoveFromCacheTrait {
         */
        abstract public function put($file, array $data);
 
+       abstract public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
+
        /**
         * Move a file or folder in the cache
         *
@@ -55,38 +57,4 @@ trait MoveFromCacheTrait {
 
                $sourceCache->remove($sourcePath);
        }
-
-       /**
-        * Copy a file or folder in the cache
-        *
-        * @param \OCP\Files\Cache\ICache $sourceCache
-        * @param ICacheEntry $sourceEntry
-        * @param string $targetPath
-        */
-       public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath) {
-               $this->put($targetPath, $this->cacheEntryToArray($sourceEntry));
-               if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
-                       $folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
-                       foreach ($folderContent as $subEntry) {
-                               $subTargetPath = $targetPath . '/' . $subEntry->getName();
-                               $this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
-                       }
-               }
-       }
-
-       private function cacheEntryToArray(ICacheEntry $entry) {
-               return [
-                       'size' => $entry->getSize(),
-                       'mtime' => $entry->getMTime(),
-                       'storage_mtime' => $entry->getStorageMTime(),
-                       'mimetype' => $entry->getMimeType(),
-                       'mimepart' => $entry->getMimePart(),
-                       'etag' => $entry->getEtag(),
-                       'permissions' => $entry->getPermissions(),
-                       'encrypted' => $entry->isEncrypted(),
-                       'creation_time' => $entry->getCreationTime(),
-                       'upload_time' => $entry->getUploadTime(),
-                       'metadata_etag' => $entry->getMetadataEtag(),
-               ];
-       }
 }
index 95ca29c2aa8874d44e57fdaa9288bb816e2bd8b0..323359dcf6847c5ca67ca25b1728449e8a97830a 100644 (file)
@@ -179,6 +179,17 @@ interface ICache {
         */
        public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath);
 
+       /**
+        * Copy a file or folder in the cache
+        *
+        * @param ICache $sourceCache
+        * @param ICacheEntry $sourceEntry
+        * @param string $targetPath
+        * @return int fileid of copied entry
+        * @since 22.0.0
+        */
+       public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
+
        /**
         * Get the scan status of a file
         *