Skip to content

Commit

Permalink
AmazonMetadataBuilder - Pass content-length header (#2333)
Browse files Browse the repository at this point in the history
* Pass content-length header

* Update src/Metadata/AmazonMetadataBuilder.php

Co-authored-by: Vincent Langlet <[email protected]>

* phpstan

* Update src/Metadata/AmazonMetadataBuilder.php

Co-authored-by: Vincent Langlet <[email protected]>

* cs-fixer

Co-authored-by: a.dmitryuk <[email protected]>
Co-authored-by: Vincent Langlet <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2022
1 parent ad1e574 commit 10ac1f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/Metadata/AmazonMetadataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function get(MediaInterface $media, string $filename): array
{
return array_replace_recursive(
$this->getDefaultMetadata(),
$this->getContentType($filename)
$this->getContentType($filename),
$this->getContentLength($media)
);
}

Expand Down Expand Up @@ -124,4 +125,19 @@ private function getContentType(string $filename): array

return ['contentType' => $mimeType];
}

/**
* @return array<string, int>
*
* @phpstan-return array{contentLength?: int}
*/
private function getContentLength(MediaInterface $media): array
{
$size = $media->getSize();
if ($size > 0) {
return ['contentLength' => $size];
}

return [];
}
}
22 changes: 20 additions & 2 deletions tests/Metadata/AmazonMetadataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ final class AmazonMetadataBuilderTest extends TestCase
/**
* @dataProvider provider
*
* @param array<string, mixed> $expected
* @param array<string, string|int> $mediaAttributes
* @param array<string, mixed> $expected
*
* @phpstan-param AmazonSettings $settings
*/
public function testAmazon(array $settings, array $expected): void
public function testAmazon(array $settings, array $mediaAttributes, array $expected): void
{
$mimeTypes = $this->createStub(MimeTypesInterface::class);
$mimeTypes->method('getMimeTypes')->willReturnCallback(
static fn (string $ext): array => 'png' === $ext ? ['image/png'] : []
);

$media = $this->createStub(MediaInterface::class);
foreach ($mediaAttributes as $attribute => $value) {
$media->method('get'.ucfirst($attribute))->willReturn($value);
}
$filename = '/test/folder/testfile.png';

$amazonmetadatabuilder = new AmazonMetadataBuilder($settings, $mimeTypes);
Expand All @@ -57,13 +61,17 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => [],
],
[
'size' => 3000,
],
[
'ACL' => AmazonMetadataBuilder::PRIVATE_ACCESS,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
'meta' => [],
'CacheControl' => '',
'encryption' => 'AES256',
'contentType' => 'image/png',
'contentLength' => 3000,
],
];

Expand All @@ -75,6 +83,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ_WRITE,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -93,6 +102,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::AUTHENTICATED_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -111,6 +121,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::BUCKET_OWNER_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -129,6 +140,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::BUCKET_OWNER_FULL_CONTROL,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -147,6 +159,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_REDUCED,
Expand All @@ -165,6 +178,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -183,6 +197,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -201,6 +216,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -219,6 +235,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand All @@ -237,6 +254,7 @@ public function provider(): iterable
'encryption' => 'aes256',
'meta' => ['key' => 'value'],
],
[],
[
'ACL' => AmazonMetadataBuilder::PUBLIC_READ,
'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,
Expand Down

0 comments on commit 10ac1f6

Please sign in to comment.