diff --git a/CHANGELOG.md b/CHANGELOG.md index ea551ba5e..6c659acea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `laravel-medialibrary` will be documented in this file +## 7.0.0 - 2018-XX-XX + +- dropped support for soft deletes +- added `MediaCollections` +- removed distinction between `HasMedia` and `HasMediaConversions` + ## 6.6.2 - 2017-11-07 - improve config comments diff --git a/src/Conversion/ConversionCollection.php b/src/Conversion/ConversionCollection.php index a962efe7f..eb4f99bb8 100644 --- a/src/Conversion/ConversionCollection.php +++ b/src/Conversion/ConversionCollection.php @@ -8,7 +8,6 @@ use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Relations\Relation; use Spatie\MediaLibrary\Exceptions\InvalidConversion; -use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; class ConversionCollection extends Collection { @@ -87,9 +86,8 @@ protected function addConversionsFromRelatedModel(Media $media) $model->mediaConversion = []; } - if ($model instanceof HasMediaConversions) { - $model->registerMediaConversions($media); - } + $model->registerMediaConversions($media); + $this->items = $model->mediaConversions; } diff --git a/src/FileAdder/FileAdder.php b/src/FileAdder/FileAdder.php index 22c378480..1d7e08f29 100644 --- a/src/FileAdder/FileAdder.php +++ b/src/FileAdder/FileAdder.php @@ -8,6 +8,7 @@ use Spatie\MediaLibrary\Filesystem\Filesystem; use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded; use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia; +use Spatie\MediaLibrary\MediaCollection\MediaCollection; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnknownType; @@ -94,7 +95,7 @@ public function setFile($file) } if ($file instanceof UploadedFile) { - $this->pathToFile = $file->getPath().'/'.$file->getFilename(); + $this->pathToFile = $file->getPath() . '/' . $file->getFilename(); $this->setFileName($file->getClientOriginalName()); $this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); @@ -102,7 +103,7 @@ public function setFile($file) } if ($file instanceof SymfonyFile) { - $this->pathToFile = $file->getPath().'/'.$file->getFilename(); + $this->pathToFile = $file->getPath() . '/' . $file->getFilename(); $this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME)); $this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME); @@ -255,7 +256,7 @@ public function toMediaCollectionOnCloudDisk(string $collectionName = 'default') */ public function toMediaCollection(string $collectionName = 'default', string $diskName = '') { - if (! is_file($this->pathToFile)) { + if (!is_file($this->pathToFile)) { throw FileDoesNotExist::create($this->pathToFile); } @@ -271,7 +272,12 @@ public function toMediaCollection(string $collectionName = 'default', string $di $this->fileName = ($this->fileNameSanitizer)($this->fileName); $media->file_name = $this->fileName; - $media->disk = $this->determineDiskName($diskName); + + $media->disk = $this->determineDiskName($diskName, $collectionName); + + if (is_null(config("filesystems.disks.{$media->disk}"))) { + throw DiskDoesNotExist::create($media->disk); + } $media->collection_name = $collectionName; @@ -289,22 +295,27 @@ public function toMediaCollection(string $collectionName = 'default', string $di /** * @param string $diskName + * @param string $collectionName * * @return string - * - * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded */ - protected function determineDiskName(string $diskName) + protected function determineDiskName(string $diskName, $collectionName): string { - if ($diskName === '') { - $diskName = config('medialibrary.default_filesystem'); + if ($diskName !== '') { + return $diskName; } - if (is_null(config("filesystems.disks.{$diskName}"))) { - throw DiskDoesNotExist::create($diskName); + if ($collection = $this->getMediaCollection($collectionName)) { + $collectionDiskName = $collection->diskName; + + if ($collectionDiskName !== '') { + + + return $collectionDiskName; + } } - return $diskName; + return config('medialibrary.default_filesystem'); } /** @@ -336,7 +347,7 @@ public function sanitizingFileName(callable $fileNameSanitizer) */ protected function attachMedia(Media $media) { - if (! $this->subject->exists) { + if (!$this->subject->exists) { $this->subject->prepareToAttachMedia($media, $this); $class = get_class($this->subject); @@ -364,8 +375,17 @@ protected function processMediaItem(HasMedia $model, Media $media, FileAdder $fi $this->filesystem->add($fileAdder->pathToFile, $media, $fileAdder->fileName); - if (! $fileAdder->preserveOriginal) { + if (!$fileAdder->preserveOriginal) { unlink($fileAdder->pathToFile); } } + + protected function getMediaCollection(string $collectionName): ?MediaCollection + { + $this->subject->registerMediaCollections(); + + return collect($this->subject->mediaCollections)->first(function (MediaCollection $collection) use ($collectionName) { + return $collection->name === $collectionName; + }); + } } diff --git a/src/HasMedia/Interfaces/HasMedia.php b/src/HasMedia/HasMedia.php similarity index 91% rename from src/HasMedia/Interfaces/HasMedia.php rename to src/HasMedia/HasMedia.php index 112b0f71c..aa3feabca 100644 --- a/src/HasMedia/Interfaces/HasMedia.php +++ b/src/HasMedia/HasMedia.php @@ -80,4 +80,15 @@ public function shouldDeletePreservingMedia(); * @return mixed */ public function loadMedia(string $collectionName); + + /** + * Register the media conversions. + */ + public function registerMediaConversions(); + + + /** + * Register the media collections. + */ + public function registerMediaCollections(); } diff --git a/src/HasMedia/HasMediaTrait.php b/src/HasMedia/HasMediaTrait.php index aae73c256..28c21f1d8 100644 --- a/src/HasMedia/HasMediaTrait.php +++ b/src/HasMedia/HasMediaTrait.php @@ -6,6 +6,7 @@ use Illuminate\Http\File; use Spatie\MediaLibrary\Media; use Illuminate\Support\Collection; +use Spatie\MediaLibrary\MediaCollection\MediaCollection; use Spatie\MediaLibrary\MediaRepository; use Illuminate\Support\Facades\Validator; use Spatie\MediaLibrary\FileAdder\FileAdder; @@ -25,6 +26,9 @@ trait HasMediaTrait /** @var array */ public $mediaConversions = []; + /** @var array */ + public $mediaCollections = []; + /** @var bool */ protected $deletePreservingMedia = false; @@ -411,6 +415,15 @@ public function addMediaConversion(string $name): Conversion return $conversion; } + public function addMediaCollection(string $name): MediaCollection + { + $mediaCollection = MediaCollection::create($name); + + $this->mediaCollections[] = $mediaCollection; + + return $mediaCollection; + } + /** * Delete the model, but preserve all the associated media. * @@ -494,4 +507,14 @@ protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeType throw MimeTypeNotAllowed::create($file, $allowedMimeTypes); } } + + public function registerMediaConversions(Media $media = null) + { + + } + + public function registerMediaCollections() + { + + } } diff --git a/src/HasMedia/Interfaces/HasMediaConversions.php b/src/HasMedia/Interfaces/HasMediaConversions.php deleted file mode 100644 index 6b00f7b8e..000000000 --- a/src/HasMedia/Interfaces/HasMediaConversions.php +++ /dev/null @@ -1,10 +0,0 @@ -name = $name; + } + + public static function create($name) + { + return new static($name); + } + + public function disk(string $diskName) + { + $this->diskName = $diskName; + + return $this; + } +} \ No newline at end of file diff --git a/tests/HasMediaConversionsTrait/MediaCollectionTest.php b/tests/HasMediaConversionsTrait/MediaCollectionTest.php new file mode 100644 index 000000000..297acb3d1 --- /dev/null +++ b/tests/HasMediaConversionsTrait/MediaCollectionTest.php @@ -0,0 +1,67 @@ +addMediaCollection('images') + ->disk('secondMediaDisk'); + } + }; + + $model = $testModel::create(['name' => 'testmodel']); + + $media = $model->addMedia($this->getTestJpg())->preservingOriginal()->toMediaCollection('images'); + + $this->assertFileNotExists($this->getTempDirectory('media').'/'.$media->id.'/test.jpg'); + + $this->assertFileExists($this->getTempDirectory('media2').'/'.$media->id.'/test.jpg'); + + $media = $model->addMedia($this->getTestJpg())->toMediaCollection('other-images'); + + $this->assertFileExists($this->getTempDirectory('media').'/'.$media->id.'/test.jpg'); + } + + /** @test */ + public function it_will_not_use_the_disk_name_of_the_collection_if_a_diskname_is_specified_while_adding() + { + $testModel = new class extends TestModelWithConversion implements HasMediaCollections + { + public function registerMediaConversions(Media $media = null) + { + + } + + public function registerMediaCollections() + { + $this->addMediaCollection('images') + ->disk('secondMediaDisk'); + } + }; + + $model = $testModel::create(['name' => 'testmodel']); + + $media = $model->addMedia($this->getTestJpg())->toMediaCollection('images', 'public'); + + $this->assertFileExists($this->getTempDirectory('media').'/'.$media->id.'/test.jpg'); + + $this->assertFileNotExists($this->getTempDirectory('media2').'/'.$media->id.'/test.jpg'); + } +} \ No newline at end of file diff --git a/tests/TestModel.php b/tests/TestModel.php index 7398a1d01..a9c5d278f 100644 --- a/tests/TestModel.php +++ b/tests/TestModel.php @@ -2,12 +2,12 @@ namespace Spatie\MediaLibrary\Test; +use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia; use Spatie\MediaLibrary\Media; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia\HasMediaTrait; -use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; -class TestModel extends Model implements HasMediaConversions +class TestModel extends Model implements HasMedia { use HasMediaTrait; diff --git a/tests/TestModelWithSoftDeletes.php b/tests/TestModelWithSoftDeletes.php deleted file mode 100644 index 50cf40581..000000000 --- a/tests/TestModelWithSoftDeletes.php +++ /dev/null @@ -1,22 +0,0 @@ -