diff --git a/src/HasMedia.php b/src/HasMedia.php index 53bcd90e9..e10041144 100644 --- a/src/HasMedia.php +++ b/src/HasMedia.php @@ -21,6 +21,16 @@ public function media(): MorphMany; */ public function addMedia($file): FileAdder; + /** + * Add a file from the given disk. + * + * @param string $key + * @param string $disk + * + * @return \Spatie\MediaLibrary\MediaCollections\FileAdder + */ + public function addMediaFromDisk(string $key, string $disk = null): FileAdder; + /** * Copy a file to the media library. * diff --git a/src/MediaCollections/Filesystem.php b/src/MediaCollections/Filesystem.php index 9727421d3..dcfd71195 100644 --- a/src/MediaCollections/Filesystem.php +++ b/src/MediaCollections/Filesystem.php @@ -61,6 +61,13 @@ public function copyToMediaLibraryFromRemote(RemoteFile $file, Media $media, ?st $storage = Storage::disk($file->getDisk()); + if ($file->getDisk() == $media->disk) { + $storage->move($file->getKey(), $destination); + + return; + } + + $headers = $diskDriverName === 'local' ? [] : $this->getRemoteHeadersForFile( diff --git a/src/MediaCollections/Models/Media.php b/src/MediaCollections/Models/Media.php index f535f732c..7be2b2320 100644 --- a/src/MediaCollections/Models/Media.php +++ b/src/MediaCollections/Models/Media.php @@ -24,7 +24,6 @@ use Spatie\MediaLibrary\ResponsiveImages\RegisteredResponsiveImages; use Spatie\MediaLibrary\Support\File; use Spatie\MediaLibrary\Support\MediaLibraryPro; -use Spatie\MediaLibrary\Support\TemporaryDirectory; use Spatie\MediaLibrary\Support\UrlGenerator\UrlGeneratorFactory; use Spatie\MediaLibraryPro\Models\TemporaryUpload; @@ -286,17 +285,13 @@ public function move(HasMedia $model, $collectionName = 'default', string $diskN public function copy(HasMedia $model, $collectionName = 'default', string $diskName = '', string $fileName = ''): self { - $temporaryDirectory = TemporaryDirectory::create(); - - $temporaryFile = $temporaryDirectory->path('/') . DIRECTORY_SEPARATOR . $this->file_name; - /** @var \Spatie\MediaLibrary\MediaCollections\Filesystem $filesystem */ $filesystem = app(Filesystem::class); - $filesystem->copyFromMediaLibrary($this, $temporaryFile); + $path = $filesystem->getMediaDirectory($this) . '/' . $this->file_name; $fileAdder = $model - ->addMedia($temporaryFile) + ->addMediaFromDisk($path, $this->disk) ->usingName($this->name) ->setOrder($this->order_column) ->withCustomProperties($this->custom_properties); @@ -308,8 +303,6 @@ public function copy(HasMedia $model, $collectionName = 'default', string $diskN $newMedia = $fileAdder ->toMediaCollection($collectionName, $diskName); - $temporaryDirectory->delete(); - return $newMedia; }