diff --git a/src/Libraries/Storage/Drivers/MinioDriver.php b/src/Libraries/Storage/Drivers/MinioDriver.php index f202f0b..20f72b8 100644 --- a/src/Libraries/Storage/Drivers/MinioDriver.php +++ b/src/Libraries/Storage/Drivers/MinioDriver.php @@ -51,17 +51,18 @@ public function __construct(string $disk = 'minio') /** * Upload a file to MinIO. * - * @param UploadedFile $file file to upload - * @param string $dirpath destination directory path + * @param UploadedFile $file file to upload + * @param string $dirpath destination directory path + * @param string $filename optionally override file name * * @throws RuntimeException when the upload fails * * @return StorageResult upload result */ - public function upload(UploadedFile $file, string $dirpath): StorageResult + public function upload(UploadedFile $file, string $dirpath, string $filename = ''): StorageResult { - $filename = $file->getClientOriginalName(); - $destPath = rtrim($dirpath, '/') . '/' . $filename; + $fileName = $filename === '' ? $file->getClientOriginalName() : $filename; + $destPath = rtrim($dirpath, '/') . '/' . $fileName; if (!LaravelStorage::disk($this->disk)->put($destPath, $file->getContent())) { throw new RuntimeException("Failed to upload file to MinIO: {$destPath}"); @@ -71,7 +72,7 @@ public function upload(UploadedFile $file, string $dirpath): StorageResult $result->driver = $this->disk === 'minio_brimen' ? StorageManager::MINIO_BRIMEN : StorageManager::MINIO; - $result->pathFile = $filename; + $result->pathFile = $fileName; $result->fullPath = $destPath; $result->folder = rtrim($dirpath, '/') . '/'; diff --git a/src/Libraries/Storage/Drivers/NfsDriver.php b/src/Libraries/Storage/Drivers/NfsDriver.php index 88dc868..c074cda 100644 --- a/src/Libraries/Storage/Drivers/NfsDriver.php +++ b/src/Libraries/Storage/Drivers/NfsDriver.php @@ -68,26 +68,27 @@ private function resolvePath(string $path): string /** * Upload a file to an NFS directory. * - * @param UploadedFile $file file to upload - * @param string $dirpath destination directory path + * @param UploadedFile $file file to upload + * @param string $dirpath destination directory path + * @param string $filename optionally override file name * * @throws RuntimeException when the destination directory cannot be created * * @return StorageResult upload result */ - public function upload(UploadedFile $file, string $dirpath): StorageResult + public function upload(UploadedFile $file, string $dirpath, string $filename = ''): StorageResult { if (!is_dir($dirpath) && !mkdir($dirpath, 0755, true)) { throw new RuntimeException("Failed to create destination directory: {$dirpath}"); } - $filename = $file->getClientOriginalName(); - $file->move($dirpath, $filename); + $fileName = $filename === '' ? $file->getClientOriginalName() : $filename; + $file->move($dirpath, $fileName); $result = new StorageResult(); $result->driver = Storage::NFS; - $result->pathFile = $filename; - $result->fullPath = rtrim($dirpath, '/') . '/' . $filename; + $result->pathFile = $fileName; + $result->fullPath = rtrim($dirpath, '/') . '/' . $fileName; $result->folder = rtrim($dirpath, '/') . '/'; return $result; diff --git a/src/Libraries/Storage/StorageDriverInterface.php b/src/Libraries/Storage/StorageDriverInterface.php index 37f7e5a..cf44c39 100644 --- a/src/Libraries/Storage/StorageDriverInterface.php +++ b/src/Libraries/Storage/StorageDriverInterface.php @@ -34,14 +34,15 @@ interface StorageDriverInterface /** * Upload a file to the storage driver. * - * @param UploadedFile $file file to upload - * @param string $dirpath destination directory path + * @param UploadedFile $file file to upload + * @param string $dirpath destination directory path + * @param string $filename optionally override file name * * @throws RuntimeException when the upload fails * * @return StorageResult upload result */ - public function upload(UploadedFile $file, string $dirpath): StorageResult; + public function upload(UploadedFile $file, string $dirpath, string $filename = ''): StorageResult; /** * Copy a file within the same storage driver. diff --git a/tests/Libraries/Storage/Drivers/MinioDriverTest.php b/tests/Libraries/Storage/Drivers/MinioDriverTest.php index 3bb0484..2250800 100644 --- a/tests/Libraries/Storage/Drivers/MinioDriverTest.php +++ b/tests/Libraries/Storage/Drivers/MinioDriverTest.php @@ -45,6 +45,25 @@ public function testUploadSuccess(): void $this->assertEquals('/tmp/dir/test.txt', $result->fullPath); } + public function testUploadSuccessWithFilenameOverride(): void + { + $diskMock = Mockery::mock(); + LaravelStorage::shouldReceive('disk')->with('minio')->andReturn($diskMock); + + $diskMock->shouldReceive('put')->with('/tmp/dir/custom.txt', 'content')->once()->andReturn(true); + + $fileMock = Mockery::mock(UploadedFile::class); + $fileMock->shouldReceive('getClientOriginalName')->andReturn('test.txt'); + $fileMock->shouldReceive('getContent')->andReturn('content'); + + $driver = new MinioDriver(); + $result = $driver->upload($fileMock, '/tmp/dir', 'custom.txt'); + + $this->assertEquals(StorageManager::MINIO, $result->driver); + $this->assertEquals('custom.txt', $result->pathFile); + $this->assertEquals('/tmp/dir/custom.txt', $result->fullPath); + } + public function testUploadFailure(): void { $diskMock = Mockery::mock(); diff --git a/tests/Libraries/Storage/Drivers/NfsDriverTest.php b/tests/Libraries/Storage/Drivers/NfsDriverTest.php index 245d926..cbfe8b4 100644 --- a/tests/Libraries/Storage/Drivers/NfsDriverTest.php +++ b/tests/Libraries/Storage/Drivers/NfsDriverTest.php @@ -62,6 +62,19 @@ public function testUpload(): void $this->assertEquals('test.txt', $result->pathFile); } + public function testUploadWithFilenameOverride(): void + { + $fileMock = Mockery::mock(UploadedFile::class); + $fileMock->shouldReceive('getClientOriginalName')->andReturn('test.txt'); + $fileMock->shouldReceive('move')->with($this->tempDir . '/uploads', 'custom.txt')->once(); + + $driver = new NfsDriver(); + $result = $driver->upload($fileMock, $this->tempDir . '/uploads', 'custom.txt'); + + $this->assertEquals(Storage::NFS, $result->driver); + $this->assertEquals('custom.txt', $result->pathFile); + } + public function testWriteStream(): void { $driver = new NfsDriver();