From dfa7bdb44234174be820be5e5c311d78ebb2e2f4 Mon Sep 17 00:00:00 2001 From: Bryce Newbury Date: Thu, 14 Nov 2024 01:41:27 +0000 Subject: [PATCH 1/2] Fix S3 filenames that include a hash --- src/Models/S3File.php | 4 +++- tests/FileTest.php | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Models/S3File.php b/src/Models/S3File.php index fe99937..aba734b 100644 --- a/src/Models/S3File.php +++ b/src/Models/S3File.php @@ -46,7 +46,9 @@ public function getBucket(): string public function getKey(): string { - return ltrim(parse_url($this->getSource(), PHP_URL_PATH), "/"); + $s3AndBucketLength = strlen("s3://{$this->getBucket()}/"); + $key = substr($this->getSource(), $s3AndBucketLength); + return ltrim($key); } protected function buildReadableStream(): StreamInterface diff --git a/tests/FileTest.php b/tests/FileTest.php index d73a9bd..ba12fcb 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -117,4 +117,11 @@ public function testFromS3Disk() $this->assertInstanceOf(S3File::class, $file); $this->assertEquals('s3://my-test-bucket/my-prefix/test.txt', $file->getSource()); } + + public function testS3HashFilename() + { + $file = S3File::make('s3://my-test-bucket/file with hash #.txt'); + $this->assertEquals('file with hash #.txt', $file->getKey()); + $this->assertEquals('my-test-bucket', $file->getBucket()); + } } From cd44eb92a250cc78391c256d6fc2abb62a0b1adb Mon Sep 17 00:00:00 2001 From: Bryce Newbury Date: Sat, 23 Nov 2024 02:29:54 +0000 Subject: [PATCH 2/2] Use AWS stream wrapper to get S3 file sizes --- src/Models/S3File.php | 17 +---------------- tests/FileTest.php | 7 ------- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/Models/S3File.php b/src/Models/S3File.php index aba734b..adee382 100644 --- a/src/Models/S3File.php +++ b/src/Models/S3File.php @@ -17,10 +17,7 @@ class S3File extends File public function calculateFilesize(): int { - return $this->getS3Client()->headObject([ - 'Bucket' => $this->getBucket(), - 'Key' => $this->getKey() - ])->get('ContentLength'); + return $this->getReadableStream()->getSize(); } public function setS3Client(S3Client $client): self @@ -39,18 +36,6 @@ public function getS3Client(): S3Client return $this->client; } - public function getBucket(): string - { - return parse_url($this->getSource(), PHP_URL_HOST); - } - - public function getKey(): string - { - $s3AndBucketLength = strlen("s3://{$this->getBucket()}/"); - $key = substr($this->getSource(), $s3AndBucketLength); - return ltrim($key); - } - protected function buildReadableStream(): StreamInterface { $this->getS3Client()->registerStreamWrapper(); diff --git a/tests/FileTest.php b/tests/FileTest.php index ba12fcb..d73a9bd 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -117,11 +117,4 @@ public function testFromS3Disk() $this->assertInstanceOf(S3File::class, $file); $this->assertEquals('s3://my-test-bucket/my-prefix/test.txt', $file->getSource()); } - - public function testS3HashFilename() - { - $file = S3File::make('s3://my-test-bucket/file with hash #.txt'); - $this->assertEquals('file with hash #.txt', $file->getKey()); - $this->assertEquals('my-test-bucket', $file->getBucket()); - } }