Skip to content

Commit

Permalink
Added ability to addMedia for non existing model (#607)
Browse files Browse the repository at this point in the history
* Added ability to addMedia for non existing model

* CS fixes, removed non saved model test

* Removed unused imports

* Renamed unsavedMedias to unsavedMediaItems
CS fixes provide

* Import order fix

* Added test case for testing correct data retrieving in unsaved model
  • Loading branch information
Vyacheslav authored and freekmurze committed Apr 11, 2017
1 parent 0587929 commit 851441f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
47 changes: 39 additions & 8 deletions src/FileAdder/FileAdder.php
Expand Up @@ -2,17 +2,18 @@

namespace Spatie\MediaLibrary\FileAdder;

use Spatie\MediaLibrary\Media;
use Spatie\MediaLibrary\Helpers\File;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\Filesystem\Filesystem;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnknownType;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\ModelDoesNotExist;

class FileAdder
{
Expand Down Expand Up @@ -247,10 +248,6 @@ public function toMediaLibraryOnCloudDisk(string $collectionName = 'default')
*/
public function toMediaCollection(string $collectionName = 'default', string $diskName = '')
{
if (! $this->subject->exists) {
throw ModelDoesNotExist::create($this->subject);
}

if (! is_file($this->pathToFile)) {
throw FileDoesNotExist::create($this->pathToFile);
}
Expand All @@ -275,9 +272,7 @@ public function toMediaCollection(string $collectionName = 'default', string $di

$media->fill($this->properties);

$this->subject->media()->save($media);

$this->filesystem->add($this->pathToFile, $media, $this->fileName);
$this->attachMedia($media);

if (! $this->preserveOriginal) {
unlink($this->pathToFile);
Expand Down Expand Up @@ -331,4 +326,40 @@ protected function sanitizeFileName(string $fileName) : string
{
return str_replace(['#', '/', '\\'], '-', $fileName);
}

/**
* @param Media $media
*/
protected function attachMedia(Media $media)
{
if (! $this->subject->exists) {
$this->subject->unsavedMediaItems[] = $media;

$class = get_class($this->subject);

$class::created(function ($model) {
if (empty($model->unsavedMediaItems)) {
return;
}

foreach ($model->unsavedMediaItems as $unsavedMediaItem) {
$this->processMediaItem($model, $unsavedMediaItem);
}
});

return;
}

$this->processMediaItem($this->subject, $media);
}

/**
* @param HasMedia $model
* @param Media $media
*/
protected function processMediaItem(HasMedia $model, Media $media)
{
$model->media()->save($media);
$this->filesystem->add($this->pathToFile, $media, $this->fileName);
}
}
8 changes: 6 additions & 2 deletions src/HasMedia/HasMediaTrait.php
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Collection;
use Spatie\MediaLibrary\MediaRepository;
use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\Filesystem\Filesystem;
use Spatie\MediaLibrary\FileAdder\FileAdderFactory;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Spatie\MediaLibrary\Events\CollectionHasBeenCleared;
Expand All @@ -23,6 +22,9 @@ trait HasMediaTrait
/** @var bool */
protected $deletePreservingMedia = false;

/** @var array */
public $unsavedMediaItems = [];

public static function bootHasMediaTrait()
{
static::deleted(function (HasMedia $entity) {
Expand Down Expand Up @@ -411,7 +413,9 @@ protected function mediaIsPreloaded(): bool
*/
public function loadMedia(string $collectionName)
{
return $this->media
$collection = $this->exists ? $this->media : collect($this->unsavedMediaItems);

return $collection
->filter(function (Media $mediaItem) use ($collectionName) {
if ($collectionName == '') {
return true;
Expand Down
12 changes: 0 additions & 12 deletions tests/FileAdder/IntegrationTest.php
Expand Up @@ -4,15 +4,13 @@

use Spatie\MediaLibrary\Media;
use Spatie\MediaLibrary\Test\TestCase;
use Spatie\MediaLibrary\Test\TestModel;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnknownType;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnreachableUrl;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\InvalidBase64Data;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\ModelDoesNotExist;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\RequestDoesNotHaveFile;

class IntegrationTest extends TestCase
Expand Down Expand Up @@ -47,16 +45,6 @@ public function it_will_throw_an_exception_when_adding_a_non_existing_file()
->toMediaCollection();
}

/** @test */
public function it_will_throw_an_exception_when_adding_a_non_saved_model()
{
$this->expectException(ModelDoesNotExist::class);

(new TestModel())
->addMedia($this->getTestJpg())
->toMediaCollection();
}

/** @test */
public function it_can_set_the_name_of_the_media()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/HasMediaTrait/HasMediaTest.php
Expand Up @@ -20,6 +20,14 @@ public function it_returns_true_for_a_non_empty_collection()
$this->assertTrue($this->testModel->hasMedia());
}

/** @test */
public function it_returns_true_for_a_non_empty_collection_in_unsaved_model()
{
$this->testUnsavedModel->addMedia($this->getTestJpg())->toMediaCollection();

$this->assertTrue($this->testUnsavedModel->hasMedia());
}

/** @test */
public function it_returns_true_for_if_any_collection_is_not_empty()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Expand Up @@ -12,6 +12,9 @@ abstract class TestCase extends Orchestra
/** @var \Spatie\MediaLibrary\Test\TestModel */
protected $testModel;

/** @var \Spatie\MediaLibrary\Test\TestModel */
protected $testUnsavedModel;

/** @var \Spatie\MediaLibrary\Test\TestModelWithConversion */
protected $testModelWithConversion;

Expand All @@ -30,6 +33,7 @@ public function setUp()
$this->setUpTempTestFiles();

$this->testModel = TestModel::first();
$this->testUnsavedModel = new TestModel;
$this->testModelWithConversion = TestModelWithConversion::first();
$this->testModelWithoutMediaConversions = TestModelWithoutMediaConversions::first();
$this->testModelWithMorphMap = TestModelWithMorphMap::first();
Expand Down

0 comments on commit 851441f

Please sign in to comment.