diff --git a/src/Libraries/Storage/Storage.php b/src/Libraries/Storage/Storage.php index 2fc224c..420a5c5 100644 --- a/src/Libraries/Storage/Storage.php +++ b/src/Libraries/Storage/Storage.php @@ -167,14 +167,15 @@ public function exists(string $filepath): bool /** * Upload a file to the resolved driver. * - * @param UploadedFile $file file from http request - * @param string $dirpath full destination path including prefix + * @param UploadedFile $file file from http request + * @param string $dirpath full destination path including prefix + * @param string $filename optionally override file name * * @throws RuntimeException * * @return StorageResult */ - public function upload(UploadedFile $file, string $dirpath): StorageResult + public function upload(UploadedFile $file, string $dirpath, string $filename = ''): StorageResult { try { if ($this->pendingAutoDetect) { @@ -188,7 +189,7 @@ public function upload(UploadedFile $file, string $dirpath): StorageResult ? $resolver->resolveExplicit($this->pendingDriver) : $resolver->resolveDefault(); - return $this->makeDriver($resolvedDriverName)->upload($file, $dirpath); + return $this->makeDriver($resolvedDriverName)->upload($file, $dirpath, $filename); } finally { $this->pendingDriver = null; $this->pendingAutoDetect = false; diff --git a/tests/Libraries/Storage/StorageTest.php b/tests/Libraries/Storage/StorageTest.php index c36c1cb..2833c2a 100644 --- a/tests/Libraries/Storage/StorageTest.php +++ b/tests/Libraries/Storage/StorageTest.php @@ -46,6 +46,21 @@ public function testUploadExplicitDriver(): void $this->assertEquals(Storage::MINIO, $result->driver); } + public function testUploadExplicitDriverWithFilenameOverride(): void + { + $diskMock = Mockery::mock(); + LaravelStorage::shouldReceive('disk')->with('minio')->andReturn($diskMock); + $diskMock->shouldReceive('put')->once()->andReturn(true); + + $fileMock = Mockery::mock(UploadedFile::class); + $fileMock->shouldReceive('getClientOriginalName')->andReturn('test.txt'); + $fileMock->shouldReceive('getContent')->andReturn('content'); + + $storage = new Storage(); + $result = $storage->driver(Storage::MINIO)->upload($fileMock, '/tmp', 'custom.txt'); + $this->assertEquals(Storage::MINIO, $result->driver); + } + public function testUploadAutoDetectFails(): void { $storage = new Storage();