Inheritance: extends Illuminate\Database\Eloquent\Model, use trait SortableTrait
 public static function createForMedia(Media $media) : UrlGenerator
 {
     $urlGeneratorClass = config('laravel-medialibrary.custom_url_generator_class') ?: 'Spatie\\MediaLibrary\\UrlGenerator\\' . ucfirst($media->getDiskDriverName()) . 'UrlGenerator';
     static::guardAgainstInvalidUrlGenerator($urlGeneratorClass);
     $urlGenerator = app($urlGeneratorClass);
     $pathGenerator = PathGeneratorFactory::create();
     $urlGenerator->setMedia($media)->setPathGenerator($pathGenerator);
     return $urlGenerator;
 }
 public static function createForMedia(Media $media)
 {
     $urlGeneratorClass = 'Spatie\\MediaLibrary\\UrlGenerator\\' . ucfirst($media->getDiskDriverName()) . 'UrlGenerator';
     $customUrlClass = config('laravel-medialibrary.custom_url_generator_class');
     $urlGenerator = self::isAValidUrlGeneratorClass($customUrlClass) ? app($customUrlClass) : app($urlGeneratorClass);
     $pathGenerator = PathGeneratorFactory::create();
     $urlGenerator->setMedia($media)->setPathGenerator($pathGenerator);
     return $urlGenerator;
 }
 public static function createForMedia(Media $media)
 {
     $urlGeneratorClass = 'Spatie\\MediaLibrary\\UrlGenerator\\' . ucfirst($media->getDiskDriverName()) . 'UrlGenerator';
     $customClass = config('laravel-medialibrary.custom_url_generator_class');
     if ($customClass != '' && class_exists($customClass) && is_subclass_of($customClass, UrlGenerator::class)) {
         $urlGeneratorClass = $customClass;
     }
     $urlGenerator = app($urlGeneratorClass);
     $urlGenerator->setMedia($media);
     return $urlGenerator;
 }
 /**
  * Bootstrap the application events.
  */
 public function boot()
 {
     Media::observe(new MediaObserver());
     $this->publishes([__DIR__ . '/../resources/config/laravel-medialibrary.php' => $this->app->configPath() . '/' . 'laravel-medialibrary.php'], 'config');
     if (!class_exists('CreateMediaTable')) {
         // Publish the migration
         $timestamp = date('Y_m_d_His', time());
         $this->publishes([__DIR__ . '/../resources/migrations/create_media_table.php.stub' => $this->app->basePath() . '/' . 'database/migrations/' . $timestamp . '_create_media_table.php'], 'migrations');
     }
 }
 /**
  * Update a media collection by deleting and inserting again with new values.
  *
  * @param array  $newMediaArray
  * @param string $collectionName
  *
  * @throws \Spatie\MediaLibrary\Exceptions\MediaIsNotPartOfCollection
  */
 public function updateMedia(array $newMediaArray, $collectionName = 'default')
 {
     $this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);
     $orderColumn = 0;
     foreach ($newMediaArray as $newMediaItem) {
         $currentMedia = Media::findOrFail($newMediaItem['id']);
         if ($currentMedia->collection_name != $collectionName) {
             throw new MediaIsNotPartOfCollection(sprintf('Media id %s is not part of collection %s', $currentMedia->id, $collectionName));
         }
         if (array_key_exists('name', $newMediaItem)) {
             $currentMedia->name = $newMediaItem['name'];
         }
         $currentMedia->temp = 0;
         $currentMedia->order_column = $orderColumn++;
         $currentMedia->save();
     }
 }
 public static function doesNotBelongToCollection(string $collectionName, Media $media)
 {
     return new static("Media id {$media->getKey()} is not part of collection `{$collectionName}`");
 }
Example #7
0
 public function transform(Media $media)
 {
     return ['id' => $media->id, 'name' => $media->name, 'file_name' => $media->file_name, 'custom_properties' => $media->custom_properties, 'order_column' => $media->order_column, 'thumbUrl' => $media->getUrl('admin'), 'originalUrl' => $media->getUrl()];
 }
 /**
  * Get the path prefix.
  *
  * @param Media $media
  *
  * @return string
  */
 protected function getPrefix(Media $media)
 {
     return md5($media->getAttribute('model_id') . '|' . $media->getAttribute('model_type'));
 }
 public function getByCollectionName(string $collectionName) : DbCollection
 {
     return $this->model->where('collection_name', $collectionName)->get();
 }
 /**
  * Re order the media in the collection.
  *
  * @param array $mediaIds
  *
  * @return $this
  *
  * @throws \Spatie\EloquentSortable\SortableException
  */
 public function saveOrder(array $mediaIds)
 {
     Media::setNewOrder($mediaIds);
     return $this;
 }
 /**
  * Get all media for the given type.
  *
  * @param string $modelType
  *
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function getByModelType($modelType)
 {
     return $this->model->where('model_type', $modelType)->get();
 }
 public function transform(Media $profilePicture)
 {
     return ['id' => $profilePicture->id, 'name' => $profilePicture->name, 'file_name' => $profilePicture->file_name, 'size' => $profilePicture->size, 'image' => $profilePicture->getUrl(), 'thumbnail' => ['square' => $profilePicture->getUrl('square'), 'small' => $profilePicture->getUrl('original180'), 'medium' => $profilePicture->getUrl('original250'), 'large' => $profilePicture->getUrl('original400')]];
 }
 public function transform(Media $media)
 {
     return ['id' => $media->id, 'name' => $media->name, 'collection' => $media->collection_name, 'fileName' => $media->file_name, 'customProperties' => json_encode($media->custom_properties, JSON_FORCE_OBJECT), 'orderColumn' => $media->order_column, 'thumbUrl' => strtolower($media->extension) === 'svg' ? $media->getUrl() : $media->getUrl('admin'), 'originalUrl' => $media->getUrl()];
 }
 public static function doesNotBelongToModel(Media $media, Model $model)
 {
     $modelClass = get_class($model);
     return new static("Media with id {$media->getKey()} cannot be deleted because it does not belong to model {$modelClass} with id {$model->id}");
 }
 public function deleteImage($galleryId, $imageId)
 {
     $image = Media::findOrFail($imageId);
     $image->delete();
     return response('ok');
 }
 /**
  * Get a (unique) base path for the given media.
  *
  * @param \Spatie\MediaLibrary\Media $media
  *
  * @return string
  */
 protected function getBasePath(Media $media)
 {
     return $media->getKey();
 }
 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 = [];
     $media->save();
     $this->subject->media()->save($media);
     $this->fileSystem->add($this->pathToFile, $media, $this->fileName);
     if (!$this->preserveOriginal) {
         unlink($this->pathToFile);
     }
     return $media;
 }
 /**
  * @param \Spatie\MediaLibrary\Media $media
  *
  * @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null
  */
 public function determineImageGenerator(Media $media)
 {
     $imageGenerators = $media->getImageGenerators()->map(function (string $imageGeneratorClassName) {
         return app($imageGeneratorClassName);
     });
     foreach ($imageGenerators as $imageGenerator) {
         if ($imageGenerator->canConvert($media)) {
             return $imageGenerator;
         }
     }
 }