diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/ObjectStore/ObjectStoreStorage.php | 4 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/S3ObjectTrait.php | 25 | ||||
-rw-r--r-- | lib/private/Files/Stream/CountReadStream.php | 65 |
3 files changed, 25 insertions, 69 deletions
diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index 7ee1c8e2055..83a649e6084 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -26,9 +26,9 @@ namespace OC\Files\ObjectStore; use Icewind\Streams\CallbackWrapper; +use Icewind\Streams\CountWrapper; use Icewind\Streams\IteratorDirectory; use OC\Files\Cache\CacheEntry; -use OC\Files\Stream\CountReadStream; use OCP\Files\NotFoundException; use OCP\Files\ObjectStore\IObjectStore; @@ -443,7 +443,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common { try { //upload to object storage if ($size === null) { - $countStream = CountReadStream::wrap($stream, function ($writtenSize) use ($fileId, &$size) { + $countStream = CountWrapper::wrap($stream, function ($writtenSize) use ($fileId, &$size) { $this->getCache()->update($fileId, [ 'size' => $writtenSize ]); diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php index 0b55c319ea8..7c46ba25c11 100644 --- a/lib/private/Files/ObjectStore/S3ObjectTrait.php +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -23,8 +23,11 @@ namespace OC\Files\ObjectStore; +use Aws\S3\Exception\S3MultipartUploadException; use Aws\S3\MultipartUploader; +use Aws\S3\ObjectUploader; use Aws\S3\S3Client; +use Icewind\Streams\CallbackWrapper; const S3_UPLOAD_PART_SIZE = 524288000; // 500MB @@ -73,12 +76,30 @@ trait S3ObjectTrait { * @since 7.0.0 */ function writeObject($urn, $stream) { - $uploader = new MultipartUploader($this->getConnection(), $stream, [ + $count = 0; + $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { + $count += $read; + }); + + $uploader = new MultipartUploader($this->getConnection(), $countStream, [ 'bucket' => $this->bucket, 'key' => $urn, 'part_size' => S3_UPLOAD_PART_SIZE ]); - $uploader->upload(); + + try { + $uploader->upload(); + } catch (S3MultipartUploadException $e) { + // This is an emty file so just touch it then + if ($count === 0 && feof($countStream)) { + $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, ''); + $uploader->upload(); + } else { + throw $e; + } + } + + fclose($countStream); } /** diff --git a/lib/private/Files/Stream/CountReadStream.php b/lib/private/Files/Stream/CountReadStream.php deleted file mode 100644 index 93cadf8f214..00000000000 --- a/lib/private/Files/Stream/CountReadStream.php +++ /dev/null @@ -1,65 +0,0 @@ -<?php declare(strict_types=1); -/** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -namespace OC\Files\Stream; - -use Icewind\Streams\Wrapper; - -class CountReadStream extends Wrapper { - /** @var int */ - private $count; - - /** @var callback */ - private $callback; - - public static function wrap($source, $callback) { - $context = stream_context_create(array( - 'count' => array( - 'source' => $source, - 'callback' => $callback, - ) - )); - return Wrapper::wrapSource($source, $context, 'count', self::class); - } - - public function dir_opendir($path, $options) { - return false; - } - - public function stream_open($path, $mode, $options, &$opened_path) { - $context = $this->loadContext('count'); - - $this->callback = $context['callback']; - return true; - } - - public function stream_read($count) { - $result = parent::stream_read($count); - $this->count += strlen($result); - return $result; - } - - public function stream_close() { - $result = parent::stream_close(); - call_user_func($this->callback, $this->count); - return $result; - } -} |