diff options
author | Robin Appelman <robin@icewind.nl> | 2021-03-16 16:11:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-16 16:11:17 +0000 |
commit | f1e2fb426e107f10f49f5c49535df04f7debf191 (patch) | |
tree | 0de7415324df49c66b7e6701351e28cf23af6e3b /lib/private/Files/Cache/Cache.php | |
parent | 7a09b7d60edfdb4426565667dfbc94b132fba034 (diff) | |
parent | d7748e2b4d80612936066e2e164b0af449240518 (diff) | |
download | nextcloud-server-f1e2fb426e107f10f49f5c49535df04f7debf191.tar.gz nextcloud-server-f1e2fb426e107f10f49f5c49535df04f7debf191.zip |
Merge pull request #26013 from nextcloud/object-store-copy-cache-id
Return the fileid from `copyFromCache` and use it instead of doing an extra query
Diffstat (limited to 'lib/private/Files/Cache/Cache.php')
-rw-r--r-- | lib/private/Files/Cache/Cache.php | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 840523c1890..a18fddcbdeb 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -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,47 @@ 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 { + if ($sourceEntry->getId() < 0) { + throw new \RuntimeException("Invalid source cache entry on copyFromCache"); + } + $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(), + ]; + } } |