Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use in objectstore copy #23912

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions lib/private/Files/ObjectStore/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,8 @@ public function objectExists($urn) {
}
}
}

public function copyObject($from, $to) {
$this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
}
}
64 changes: 62 additions & 2 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
use Icewind\Streams\CountWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\Cache\CacheEntry;
use OC\Files\Storage\PolyFill\CopyDirectory;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCP\Files\ObjectStore\IObjectStore;

class ObjectStoreStorage extends \OC\Files\Storage\Common {
use CopyDirectory;

/**
* @var \OCP\Files\ObjectStore\IObjectStore $objectStore
*/
Expand Down Expand Up @@ -319,7 +324,7 @@ public function fopen($path, $mode) {
} else {
return false;
}
// no break
// no break
case 'w':
case 'wb':
case 'w+':
Expand Down Expand Up @@ -474,7 +479,7 @@ public function writeStream(string $path, $stream, int $size = null): int {
if ($size === null) {
$countStream = CountWrapper::wrap($stream, function ($writtenSize) use ($fileId, &$size) {
$this->getCache()->update($fileId, [
'size' => $writtenSize
'size' => $writtenSize,
]);
$size = $writtenSize;
});
Expand Down Expand Up @@ -523,4 +528,59 @@ public function writeStream(string $path, $stream, int $size = null): int {
public function getObjectStore(): IObjectStore {
return $this->objectStore;
}

public function copy($path1, $path2) {
$path1 = $this->normalizePath($path1);
$path2 = $this->normalizePath($path2);

$cache = $this->getCache();
$sourceEntry = $cache->get($path1);
if (!$sourceEntry) {
throw new NotFoundException('Source object not found');
}

$this->copyInner($sourceEntry, $path2);

return true;
}

private function copyInner(ICacheEntry $sourceEntry, string $to) {
$cache = $this->getCache();

if ($sourceEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) {
if ($cache->inCache($to)) {
$cache->remove($to);
}
$this->mkdir($to);

foreach ($cache->getFolderContentsById($sourceEntry->getId()) as $child) {
$this->copyInner($child, $to . '/' . $child->getName());
}
} else {
$this->copyFile($sourceEntry, $to);
}
}

private function copyFile(ICacheEntry $sourceEntry, string $to) {
$cache = $this->getCache();

$sourceUrn = $this->getURN($sourceEntry->getId());

$cache->copyFromCache($cache, $sourceEntry, $to);
$targetEntry = $cache->get($to);

if (!$targetEntry) {
throw new \Exception('Target not in cache after copy');
}

$targetUrn = $this->getURN($targetEntry->getId());

try {
$this->objectStore->copyObject($sourceUrn, $targetUrn);
} catch (\Exception $e) {
$cache->remove($to);

throw $e;
}
}
}
4 changes: 4 additions & 0 deletions lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ public function deleteObject($urn) {
public function objectExists($urn) {
return $this->getConnection()->doesObjectExist($this->bucket, $urn);
}

public function copyObject($from, $to) {
$this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to);
}
}
4 changes: 4 additions & 0 deletions lib/private/Files/ObjectStore/StorageObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ public function deleteObject($urn) {
public function objectExists($urn) {
return $this->storage->file_exists($urn);
}

public function copyObject($from, $to) {
$this->storage->copy($from, $to);
}
}
12 changes: 9 additions & 3 deletions lib/private/Files/ObjectStore/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public function writeObject($urn, $stream) {
if (filesize($tmpFile) < SWIFT_SEGMENT_SIZE) {
$this->getContainer()->createObject([
'name' => $urn,
'stream' => stream_for($handle)
'stream' => stream_for($handle),
]);
} else {
$this->getContainer()->createLargeObject([
'name' => $urn,
'stream' => stream_for($handle),
'segmentSize' => SWIFT_SEGMENT_SIZE
'segmentSize' => SWIFT_SEGMENT_SIZE,
]);
}
}
Expand All @@ -114,7 +114,7 @@ public function readObject($urn) {
'stream' => true,
'headers' => [
'X-Auth-Token' => $tokenId,
'Cache-Control' => 'no-cache'
'Cache-Control' => 'no-cache',
],
]
);
Expand Down Expand Up @@ -149,4 +149,10 @@ public function deleteContainer() {
public function objectExists($urn) {
return $this->getContainer()->objectExists($urn);
}

public function copyObject($from, $to) {
$this->getContainer()->getObject($from)->copy([
'destination' => $this->getContainer()->name . '/' . $to
]);
}
}
8 changes: 8 additions & 0 deletions lib/public/Files/ObjectStore/IObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public function deleteObject($urn);
* @since 16.0.0
*/
public function objectExists($urn);

/**
* @param string $from the unified resource name used to identify the source object
* @param string $to the unified resource name used to identify the target object
* @return void
* @since 21.0.0
*/
public function copyObject($from, $to);
}
4 changes: 4 additions & 0 deletions tests/lib/Files/ObjectStore/FailDeleteObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public function deleteObject($urn) {
public function objectExists($urn) {
return $this->objectStore->objectExists($urn);
}

public function copyObject($from, $to) {
$this->objectStore->copyObject($from, $to);
}
}
4 changes: 4 additions & 0 deletions tests/lib/Files/ObjectStore/FailWriteObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public function deleteObject($urn) {
public function objectExists($urn) {
return $this->objectStore->objectExists($urn);
}

public function copyObject($from, $to) {
$this->objectStore->copyObject($from, $to);
}
}
16 changes: 16 additions & 0 deletions tests/lib/Files/ObjectStore/ObjectStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,20 @@ public function testExists() {

$this->assertFalse($instance->objectExists('2'));
}

public function testCopy() {
$stream = $this->stringToStream('foobar');

$instance = $this->getInstance();

$instance->writeObject('source', $stream);

$this->assertFalse($instance->objectExists('target'));

$instance->copyObject('source', 'target');

$this->assertTrue($instance->objectExists('target'));

$this->assertEquals('foobar', stream_get_contents($instance->readObject('target')));
}
}