Skip to content
Open
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
26 changes: 26 additions & 0 deletions Storage/src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,8 @@ public function update(array $options = [])
* matches the given value.
* @type string $ifMetagenerationMatch Makes the operation conditional on whether the object's current
* metageneration matches the given value.
* @type bool $deleteSourceObjects If true, the source objects will be
* deleted after a successful compose operation.
* }
* @return StorageObject
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -1185,11 +1187,35 @@ public function compose(array $sourceObjects, $name, array $options = [])
throw new \InvalidArgumentException('A content type could not be detected and must be provided manually.');
}

$deleteSourceObjects = $options['deleteSourceObjects'] ?? false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the option is a bool we should enforce the bool:

if (!$deleteSourceObjects instanceof bool) {
    throw new \InvalidArgumentException('The deleteSourceObjects option should be a boolean');
}

currently If the user passes a ['deleteSourceObjects'] = 52 the check for $deleteSourceObjects would be "true" even if the value is not a boolean.

It might behave as expected, but I strongly believe we should let the user know that a parameter is the wrong parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the excellent feedback! Enforcing strict type checking for deleteSourceObjects is a great safety addition to prevent loose 'truthy' evaluations.

Regarding the implementation: in PHP, the instanceof operator is reserved only for checking objects against class/interface structures and does not support checking primitive types (like bool, string, int). Calling $val instanceof bool on a real boolean will look for a class named bool, return false, and therefore always throw the exception.

To resolve this, I've implemented the validation using PHP's built-in primitive validation function is_bool():

if (!is_bool($deleteSourceObjects)) {
    throw new \InvalidArgumentException('The deleteSourceObjects option should be a boolean.');
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah! My bad! haha it is in fact not instanceof bool but is_bool, you got it!


if (!is_bool($deleteSourceObjects)) {
throw new \InvalidArgumentException('The deleteSourceObjects option should be a boolean.');
}

unset($options['metadata']);
unset($options['predefinedAcl']);

$response = $this->connection->composeObject(array_filter($options));

if ($deleteSourceObjects) {
$deleteOptions = array_filter([
'userProject' => $this->identity['userProject']
]);

foreach ($sourceObjects as $sourceObject) {
try {
if ($sourceObject instanceof StorageObject) {
$sourceObject->delete($deleteOptions);
} else {
$this->object($sourceObject)->delete($deleteOptions);
}
} catch (NotFoundException $e) {
// Gracefully ignore duplicate source files or parallel deletions
}
}
}

return new StorageObject(
$this->connection,
$response['name'],
Expand Down
5 changes: 5 additions & 0 deletions Storage/src/Connection/ServiceDefinition/storage-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4398,6 +4398,11 @@
"type": "string",
"description": "The project to be billed for this request. Required for Requester Pays buckets.",
"location": "query"
},
"deleteSourceObjects": {
"type": "boolean",
"description": "If true, the source objects will be deleted after a successful compose operation.",
"location": "query"
}
},
"parameterOrder": [
Expand Down
78 changes: 78 additions & 0 deletions Storage/tests/System/ManageObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,84 @@ public function testComposeObjects($object)
return $composedObject;
}

public function testComposeObjectsWithDeleteSourceObjects()
{
$source1 = self::$bucket->upload('content1', ['name' => uniqid(self::TESTING_PREFIX) . '-s1.txt']);
$source2 = self::$bucket->upload('content2', ['name' => uniqid(self::TESTING_PREFIX) . '-s2.txt']);

$this->assertTrue($source1->exists());
$this->assertTrue($source2->exists());

$name = uniqid(self::TESTING_PREFIX) . '-composed.txt';
$composedObject = self::$bucket->compose(
[$source1, $source2],
$name,
['deleteSourceObjects' => true]
);

$this->assertEquals($name, $composedObject->name());
$this->assertEquals('content1content2', $composedObject->downloadAsString());

$this->assertFalse($source1->exists());
$this->assertFalse($source2->exists());

$composedObject->delete();
}

public function testComposeObjectsWithDeleteSourceObjectsFalse()
{
$source1 = self::$bucket->upload('content1', ['name' => uniqid(self::TESTING_PREFIX) . '-s1.txt']);
$source2 = self::$bucket->upload('content2', ['name' => uniqid(self::TESTING_PREFIX) . '-s2.txt']);

$this->assertTrue($source1->exists());
$this->assertTrue($source2->exists());

$name = uniqid(self::TESTING_PREFIX) . '-composed.txt';
$composedObject = self::$bucket->compose(
[$source1, $source2],
$name,
['deleteSourceObjects' => false]
);

$this->assertEquals($name, $composedObject->name());
$this->assertEquals('content1content2', $composedObject->downloadAsString());

// Source objects should still exist because deleteSourceObjects is false
$this->assertTrue($source1->exists());
$this->assertTrue($source2->exists());

$source1->delete();
$source2->delete();
$composedObject->delete();
}

