Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Nov 21, 2017
1 parent 8e1256c commit 48f371a
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 52 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Conversion/ConversionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
48 changes: 34 additions & 14 deletions src/FileAdder/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,15 +95,15 @@ 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);

return $this;
}

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);

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;

Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
}
}
11 changes: 11 additions & 0 deletions src/HasMedia/Interfaces/HasMedia.php → src/HasMedia/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
23 changes: 23 additions & 0 deletions src/HasMedia/HasMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,9 @@ trait HasMediaTrait
/** @var array */
public $mediaConversions = [];

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

/** @var bool */
protected $deletePreservingMedia = false;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -494,4 +507,14 @@ protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeType
throw MimeTypeNotAllowed::create($file, $allowedMimeTypes);
}
}

public function registerMediaConversions(Media $media = null)
{

}

public function registerMediaCollections()
{

}
}
10 changes: 0 additions & 10 deletions src/HasMedia/Interfaces/HasMediaConversions.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/MediaCollection/MediaCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Spatie\MediaLibrary\MediaCollection;

class MediaCollection
{
/** @var string */
public $name = '';

/** @var string */
public $diskName = '';

public function __construct(string $name)
{
$this->name = $name;
}

public static function create($name)
{
return new static($name);
}

public function disk(string $diskName)
{
$this->diskName = $diskName;

return $this;
}
}
67 changes: 67 additions & 0 deletions tests/HasMediaConversionsTrait/MediaCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Spatie\MediaLibrary\Test\HasMediaConversionsTrait;

use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaCollections;
use Spatie\MediaLibrary\Media;
use Spatie\MediaLibrary\Test\TestCase;
use Spatie\MediaLibrary\Test\TestModelWithConversion;

class MediaCollectionTest extends TestCase
{
/** @test */
public function it_will_use_the_disk_from_a_media_collection()
{
$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())->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');
}
}
4 changes: 2 additions & 2 deletions tests/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 0 additions & 22 deletions tests/TestModelWithSoftDeletes.php

This file was deleted.

0 comments on commit 48f371a

Please sign in to comment.