media = $media; $this->newStorage = $newStorage; } /** * Execute the job. * * @return void */ public function handle(): void { // Don't continue if the media is already on the new storage disk if ($this->media->storage === $this->newStorage) { return; } $this->media->status = 'Moving file'; $this->media->save(); $files = ["/{$this->media->name}"]; if (empty($this->media->variants) === false) { foreach ($this->media->variants as $variant => $name) { $files[] = "/{$name}"; } } $this->media->invalidateCFCache(); // Move the files from the old storage disk to the new storage disk foreach ($files as $file) { Storage::disk($this->newStorage)->put($file, Storage::disk($this->media->storage)->get($file)); Storage::disk($this->media->storage)->delete($file); } // Update the media model with the new storage and save it to the database $this->media->storage = $this->newStorage; $this->media->status = 'OK'; $this->media->save(); } }