diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-02-20 15:26:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-20 15:26:36 +0100 |
commit | c9a4f010dc588afc2969d5f347302fd8832cf638 (patch) | |
tree | 2098d80b8702b349bbe27c1c9a8892f98247aa53 | |
parent | 6e110c35b03ed780b1810e9bb68877a800fee195 (diff) | |
parent | 7cc3486229455a58f98b805ba4aa8ed99a6ac26d (diff) | |
download | nextcloud-server-c9a4f010dc588afc2969d5f347302fd8832cf638.tar.gz nextcloud-server-c9a4f010dc588afc2969d5f347302fd8832cf638.zip |
Merge pull request #14273 from nextcloud/backport/14210/stable15
[stable15] Fix empty file uploads to S3 (and other streaming storages)
m--------- | 3rdparty | 0 | ||||
-rw-r--r-- | apps/dav/lib/Connector/Sabre/File.php | 21 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/S3ObjectTrait.php | 25 |
3 files changed, 42 insertions, 4 deletions
diff --git a/3rdparty b/3rdparty -Subproject 4c31d7509c9ab8388429a5c3e671dc3e49f9fe9 +Subproject 88f57274c0fa29e05a08351944915b0772a2fc2 diff --git a/apps/dav/lib/Connector/Sabre/File.php b/apps/dav/lib/Connector/Sabre/File.php index f948f0f552d..8aa7d66ba34 100644 --- a/apps/dav/lib/Connector/Sabre/File.php +++ b/apps/dav/lib/Connector/Sabre/File.php @@ -36,6 +36,7 @@ namespace OCA\DAV\Connector\Sabre; +use Icewind\Streams\CallbackWrapper; use OC\AppFramework\Http\Request; use OC\Files\Filesystem; use OC\Files\View; @@ -166,10 +167,26 @@ class File extends Node implements IFile { } if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { - $count = $partStorage->writeStream($internalPartPath, $data); + + if (!is_resource($data)) { + $data = fopen('php://temp', 'r+'); + fwrite($data, 'foobar'); + rewind($data); + } + + $isEOF = false; + $wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function($stream) use (&$isEOF) { + $isEOF = feof($stream); + }); + + $count = $partStorage->writeStream($internalPartPath, $wrappedData); $result = $count > 0; + if ($result === false) { - $result = feof($data); + $result = $isEOF; + if (is_resource($wrappedData)) { + $result = feof($wrappedData); + } } } else { 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); } /** |