From a290edf75660817ca71d8245ccc8744c5a05fa4c Mon Sep 17 00:00:00 2001 From: Sjors Ottjes Date: Tue, 24 Dec 2019 15:19:45 +0100 Subject: [PATCH 1/3] Update FilesystemAdapter.php --- src/Illuminate/Filesystem/FilesystemAdapter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index bd79e7feec55..c25f6efe5ab5 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -248,6 +248,8 @@ public function putFile($path, $file, $options = []) */ public function putFileAs($path, $file, $name, $options = []) { + // TODO: + $stream = fopen($file->getRealPath(), 'r'); // Next, we will format the path of the file and store the file using a stream since From 3c3af2d3bb7976876f8fb544078c41d106ab2b7b Mon Sep 17 00:00:00 2001 From: Sjors Date: Tue, 24 Dec 2019 15:38:32 +0100 Subject: [PATCH 2/3] allow absolute file path for Storage::putFileAs --- .../Filesystem/FilesystemAdapter.php | 8 ++--- tests/Filesystem/FilesystemAdapterTest.php | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index c25f6efe5ab5..e83c8127a5b2 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -241,16 +241,16 @@ public function putFile($path, $file, $options = []) * Store the uploaded file on the disk with a given name. * * @param string $path - * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file + * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file * @param string $name * @param array $options * @return string|false */ public function putFileAs($path, $file, $name, $options = []) { - // TODO: - - $stream = fopen($file->getRealPath(), 'r'); + $filePath = is_string($file) ? $file : $file->getRealPath(); + + $stream = fopen($filePath, 'r'); // Next, we will format the path of the file and store the file using a stream since // they provide better performance than alternatives. Once we write the file this diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 9e1df65ec37e..7143ff7d1207 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Filesystem\FileExistsException; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Http\UploadedFile; use InvalidArgumentException; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; @@ -240,4 +241,34 @@ public function testPutWithStreamInterface() $spy->shouldHaveReceived('putStream'); $this->assertEquals('some-data', $filesystemAdapter->get('bar.txt')); } + + public function testPutFile() + { + file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content'); + + $filesystemAdapter = new FilesystemAdapter($this->filesystem); + + $uploadedFile = new UploadedFile($filePath, 'org.txt', null, null, true); + + $storagePath = $filesystemAdapter->putFile('/', $uploadedFile); + + $this->assertFileExists($filePath); + + $filesystemAdapter->assertExists($storagePath); + + $this->assertSame('uploaded file content', $filesystemAdapter->read($storagePath)); + } + + public function testPutFileWithAbsoluteFilePath() + { + file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content'); + + $filesystemAdapter = new FilesystemAdapter($this->filesystem); + + $storagePath = $filesystemAdapter->putFileAs('/', $filePath, 'new.txt'); + + $this->assertSame('new.txt', $storagePath); + + $this->assertSame('normal file content', $filesystemAdapter->read($storagePath)); + } } From 923170a9361a300a74ad6e5d3f08c66b7df883f1 Mon Sep 17 00:00:00 2001 From: Sjors Date: Tue, 24 Dec 2019 15:56:37 +0100 Subject: [PATCH 3/3] update test --- tests/Filesystem/FilesystemAdapterTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 7143ff7d1207..5638aa4f0624 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -242,7 +242,7 @@ public function testPutWithStreamInterface() $this->assertEquals('some-data', $filesystemAdapter->get('bar.txt')); } - public function testPutFile() + public function testPutFileAs() { file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content'); @@ -250,7 +250,9 @@ public function testPutFile() $uploadedFile = new UploadedFile($filePath, 'org.txt', null, null, true); - $storagePath = $filesystemAdapter->putFile('/', $uploadedFile); + $storagePath = $filesystemAdapter->putFileAs('/', $uploadedFile, 'new.txt'); + + $this->assertSame('new.txt', $storagePath); $this->assertFileExists($filePath); @@ -259,7 +261,7 @@ public function testPutFile() $this->assertSame('uploaded file content', $filesystemAdapter->read($storagePath)); } - public function testPutFileWithAbsoluteFilePath() + public function testPutFileAsWithAbsoluteFilePath() { file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content'); @@ -267,8 +269,6 @@ public function testPutFileWithAbsoluteFilePath() $storagePath = $filesystemAdapter->putFileAs('/', $filePath, 'new.txt'); - $this->assertSame('new.txt', $storagePath); - $this->assertSame('normal file content', $filesystemAdapter->read($storagePath)); } }