@@ -180,6 +180,22 @@ public function objectExists(string $name): bool
180
180
}
181
181
}
182
182
183
+ /**
184
+ * Verifies if provied segment index format for DLOs is valid.
185
+ *
186
+ * @param string $fmt The format of segment index name, e.g. %05d for 00001, 00002, etc.
187
+ *
188
+ * @return bool TRUE if the format is valid, FALSE if it is not
189
+ */
190
+ public function isValidSegmentIndexFormat ($ fmt )
191
+ {
192
+ $ testValue1 = sprintf ($ fmt , 1 );
193
+ $ testValue2 = sprintf ($ fmt , 10 );
194
+
195
+ // Test if different results of the same string length
196
+ return ($ testValue1 !== $ testValue2 ) && (strlen ($ testValue1 ) === strlen ($ testValue2 ));
197
+ }
198
+
183
199
/**
184
200
* Creates a single object according to the values provided.
185
201
*
@@ -197,11 +213,12 @@ public function createObject(array $data): StorageObject
197
213
* container. When this completes, a manifest file is uploaded which references the prefix of the segments,
198
214
* allowing concatenation when a request is executed against the manifest.
199
215
*
200
- * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
201
- * @param int $data['segmentSize'] The size in Bytes of each segment
202
- * @param string $data['segmentContainer'] The container to which each segment will be uploaded
203
- * @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default
204
- * is used: name/timestamp/filesize
216
+ * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
217
+ * @param int $data['segmentSize'] The size in Bytes of each segment
218
+ * @param string $data['segmentContainer'] The container to which each segment will be uploaded
219
+ * @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default
220
+ * is used: name/timestamp/filesize
221
+ * @param string $data['segmentIndexFormat'] The format of segment index name, default %05d - 00001, 00002, etc.
205
222
*/
206
223
public function createLargeObject (array $ data ): StorageObject
207
224
{
@@ -213,6 +230,11 @@ public function createLargeObject(array $data): StorageObject
213
230
$ segmentPrefix = isset ($ data ['segmentPrefix ' ])
214
231
? $ data ['segmentPrefix ' ]
215
232
: sprintf ('%s/%s/%d ' , $ data ['name ' ], microtime (true ), $ stream ->getSize ());
233
+ $ segmentIndexFormat = isset ($ data ['segmentIndexFormat ' ]) ? $ data ['segmentIndexFormat ' ] : '%05d ' ;
234
+
235
+ if (!$ this ->isValidSegmentIndexFormat ($ segmentIndexFormat )) {
236
+ throw new \InvalidArgumentException ('The provided segmentIndexFormat is not valid. ' );
237
+ }
216
238
217
239
/** @var \OpenStack\ObjectStore\v1\Service $service */
218
240
$ service = $ this ->getService ();
@@ -226,7 +248,7 @@ public function createLargeObject(array $data): StorageObject
226
248
227
249
while (!$ stream ->eof () && $ count < $ totalSegments ) {
228
250
$ promises [] = $ this ->model (StorageObject::class)->createAsync ([
229
- 'name ' => sprintf ('%s/%d ' , $ segmentPrefix , ++$ count ),
251
+ 'name ' => sprintf ('%s/ ' . $ segmentIndexFormat , $ segmentPrefix , ++$ count ),
230
252
'stream ' => new LimitStream ($ stream , $ segmentSize , ($ count - 1 ) * $ segmentSize ),
231
253
'containerName ' => $ segmentContainer ,
232
254
]);
0 commit comments