コード例 #1
0
ファイル: MediaMover.php プロジェクト: plank/laravel-mediable
 /**
  * Move the file to a new location on disk.
  *
  * Will invoke the `save()` method on the model after the associated file has been moved to prevent synchronization errors
  * @param  \Plank\Mediable\Media $media
  * @param  string                $directory directory relative to disk root
  * @param  string                $name      filename. Do not include extension
  * @return void
  * @throws \Plank\Mediable\Exceptions\MediaMoveException If attempting to change the file extension or a file with the same name already exists at the destination
  */
 public function move(Media $media, $directory, $filename = null)
 {
     $storage = $this->filesystem->disk($media->disk);
     if ($filename) {
         $filename = $this->removeExtensionFromFilename($filename, $media->extension);
     } else {
         $filename = $media->filename;
     }
     $directory = trim($directory, '/');
     $target_path = $directory . '/' . $filename . '.' . $media->extension;
     if ($storage->has($target_path)) {
         throw MediaMoveException::destinationExists($target_path);
     }
     $storage->move($media->getDiskPath(), $target_path);
     $media->filename = $filename;
     $media->directory = $directory;
     $media->save();
 }
コード例 #2
0
 /**
  * Reanalyze a media record's file and adjust the aggregate type and size, if necessary.
  * @param  \Plank\Mediable\Media  $media
  * @return bool Whether the model was modified
  */
 public function update(Media $media)
 {
     $storage = $this->filesystem->disk($media->disk);
     $media->size = $this->verifyFileSize($storage->size($media->getDiskPath()));
     $media->mime_type = $this->verifyMimeType($storage->mimeType($media->getDiskPath()));
     $media->aggregate_type = $this->inferAggregateType($media->mime_type, $media->extension);
     if ($dirty = $media->isDirty()) {
         $media->save();
     }
     return $dirty;
 }