2016-01-27 09:45:19 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2016-01-27 09:45:19 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-01-27 09:45:19 -05:00
|
|
|
*/
|
2026-05-28 08:54:31 -04:00
|
|
|
|
2016-01-27 09:45:19 -05:00
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
|
|
|
|
use OCP\Files\Cache\ICache;
|
|
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-26 09:48:39 -04:00
|
|
|
* Generic fallback implementation for moving cache entries.
|
|
|
|
|
*
|
|
|
|
|
* This fallback copies the source entry to the target path and then removes
|
|
|
|
|
* it from the source cache.
|
|
|
|
|
*
|
|
|
|
|
* It is intended for cache implementations that do not provide a specialized
|
|
|
|
|
* in-place move operation.
|
2016-01-27 09:45:19 -05:00
|
|
|
*/
|
|
|
|
|
trait MoveFromCacheTrait {
|
2026-04-26 09:48:39 -04:00
|
|
|
|
2016-01-27 09:45:19 -05:00
|
|
|
abstract public function put($file, array $data);
|
|
|
|
|
|
2021-03-08 12:49:08 -05:00
|
|
|
abstract public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
|
|
|
|
|
|
2016-01-27 09:45:19 -05:00
|
|
|
/**
|
2026-04-26 09:48:39 -04:00
|
|
|
* Move a file or folder in the cache.
|
|
|
|
|
*
|
|
|
|
|
* This fallback performs the move as a copy-then-delete, so it does not
|
|
|
|
|
* preserve the original cache entry identity and may result in a new file id
|
|
|
|
|
* at the destination.
|
2016-01-27 09:45:19 -05:00
|
|
|
*
|
2025-11-17 09:32:54 -05:00
|
|
|
* @param ICache $sourceCache
|
2016-01-27 09:45:19 -05:00
|
|
|
* @param string $sourcePath
|
|
|
|
|
* @param string $targetPath
|
2026-04-26 10:07:01 -04:00
|
|
|
* @throws \RuntimeException if the source path cannot be found in cache
|
2016-01-27 09:45:19 -05:00
|
|
|
*/
|
|
|
|
|
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
|
|
|
|
|
$sourceEntry = $sourceCache->get($sourcePath);
|
|
|
|
|
|
2026-04-26 09:48:39 -04:00
|
|
|
if (!$sourceEntry) {
|
|
|
|
|
throw new \RuntimeException('Source path not found in cache: ' . $sourcePath);
|
|
|
|
|
}
|
2016-01-27 09:45:19 -05:00
|
|
|
|
2026-04-26 09:48:39 -04:00
|
|
|
$this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
|
|
|
|
|
// non-atomic; failed removals can leave duplicates
|
2016-01-27 09:45:19 -05:00
|
|
|
$sourceCache->remove($sourcePath);
|
|
|
|
|
}
|
|
|
|
|
}
|