From dfa7bdb44234174be820be5e5c311d78ebb2e2f4 Mon Sep 17 00:00:00 2001 From: Bryce Newbury Date: Thu, 14 Nov 2024 01:41:27 +0000 Subject: [PATCH] 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()); + } }