@@ -180,6 +180,22 @@ public function objectExists(string $name): bool
180180 }
181181 }
182182
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+
183199 /**
184200 * Creates a single object according to the values provided.
185201 *
@@ -197,11 +213,12 @@ public function createObject(array $data): StorageObject
197213 * container. When this completes, a manifest file is uploaded which references the prefix of the segments,
198214 * allowing concatenation when a request is executed against the manifest.
199215 *
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.
205222 */
206223 public function createLargeObject (array $ data ): StorageObject
207224 {
@@ -213,6 +230,11 @@ public function createLargeObject(array $data): StorageObject
213230 $ segmentPrefix = isset ($ data ['segmentPrefix ' ])
214231 ? $ data ['segmentPrefix ' ]
215232 : 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+ }
216238
217239 /** @var \OpenStack\ObjectStore\v1\Service $service */
218240 $ service = $ this ->getService ();
@@ -226,7 +248,7 @@ public function createLargeObject(array $data): StorageObject
226248
227249 while (!$ stream ->eof () && $ count < $ totalSegments ) {
228250 $ promises [] = $ this ->model (StorageObject::class)->createAsync ([
229- 'name ' => sprintf ('%s/%d ' , $ segmentPrefix , ++$ count ),
251+ 'name ' => sprintf ('%s/ ' . $ segmentIndexFormat , $ segmentPrefix , ++$ count ),
230252 'stream ' => new LimitStream ($ stream , $ segmentSize , ($ count - 1 ) * $ segmentSize ),
231253 'containerName ' => $ segmentContainer ,
232254 ]);
0 commit comments