Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add ICopyFromCache trait to expose existing implementation
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot[bot] committed Mar 16, 2021
commit aaa4c071edfa0dd6f3ae831e08459925a1c6a541
42 changes: 41 additions & 1 deletion lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ protected function getMoveInfo($path) {
/**
* 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
Expand Down Expand Up @@ -1048,4 +1048,44 @@ public static function getById($id) {
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(),
];
}
}
36 changes: 2 additions & 34 deletions lib/private/Files/Cache/MoveFromCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -55,38 +57,4 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {

$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(),
];
}
}
11 changes: 11 additions & 0 deletions lib/public/Files/Cache/ICache.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public function move($source, $target);
*/
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
*
Expand Down