/**
  * @param \Illuminate\Database\Eloquent\Model $subject
  * @param string                              $key
  *
  * @return \Spatie\MediaLibrary\FileAdder\FileAdder
  *
  * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
  */
 public static function createFromRequest(Model $subject, string $key)
 {
     if (!request()->hasFile($key)) {
         throw FileCannotBeAdded::requestDoesNotHaveFile($key);
     }
     return static::create($subject, request()->file($key));
 }
Пример #2
0
 /**
  * Add a remote file to the medialibrary.
  *
  * @param string $url
  *
  * @return \Spatie\MediaLibrary\FileAdder\FileAdder
  *
  * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
  */
 public function addMediaFromUrl(string $url)
 {
     if (!($stream = @fopen($url, 'r'))) {
         throw FileCannotBeAdded::unreachableUrl($url);
     }
     $tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
     file_put_contents($tmpFile, $stream);
     $filename = basename(parse_url($url, PHP_URL_PATH));
     return app(FileAdderFactory::class)->create($this, $tmpFile)->usingName(pathinfo($filename, PATHINFO_FILENAME))->usingFileName($filename);
 }
Пример #3
0
 /**
  * Determine the disk to be used.
  *
  * @param string $diskName
  *
  * @return string
  *
  * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
  */
 protected function determineDiskName(string $diskName)
 {
     if ($diskName === '') {
         $diskName = config('laravel-medialibrary.defaultFilesystem');
     }
     if (is_null(config("filesystems.disks.{$diskName}"))) {
         throw FileCannotBeAdded::diskDoesNotExist($diskName);
     }
     return $diskName;
 }
Пример #4
0
 protected function deleteOrphanedFiles()
 {
     $diskName = $this->argument('disk') ?: config('laravel-medialibrary.defaultFilesystem');
     if (is_null(config("filesystems.disks.{$diskName}"))) {
         throw FileCannotBeAdded::diskDoesNotExist($diskName);
     }
     $mediaIds = collect($this->mediaRepository->all()->pluck('id')->toArray());
     collect($this->fileSystem->disk($diskName)->directories())->filter(function (string $directory) use($mediaIds) {
         return is_numeric($directory) ? !$mediaIds->contains((int) $directory) : false;
     })->each(function (string $directory) use($diskName) {
         if (!$this->isDryRun) {
             $this->fileSystem->disk($diskName)->deleteDirectory($directory);
         }
         $this->info("Orphaned media directory `{$directory}` " . ($this->isDryRun ? 'found' : 'has been removed'));
     });
 }