/** * @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; }
public function toCollectionOnDisk($collectionName = 'default', $diskName = '') { if (!is_file($this->pathToFile)) { throw new FileDoesNotExist(); } if (filesize($this->pathToFile) > config('laravel-medialibrary.max_file_size')) { throw new FileTooBig(); } $media = new Media(); $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 = []; $this->subject->media()->save($media); $this->fileSystem->add($this->pathToFile, $media, $this->fileName); if (!$this->preserveOriginal) { unlink($this->pathToFile); } return $media; }