Skip to content
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
9 changes: 7 additions & 2 deletions apps/files_external/lib/Lib/Storage/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OC\Files\ObjectStore\S3ConnectionTrait;
use OC\Files\ObjectStore\S3ObjectTrait;
use OCP\Constants;
use OCP\Files\IMimeTypeDetector;

class AmazonS3 extends \OC\Files\Storage\Common {
use S3ConnectionTrait;
Expand All @@ -68,12 +69,16 @@ public function needsPartFile() {
/** @var CappedMemoryCache|array */
private $filesCache;

/** @var IMimeTypeDetector */
private $mimeDetector;

public function __construct($parameters) {
parent::__construct($parameters);
$this->parseParams($parameters);
$this->objectCache = new CappedMemoryCache();
$this->directoryCache = new CappedMemoryCache();
$this->filesCache = new CappedMemoryCache();
$this->mimeDetector = \OC::$server->get(IMimeTypeDetector::class);
}

/**
Expand Down Expand Up @@ -573,7 +578,7 @@ public function touch($path, $mtime = null) {

try {
if (!$this->file_exists($path)) {
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
$mimeType = $this->mimeDetector->detectPath($path);
$this->getConnection()->putObject([
'Bucket' => $this->bucket,
'Key' => $this->cleanKey($path),
Expand Down Expand Up @@ -684,7 +689,7 @@ public function getId() {
public function writeBack($tmpFile, $path) {
try {
$source = fopen($tmpFile, 'r');
$this->writeObject($path, $source);
$this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
$this->invalidateCache($path);

unlink($tmpFile);
Expand Down
9 changes: 7 additions & 2 deletions apps/files_external/lib/Lib/Storage/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\ObjectStore\SwiftFactory;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\StorageBadConfigException;
use OCP\ILogger;
use OpenStack\Common\Error\BadResponseError;
Expand Down Expand Up @@ -76,6 +77,9 @@ class Swift extends \OC\Files\Storage\Common {
/** @var \OC\Files\ObjectStore\Swift */
private $objectStore;

/** @var IMimeTypeDetector */
private $mimeDetector;

/**
* Key value cache mapping path to data object. Maps path to
* \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing
Expand Down Expand Up @@ -205,6 +209,7 @@ public function __construct($params) {
);
$this->objectStore = new \OC\Files\ObjectStore\Swift($this->params, $this->connectionFactory);
$this->bucket = $params['bucket'];
$this->mimeDetector = \OC::$server->get(IMimeTypeDetector::class);
}

public function mkdir($path) {
Expand Down Expand Up @@ -466,7 +471,7 @@ public function touch($path, $mtime = null) {
}
return true;
} else {
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
$mimeType = $this->mimeDetector->detectPath($path);
$this->getContainer()->createObject([
'name' => $path,
'content' => '',
Expand Down Expand Up @@ -588,7 +593,7 @@ public function getContainer() {

public function writeBack($tmpFile, $path) {
$fileData = fopen($tmpFile, 'r');
$this->objectStore->writeObject($path, $fileData);
$this->objectStore->writeObject($path, $fileData, $this->mimeDetector->detectPath($path));
// invalidate target object to force repopulation on fetch
$this->objectCache->remove($path);
unlink($tmpFile);
Expand Down
14 changes: 7 additions & 7 deletions lib/private/Files/ObjectStore/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -100,13 +101,12 @@ public function readObject($urn) {
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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ public function writeStream(string $path, $stream, int $size = null): int {
]);
$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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ public function readObject($urn) {
/**
* @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;
Expand All @@ -91,6 +92,9 @@ public function writeObject($urn, $stream) {
'bucket' => $this->bucket,
'key' => $urn,
'part_size' => $this->uploadPartSize,
'params' => [
'ContentType' => $mimetype
]
]);

try {
Expand Down
8 changes: 1 addition & 7 deletions lib/private/Files/ObjectStore/StorageObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ public function readObject($urn) {
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);
Expand Down
9 changes: 3 additions & 6 deletions lib/private/Files/ObjectStore/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ public function getStorageId() {
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');
Expand All @@ -88,12 +83,14 @@ public function writeObject($urn, $stream) {
$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,
]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Files/ObjectStore/IObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public function readObject($urn);
/**
* @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);

/**
* @param string $urn the unified resource name used to identify the object
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Files/ObjectStore/FailDeleteObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function readObject($urn) {
return $this->objectStore->readObject($urn);
}

public function writeObject($urn, $stream) {
return $this->objectStore->writeObject($urn, $stream);
public function writeObject($urn, $stream, string $mimetype = null) {
return $this->objectStore->writeObject($urn, $stream, $mimetype);
}

public function deleteObject($urn) {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Files/ObjectStore/FailWriteObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function readObject($urn) {
return $this->objectStore->readObject($urn);
}

public function writeObject($urn, $stream) {
public function writeObject($urn, $stream, string $mimetype = null) {
// emulate a failed write that didn't throw an error
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Files/ObjectStore/S3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use OC\Files\ObjectStore\S3;

class MultiPartUploadS3 extends S3 {
public function writeObject($urn, $stream) {
public function writeObject($urn, $stream, string $mimetype = null) {
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
'mup_threshold' => 1,
]);
Expand Down