Skip to content

Commit e483387

Browse files
committed
feat: more generic way of passing metadata to object storage backends for new objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 14a3b13 commit e483387

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

lib/private/Files/ObjectStore/IObjectStoreMetaData.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ public function getObjectMetaData(string $urn): array;
3333
* @since 32.0.0
3434
*/
3535
public function listObjects(string $prefix = ''): \Iterator;
36+
37+
/**
38+
* @param string $urn the unified resource name used to identify the object
39+
* @param resource $stream stream with the data to write
40+
* @param ObjectMetaData $metaData the metadata to set for the object
41+
* @throws \Exception when something goes wrong, message will be logged
42+
* @since 32.0.0
43+
*/
44+
public function writeObjectWithMetaData(string $urn, $stream, array $metaData): void;
3645
}

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
479479

480480
$mimetypeDetector = \OC::$server->getMimeTypeDetector();
481481
$mimetype = $mimetypeDetector->detectPath($path);
482+
$metadata = [
483+
'mimetype' => $mimetype,
484+
];
482485

483486
$stat['mimetype'] = $mimetype;
484487
$stat['etag'] = $this->getETag($path);
@@ -507,13 +510,21 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
507510
]);
508511
$size = $writtenSize;
509512
});
510-
$this->objectStore->writeObject($urn, $countStream, $mimetype);
513+
if ($this->objectStore instanceof IObjectStoreMetaData) {
514+
$this->objectStore->writeObjectWithMetaData($urn, $countStream, $metadata);
515+
} else {
516+
$this->objectStore->writeObject($urn, $countStream, $metadata['mimetype']);
517+
}
511518
if (is_resource($countStream)) {
512519
fclose($countStream);
513520
}
514521
$stat['size'] = $size;
515522
} else {
516-
$this->objectStore->writeObject($urn, $stream, $mimetype);
523+
if ($this->objectStore instanceof IObjectStoreMetaData) {
524+
$this->objectStore->writeObjectWithMetaData($urn, $stream, $metadata);
525+
} else {
526+
$this->objectStore->writeObject($urn, $stream, $metadata['mimetype']);
527+
}
517528
if (is_resource($stream)) {
518529
fclose($stream);
519530
}

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ public function readObject($urn) {
8383
*
8484
* @param string $urn the unified resource name used to identify the object
8585
* @param StreamInterface $stream stream with the data to write
86-
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
86+
* @param array $metaData the metadata to set for the object
8787
* @throws \Exception when something goes wrong, message will be logged
8888
*/
89-
protected function writeSingle(string $urn, StreamInterface $stream, ?string $mimetype = null): void {
89+
protected function writeSingle(string $urn, StreamInterface $stream, array $metaData): void {
9090
$this->getConnection()->putObject([
9191
'Bucket' => $this->bucket,
9292
'Key' => $urn,
9393
'Body' => $stream,
9494
'ACL' => 'private',
95-
'ContentType' => $mimetype,
95+
'ContentType' => $metaData['mimetype'] ?? null,
9696
'StorageClass' => $this->storageClass,
9797
] + $this->getSSECParameters());
9898
}
@@ -103,10 +103,10 @@ protected function writeSingle(string $urn, StreamInterface $stream, ?string $mi
103103
*
104104
* @param string $urn the unified resource name used to identify the object
105105
* @param StreamInterface $stream stream with the data to write
106-
* @param string|null $mimetype the mimetype to set for the remove object
106+
* @param array $metaData the metadata to set for the object
107107
* @throws \Exception when something goes wrong, message will be logged
108108
*/
109-
protected function writeMultiPart(string $urn, StreamInterface $stream, ?string $mimetype = null): void {
109+
protected function writeMultiPart(string $urn, StreamInterface $stream, array $metaData): void {
110110
$attempts = 0;
111111
$uploaded = false;
112112
$concurrency = $this->concurrency;
@@ -122,7 +122,7 @@ protected function writeMultiPart(string $urn, StreamInterface $stream, ?string
122122
'part_size' => $this->uploadPartSize,
123123
'state' => $state,
124124
'params' => [
125-
'ContentType' => $mimetype,
125+
'ContentType' => $metaData['mimetype'] ?? null,
126126
'StorageClass' => $this->storageClass,
127127
] + $this->getSSECParameters(),
128128
]);
@@ -156,15 +156,15 @@ protected function writeMultiPart(string $urn, StreamInterface $stream, ?string
156156
}
157157
}
158158

159-
160-
/**
161-
* @param string $urn the unified resource name used to identify the object
162-
* @param resource $stream stream with the data to write
163-
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
164-
* @throws \Exception when something goes wrong, message will be logged
165-
* @since 7.0.0
166-
*/
167159
public function writeObject($urn, $stream, ?string $mimetype = null) {
160+
$metaData = [];
161+
if ($mimetype) {
162+
$metaData['mimetype'] = $mimetype;
163+
}
164+
$this->writeObjectWithMetaData($urn, $stream, $metaData);
165+
}
166+
167+
public function writeObjectWithMetaData(string $urn, $stream, array $metaData): void {
168168
$canSeek = fseek($stream, 0, SEEK_CUR) === 0;
169169
$psrStream = Utils::streamFor($stream);
170170

@@ -179,16 +179,16 @@ public function writeObject($urn, $stream, ?string $mimetype = null) {
179179
$buffer->seek(0);
180180
if ($buffer->getSize() < $this->putSizeLimit) {
181181
// buffer is fully seekable, so use it directly for the small upload
182-
$this->writeSingle($urn, $buffer, $mimetype);
182+
$this->writeSingle($urn, $buffer, $metaData);
183183
} else {
184184
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
185-
$this->writeMultiPart($urn, $loadStream, $mimetype);
185+
$this->writeMultiPart($urn, $loadStream, $metaData);
186186
}
187187
} else {
188188
if ($size < $this->putSizeLimit) {
189-
$this->writeSingle($urn, $psrStream, $mimetype);
189+
$this->writeSingle($urn, $psrStream, $metaData);
190190
} else {
191-
$this->writeMultiPart($urn, $psrStream, $mimetype);
191+
$this->writeMultiPart($urn, $psrStream, $metaData);
192192
}
193193
}
194194
$psrStream->close();

0 commit comments

Comments
 (0)