public function add(string $file, Media $media, string $targetFileName = '') { $this->copyToMediaLibrary($file, $media, false, $targetFileName); event(new MediaHasBeenAdded($media)); app(FileManipulator::class)->createDerivedFiles($media); }
/** * @param string $collectionName * @param string $diskName * * @return \Spatie\MediaLibrary\Media * * @throws FileCannotBeAdded * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded */ public function toCollectionOnDisk(string $collectionName = 'default', string $diskName = '') { if (!$this->subject->exists) { throw FileCannotBeAdded::modelDoesNotExist($this->subject); } if (!is_file($this->pathToFile)) { throw FileCannotBeAdded::fileDoesNotExist($this->pathToFile); } if (filesize($this->pathToFile) > config('laravel-medialibrary.max_file_size')) { throw FileCannotBeAdded::fileIsTooBig($this->pathToFile); } $mediaClass = config('laravel-medialibrary.media_model'); $media = new $mediaClass(); $media->name = $this->mediaName; $media->file_name = $this->fileName; $media->disk = $this->determineDiskName($diskName); $media->collection_name = $collectionName; $media->size = filesize($this->pathToFile); $media->custom_properties = $this->customProperties; $media->manipulations = []; $media->fill($this->properties); $this->subject->media()->save($media); $this->filesystem->add($this->pathToFile, $media, $this->fileName); if (!$this->preserveOriginal) { unlink($this->pathToFile); } return $media; }