From effb7dc8ba00c683e3b6717eaf1f358ce1c69b87 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 15 Apr 2021 17:14:57 +0200 Subject: set mimetype for objects uploaded to object storages Signed-off-by: Robin Appelman --- lib/private/Files/ObjectStore/Azure.php | 14 +++++++------- lib/private/Files/ObjectStore/ObjectStoreStorage.php | 4 ++-- lib/private/Files/ObjectStore/S3ObjectTrait.php | 6 +++++- lib/private/Files/ObjectStore/StorageObjectStore.php | 8 +------- lib/private/Files/ObjectStore/Swift.php | 9 +++------ 5 files changed, 18 insertions(+), 23 deletions(-) (limited to 'lib/private/Files/ObjectStore') diff --git a/lib/private/Files/ObjectStore/Azure.php b/lib/private/Files/ObjectStore/Azure.php index 2ef13d60c56..9faaf385bd8 100644 --- a/lib/private/Files/ObjectStore/Azure.php +++ b/lib/private/Files/ObjectStore/Azure.php @@ -24,6 +24,7 @@ namespace OC\Files\ObjectStore; use MicrosoftAzure\Storage\Blob\BlobRestProxy; +use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions; use MicrosoftAzure\Storage\Common\Exceptions\ServiceException; use OCP\Files\ObjectStore\IObjectStore; @@ -100,13 +101,12 @@ class Azure implements IObjectStore { return $blob->getContentStream(); } - /** - * @param string $urn the unified resource name used to identify the object - * @param resource $stream stream with the data to write - * @throws \Exception when something goes wrong, message will be logged - */ - public function writeObject($urn, $stream) { - $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream); + public function writeObject($urn, $stream, string $mimetype = null) { + $options = new CreateBlockBlobOptions(); + if ($mimetype) { + $options->setContentType($mimetype); + } + $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options); } /** diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index 5d2cbe61ab6..598dd4f80ae 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -486,13 +486,13 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common { ]); $size = $writtenSize; }); - $this->objectStore->writeObject($urn, $countStream); + $this->objectStore->writeObject($urn, $countStream, $mimetype); if (is_resource($countStream)) { fclose($countStream); } $stat['size'] = $size; } else { - $this->objectStore->writeObject($urn, $stream); + $this->objectStore->writeObject($urn, $stream, $mimetype); } } catch (\Exception $ex) { if (!$exists) { diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php index 4d6ac3608df..250f8fd1edd 100644 --- a/lib/private/Files/ObjectStore/S3ObjectTrait.php +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -78,10 +78,11 @@ trait S3ObjectTrait { /** * @param string $urn the unified resource name used to identify the object * @param resource $stream stream with the data to write + * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0 * @throws \Exception when something goes wrong, message will be logged * @since 7.0.0 */ - public function writeObject($urn, $stream) { + public function writeObject($urn, $stream, string $mimetype = null) { $count = 0; $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { $count += $read; @@ -91,6 +92,9 @@ trait S3ObjectTrait { 'bucket' => $this->bucket, 'key' => $urn, 'part_size' => $this->uploadPartSize, + 'params' => [ + 'ContentType' => $mimetype + ] ]); try { diff --git a/lib/private/Files/ObjectStore/StorageObjectStore.php b/lib/private/Files/ObjectStore/StorageObjectStore.php index 2076bb3f88b..a3fbc3cec74 100644 --- a/lib/private/Files/ObjectStore/StorageObjectStore.php +++ b/lib/private/Files/ObjectStore/StorageObjectStore.php @@ -65,13 +65,7 @@ class StorageObjectStore implements IObjectStore { throw new \Exception(); } - /** - * @param string $urn the unified resource name used to identify the object - * @param resource $stream stream with the data to write - * @throws \Exception when something goes wrong, message will be logged - * @since 7.0.0 - */ - public function writeObject($urn, $stream) { + public function writeObject($urn, $stream, string $mimetype = null) { $handle = $this->storage->fopen($urn, 'w'); if ($handle) { stream_copy_to_stream($stream, $handle); diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php index 1b0888b0700..f9cccd0e205 100644 --- a/lib/private/Files/ObjectStore/Swift.php +++ b/lib/private/Files/ObjectStore/Swift.php @@ -74,12 +74,7 @@ class Swift implements IObjectStore { return $this->params['container']; } - /** - * @param string $urn the unified resource name used to identify the object - * @param resource $stream stream with the data to write - * @throws \Exception from openstack lib when something goes wrong - */ - public function writeObject($urn, $stream) { + public function writeObject($urn, $stream, string $mimetype = null) { $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite'); file_put_contents($tmpFile, $stream); $handle = fopen($tmpFile, 'rb'); @@ -88,12 +83,14 @@ class Swift implements IObjectStore { $this->getContainer()->createObject([ 'name' => $urn, 'stream' => stream_for($handle), + 'contentType' => $mimetype, ]); } else { $this->getContainer()->createLargeObject([ 'name' => $urn, 'stream' => stream_for($handle), 'segmentSize' => SWIFT_SEGMENT_SIZE, + 'contentType' => $mimetype, ]); } } -- cgit v1.2.3