From 10ac1f6b7167b0e8022731b6b19b05601b0512d8 Mon Sep 17 00:00:00 2001 From: Alexander Dmitryuk Date: Mon, 29 Aug 2022 17:03:38 +0700 Subject: [PATCH] AmazonMetadataBuilder - Pass content-length header (#2333) * Pass content-length header * Update src/Metadata/AmazonMetadataBuilder.php Co-authored-by: Vincent Langlet * phpstan * Update src/Metadata/AmazonMetadataBuilder.php Co-authored-by: Vincent Langlet * cs-fixer Co-authored-by: a.dmitryuk Co-authored-by: Vincent Langlet --- src/Metadata/AmazonMetadataBuilder.php | 18 +++++++++++++++- tests/Metadata/AmazonMetadataBuilderTest.php | 22 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Metadata/AmazonMetadataBuilder.php b/src/Metadata/AmazonMetadataBuilder.php index 993974333..2410831eb 100644 --- a/src/Metadata/AmazonMetadataBuilder.php +++ b/src/Metadata/AmazonMetadataBuilder.php @@ -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) ); } @@ -124,4 +125,19 @@ private function getContentType(string $filename): array return ['contentType' => $mimeType]; } + + /** + * @return array + * + * @phpstan-return array{contentLength?: int} + */ + private function getContentLength(MediaInterface $media): array + { + $size = $media->getSize(); + if ($size > 0) { + return ['contentLength' => $size]; + } + + return []; + } } diff --git a/tests/Metadata/AmazonMetadataBuilderTest.php b/tests/Metadata/AmazonMetadataBuilderTest.php index e6cd7ca0d..87b9377b6 100644 --- a/tests/Metadata/AmazonMetadataBuilderTest.php +++ b/tests/Metadata/AmazonMetadataBuilderTest.php @@ -26,11 +26,12 @@ final class AmazonMetadataBuilderTest extends TestCase /** * @dataProvider provider * - * @param array $expected + * @param array $mediaAttributes + * @param array $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( @@ -38,6 +39,9 @@ public function testAmazon(array $settings, array $expected): void ); $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); @@ -57,6 +61,9 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => [], ], + [ + 'size' => 3000, + ], [ 'ACL' => AmazonMetadataBuilder::PRIVATE_ACCESS, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -64,6 +71,7 @@ public function provider(): iterable 'CacheControl' => '', 'encryption' => 'AES256', 'contentType' => 'image/png', + 'contentLength' => 3000, ], ]; @@ -75,6 +83,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ_WRITE, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -93,6 +102,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::AUTHENTICATED_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -111,6 +121,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::BUCKET_OWNER_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -129,6 +140,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::BUCKET_OWNER_FULL_CONTROL, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -147,6 +159,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_REDUCED, @@ -165,6 +178,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -183,6 +197,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -201,6 +216,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -219,6 +235,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD, @@ -237,6 +254,7 @@ public function provider(): iterable 'encryption' => 'aes256', 'meta' => ['key' => 'value'], ], + [], [ 'ACL' => AmazonMetadataBuilder::PUBLIC_READ, 'storage' => AmazonMetadataBuilder::STORAGE_STANDARD,