diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 2b7bedeb..22c5c161 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -2,7 +2,9 @@ use DirectoryIterator; 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; @@ -528,4 +530,67 @@ protected function findSymlinks(): void $this->symlinks = $symlinks; } + + /** + * Copies a file from one storage disk to another. + * + * @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|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool + { + // Resolve source disk + if (is_string($sourceDisk)) { + $sourceDisk = Storage::disk($sourceDisk); + } + + // Resolve destination disk + if (is_string($destinationDisk)) { + $destinationDisk = Storage::disk($destinationDisk); + } + + // Open a read stream from the source disk + $readStream = $sourceDisk->readStream($filePath); + if (!$readStream) { + return false; + } + + // Write the stream to the destination disk + $result = $destinationDisk->put($targetPath ?? $filePath, $readStream); + + // Close the read stream + if (is_resource($readStream)) { + fclose($readStream); + } + + return $result; + } + + /** + * Moves a file from one storage disk to another. + * + * @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|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null): bool + { + $copied = $this->copyBetweenDisks($sourceDisk, $destinationDisk, $filePath, $targetPath); + + if ($copied) { + // Delete the original file from the source disk + if (is_string($sourceDisk)) { + $sourceDisk = Storage::disk($sourceDisk); + } + + return $sourceDisk->delete($filePath); + } + + return false; + } } diff --git a/src/Support/Facades/File.php b/src/Support/Facades/File.php index 4442c87f..df56aac4 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|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 */