public function testComposeObjectsWithDeleteSourceObjectsNull()
{
$source1 = self::$bucket->upload('content1', ['name' => uniqid(self::TESTING_PREFIX) . '-s1.txt']);
$source2 = self::$bucket->upload('content2', ['name' => uniqid(self::TESTING_PREFIX) . '-s2.txt']);

$this->assertTrue($source1->exists());
$this->assertTrue($source2->exists());

$name = uniqid(self::TESTING_PREFIX) . '-composed.txt';
$composedObject = self::$bucket->compose(
[$source1, $source2],
$name,
['deleteSourceObjects' => null]
);

$this->assertEquals($name, $composedObject->name());
$this->assertEquals('content1content2', $composedObject->downloadAsString());

// Source objects should still exist because deleteSourceObjects is null
$this->assertTrue($source1->exists());
$this->assertTrue($source2->exists());

$source1->delete();
$source2->delete();
$composedObject->delete();
}

public function testSoftDeleteObject()
{
$softDeleteBucketName = "soft-delete-bucket-" . uniqid();
Expand Down
101 changes: 101 additions & 0 deletions Storage/tests/Unit/BucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@ public function testComposeThrowsExceptionWithUnknownContentType()
$bucket->compose(['file1.txt', 'file2.txt'], 'combined-files.abc');
}

public function testComposeThrowsExceptionWithInvalidDeleteSourceObjects()
{
$this->expectException(InvalidArgumentException::class);

$bucket = $this->getBucket();

$bucket->compose(['file1.txt', 'file2.txt'], 'combined-files.txt', [
'deleteSourceObjects' => 'not-a-boolean'
]);
}

/**
* @dataProvider composeProvider
*/
Expand Down Expand Up @@ -419,6 +430,96 @@ public function testComposesObjects(
$this->assertEquals($destinationObject, $object->name());
}

public function testComposeWithDeleteSourceObjects()
{
$acl = 'private';
$destinationObject = 'combined-files.txt';
$this->connection->composeObject([
'destinationBucket' => self::BUCKET_NAME,
'destinationObject' => $destinationObject,
'destinationPredefinedAcl' => $acl,
'destination' => ['contentType' => 'text/plain'],
'sourceObjects' => [['name' => 'file1.txt'], ['name' => 'file2.txt']],
'deleteSourceObjects' => true,
])
->willReturn([
'name' => $destinationObject,
'generation' => 1
])
->shouldBeCalledTimes(1);

$this->connection->deleteObject([
'bucket' => self::BUCKET_NAME,
'object' => 'file1.txt'
])->shouldBeCalledTimes(1);

$this->connection->deleteObject([
'bucket' => self::BUCKET_NAME,
'object' => 'file2.txt'
])->shouldBeCalledTimes(1);

$bucket = $this->getBucket();

$object = $bucket->compose(['file1.txt', 'file2.txt'], $destinationObject, [
'predefinedAcl' => $acl,
'deleteSourceObjects' => true
]);

$this->assertEquals($destinationObject, $object->name());
}

public function testComposeWithDeleteSourceObjectsFalse()
{
$acl = 'private';
$destinationObject = 'combined-files.txt';
$this->connection->composeObject([
'destinationBucket' => self::BUCKET_NAME,
'destinationObject' => $destinationObject,
'destinationPredefinedAcl' => $acl,
'destination' => ['contentType' => 'text/plain'],
'sourceObjects' => [['name' => 'file1.txt'], ['name' => 'file2.txt']],
])
->willReturn([
'name' => $destinationObject,
'generation' => 1
])
->shouldBeCalledTimes(1);
$bucket = $this->getBucket();

$object = $bucket->compose(['file1.txt', 'file2.txt'], $destinationObject, [
'predefinedAcl' => $acl,
'deleteSourceObjects' => false
]);

$this->assertEquals($destinationObject, $object->name());
}

public function testComposeWithDeleteSourceObjectsNull()
{
$acl = 'private';
$destinationObject = 'combined-files.txt';
$this->connection->composeObject([
'destinationBucket' => self::BUCKET_NAME,
'destinationObject' => $destinationObject,
'destinationPredefinedAcl' => $acl,
'destination' => ['contentType' => 'text/plain'],
'sourceObjects' => [['name' => 'file1.txt'], ['name' => 'file2.txt']],
])
->willReturn([
'name' => $destinationObject,
'generation' => 1
])
->shouldBeCalledTimes(1);
$bucket = $this->getBucket();

$object = $bucket->compose(['file1.txt', 'file2.txt'], $destinationObject, [
'predefinedAcl' => $acl,
'deleteSourceObjects' => null
]);

$this->assertEquals($destinationObject, $object->name());
}

public function composeProvider()
{
$object1 = $this->prophesize(StorageObject::class);
Expand Down
Loading