From b961b5e41895a6a7eec335d20d2e788e58dc28bc Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:12:57 -0600 Subject: [PATCH 1/9] Add File::copyBetweenDisks() and File::moveBetweenDisks() --- src/Filesystem/Filesystem.php | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 2b7bedeb..5155013a 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -2,6 +2,7 @@ use DirectoryIterator; use FilesystemIterator; +use Illuminate\Support\Facades\Storage; use Illuminate\Filesystem\Filesystem as FilesystemBase; use InvalidArgumentException; use ReflectionClass; @@ -528,4 +529,57 @@ protected function findSymlinks(): void $this->symlinks = $symlinks; } + + /** + * Copies a file from one storage disk to another. + * + * @param string $sourceDisk The name of the source disk. + * @param string $destinationDisk The name of the destination disk. + * @param string $filePath The path to the file on the source disk. + * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. + * @return bool Returns true if the file was copied successfully, false otherwise. + */ + public function copyBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string $targetPath = null): bool + { + $targetPath = $targetPath ?? $filePath; + + // Open a read stream from the source disk + $readStream = Storage::disk($sourceDisk)->readStream($filePath); + + if ($readStream === false) { + // Handle the error (e.g., file not found on source disk) + return false; + } + + // Write the stream to the destination disk + $result = Storage::disk($destinationDisk)->put($targetPath, $readStream); + + // Close the read stream + if (is_resource($readStream)) { + fclose($readStream); + } + + return $result; + } + + /** + * Moves a file from one storage disk to another. + * + * @param string $sourceDisk The name of the source disk. + * @param string $destinationDisk The name of the destination disk. + * @param string $filePath The path to the file on the source disk. + * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. + * @return bool Returns true if the file was moved successfully, false otherwise. + */ + public function moveBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string $targetPath = null): bool + { + $copied = $this->copyBetweenDisks($sourceDisk, $destinationDisk, $filePath, $targetPath); + + if ($copied) { + // Delete the original file from the source disk + return Storage::disk($sourceDisk)->delete($filePath); + } + + return false; + } } From 59f947004cb29347e769f915919addecc9ca1a7b Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:14:17 -0600 Subject: [PATCH 2/9] Update facade --- src/Support/Facades/File.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Support/Facades/File.php b/src/Support/Facades/File.php index 4442c87f..9bcc5db7 100644 --- a/src/Support/Facades/File.php +++ b/src/Support/Facades/File.php @@ -53,6 +53,8 @@ * @method static int|float|null getFilePermissions() * @method static int|float|null getFolderPermissions() * @method static bool fileNameMatch(string $fileName, string $pattern) + * @method static bool copyBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string|null $targetPath = null) + * @method static bool moveBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string|null $targetPath = null) * * @see \Winter\Storm\Filesystem\Filesystem */ From c8854fd60a36cfa18a3ee2b00e1e3cff41bb9b0b Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:21:02 -0600 Subject: [PATCH 3/9] Support passing a string or an instantiated disk --- src/Filesystem/Filesystem.php | 48 +++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 5155013a..ef246b10 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -4,6 +4,7 @@ use FilesystemIterator; use Illuminate\Support\Facades\Storage; use Illuminate\Filesystem\Filesystem as FilesystemBase; +use Illuminate\Filesystem\FilesystemAdapter; use InvalidArgumentException; use ReflectionClass; use Winter\Storm\Support\Facades\Config; @@ -533,18 +534,32 @@ protected function findSymlinks(): void /** * Copies a file from one storage disk to another. * - * @param string $sourceDisk The name of the source disk. - * @param string $destinationDisk The name of the destination disk. - * @param string $filePath The path to the file on the source disk. - * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. + * @param string|FilesystemAdapter $sourceDisk The source disk name or instance. + * @param string|FilesystemAdapter $destinationDisk The destination disk name or instance. + * @param string $filePath The path to the file on the source disk. + * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. * @return bool Returns true if the file was copied successfully, false otherwise. */ - public function copyBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string $targetPath = null): bool + public function copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool { $targetPath = $targetPath ?? $filePath; + // Resolve source disk + if (is_string($sourceDisk)) { + $source = Storage::disk($sourceDisk); + } else { + $source = $sourceDisk; + } + + // Resolve destination disk + if (is_string($destinationDisk)) { + $destination = Storage::disk($destinationDisk); + } else { + $destination = $destinationDisk; + } + // Open a read stream from the source disk - $readStream = Storage::disk($sourceDisk)->readStream($filePath); + $readStream = $source->readStream($filePath); if ($readStream === false) { // Handle the error (e.g., file not found on source disk) @@ -552,7 +567,7 @@ public function copyBetweenDisks(string $sourceDisk, string $destinationDisk, st } // Write the stream to the destination disk - $result = Storage::disk($destinationDisk)->put($targetPath, $readStream); + $result = $destination->put($targetPath, $readStream); // Close the read stream if (is_resource($readStream)) { @@ -565,19 +580,26 @@ public function copyBetweenDisks(string $sourceDisk, string $destinationDisk, st /** * Moves a file from one storage disk to another. * - * @param string $sourceDisk The name of the source disk. - * @param string $destinationDisk The name of the destination disk. - * @param string $filePath The path to the file on the source disk. - * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. + * @param string|FilesystemAdapter $sourceDisk The source disk name or instance. + * @param string|FilesystemAdapter $destinationDisk The destination disk name or instance. + * @param string $filePath The path to the file on the source disk. + * @param string|null $targetPath The path to the file on the destination disk. If null, uses the same as $filePath. * @return bool Returns true if the file was moved successfully, false otherwise. */ - public function moveBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string $targetPath = null): bool + public function moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool { $copied = $this->copyBetweenDisks($sourceDisk, $destinationDisk, $filePath, $targetPath); if ($copied) { + // Resolve source disk + if (is_string($sourceDisk)) { + $source = Storage::disk($sourceDisk); + } else { + $source = $sourceDisk; + } + // Delete the original file from the source disk - return Storage::disk($sourceDisk)->delete($filePath); + return $source->delete($filePath); } return false; From df4301cb50d8a28cefd8aaaa7338b2476dd92893 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:22:04 -0600 Subject: [PATCH 4/9] Update File.php --- src/Support/Facades/File.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Support/Facades/File.php b/src/Support/Facades/File.php index 9bcc5db7..df56aac4 100644 --- a/src/Support/Facades/File.php +++ b/src/Support/Facades/File.php @@ -53,8 +53,8 @@ * @method static int|float|null getFilePermissions() * @method static int|float|null getFolderPermissions() * @method static bool fileNameMatch(string $fileName, string $pattern) - * @method static bool copyBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string|null $targetPath = null) - * @method static bool moveBetweenDisks(string $sourceDisk, string $destinationDisk, string $filePath, string|null $targetPath = null) + * @method static bool copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null) + * @method static bool moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null) * * @see \Winter\Storm\Filesystem\Filesystem */ From 0423341d2161f4610fe7063d6fb0ce3cbaf780c0 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:25:27 -0600 Subject: [PATCH 5/9] Make less bad --- src/Filesystem/Filesystem.php | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index ef246b10..d18fad01 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -546,28 +546,23 @@ public function copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi // Resolve source disk if (is_string($sourceDisk)) { - $source = Storage::disk($sourceDisk); - } else { - $source = $sourceDisk; + $sourceDisk = Storage::disk($sourceDisk); } // Resolve destination disk if (is_string($destinationDisk)) { - $destination = Storage::disk($destinationDisk); - } else { - $destination = $destinationDisk; + $destinationDisk = Storage::disk($destinationDisk); } // Open a read stream from the source disk - $readStream = $source->readStream($filePath); - - if ($readStream === false) { + $readStream = $sourceDisk->readStream($filePath); + if (!$readStream) { // Handle the error (e.g., file not found on source disk) return false; } // Write the stream to the destination disk - $result = $destination->put($targetPath, $readStream); + $result = $destinationDisk->put($targetPath, $readStream); // Close the read stream if (is_resource($readStream)) { @@ -593,13 +588,11 @@ public function moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi if ($copied) { // Resolve source disk if (is_string($sourceDisk)) { - $source = Storage::disk($sourceDisk); - } else { - $source = $sourceDisk; + $sourceDisk = Storage::disk($sourceDisk); } // Delete the original file from the source disk - return $source->delete($filePath); + return $sourceDisk->delete($filePath); } return false; From a517ecf2b1512129179f630da375f475edae2af2 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:29:16 -0600 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Jack Wilkinson <31214002+jaxwilko@users.noreply.github.com> --- src/Filesystem/Filesystem.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index d18fad01..734145ec 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -542,7 +542,6 @@ protected function findSymlinks(): void */ public function copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool { - $targetPath = $targetPath ?? $filePath; // Resolve source disk if (is_string($sourceDisk)) { @@ -557,12 +556,11 @@ public function copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi // Open a read stream from the source disk $readStream = $sourceDisk->readStream($filePath); if (!$readStream) { - // Handle the error (e.g., file not found on source disk) return false; } // Write the stream to the destination disk - $result = $destinationDisk->put($targetPath, $readStream); + $result = $destinationDisk->put($targetPath ?? $filePath, $readStream); // Close the read stream if (is_resource($readStream)) { @@ -586,13 +584,8 @@ public function moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi $copied = $this->copyBetweenDisks($sourceDisk, $destinationDisk, $filePath, $targetPath); if ($copied) { - // Resolve source disk - if (is_string($sourceDisk)) { - $sourceDisk = Storage::disk($sourceDisk); - } - // Delete the original file from the source disk - return $sourceDisk->delete($filePath); + return (is_string($sourceDisk) ? Storage::disk($sourceDisk) : $sourceDisk)->delete($filePath); } return false; From ba00625bdab3fb477312ecb00accc458054c16a8 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 29 Nov 2024 10:29:29 -0600 Subject: [PATCH 7/9] Update src/Filesystem/Filesystem.php --- src/Filesystem/Filesystem.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 734145ec..46a52400 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -542,7 +542,6 @@ protected function findSymlinks(): void */ public function copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool { - // Resolve source disk if (is_string($sourceDisk)) { $sourceDisk = Storage::disk($sourceDisk); From e69566a5f3dbf445c5d4ac5cfb7b301204e07a88 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson <31214002+jaxwilko@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:31:24 +0000 Subject: [PATCH 8/9] Update src/Filesystem/Filesystem.php --- src/Filesystem/Filesystem.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 46a52400..156d3e33 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -584,7 +584,11 @@ public function moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi if ($copied) { // Delete the original file from the source disk - return (is_string($sourceDisk) ? Storage::disk($sourceDisk) : $sourceDisk)->delete($filePath); + if (is_string($sourceDisk) { + $sourceDisk = Storage::disk($sourceDisk); + } + + return $sourceDisk->delete($filePath); } return false; From 63401e7eea2dfae2780136a6b0e2c9e5daca8e73 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson <31214002+jaxwilko@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:34:30 +0000 Subject: [PATCH 9/9] Update src/Filesystem/Filesystem.php --- src/Filesystem/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 156d3e33..22c5c161 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -584,7 +584,7 @@ public function moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|Fi if ($copied) { // Delete the original file from the source disk - if (is_string($sourceDisk) { + if (is_string($sourceDisk)) { $sourceDisk = Storage::disk($sourceDisk); }