Skip to content

Commit

Permalink
Add a setDisk method to the uploader (#31)
Browse files Browse the repository at this point in the history
* Add a setDisk method to the uploader

* Add method alias for consistency
  • Loading branch information
Jack97 committed Apr 23, 2020
1 parent 8d22540 commit e3d3344
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/MediaUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class MediaUploader
/** @var string */
protected $fileName;

/** @var string */
protected $disk;

/** @var array */
protected $attributes = [];

Expand Down Expand Up @@ -112,6 +115,28 @@ protected function sanitiseFileName(string $fileName)
return str_replace(['#', '/', '\\', ' '], '-', $fileName);
}

/**
* Specify the disk where the file will be stored.
*
* @param string $disk
* @return MediaUploader
*/
public function setDisk(string $disk)
{
$this->disk = $disk;

return $this;
}

/**
* @param string $disk
* @return MediaUploader
*/
public function toDisk(string $disk)
{
return $this->setDisk($disk);
}

/**
* Set any custom attributes to be saved to the media item.
*
Expand Down Expand Up @@ -147,7 +172,7 @@ public function upload()

$media->name = $this->name;
$media->file_name = $this->fileName;
$media->disk = config('media.disk');
$media->disk = $this->disk ?: config('media.disk');
$media->mime_type = $this->file->getMimeType();
$media->size = $this->file->getSize();

Expand Down
45 changes: 43 additions & 2 deletions tests/MediaUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,62 @@
namespace Optix\Media\Tests;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Optix\Media\MediaUploader;
use Optix\Media\Models\Media;
use Optix\Media\Tests\Models\Media as CustomMedia;

class MediaUploaderTest extends TestCase
{
const DEFAULT_DISK = 'default';

protected function setUp(): void
{
parent::setUp();

// Use a test disk as the default disk...
Config::set('media.disk', self::DEFAULT_DISK);

// Create a test filesystem for the default disk...
Storage::fake(self::DEFAULT_DISK);
}

/** @test */
public function it_can_upload_media()
public function it_can_upload_a_file_to_the_default_disk()
{
$file = UploadedFile::fake()->image('file-name.jpg');

$media = MediaUploader::fromFile($file)->upload();

$this->assertInstanceOf(Media::class, $media);
$this->assertTrue($media->filesystem()->exists($media->getPath()));
$this->assertEquals(self::DEFAULT_DISK, $media->disk);

$filesystem = Storage::disk(self::DEFAULT_DISK);

$this->assertTrue($filesystem->exists($media->getPath()));
}

/** @test */
public function it_can_upload_a_file_to_a_specific_disk()
{
$file = UploadedFile::fake()->image('file-name.jpg');

$customDisk = 'custom';

// Create a test filesystem for the custom disk...
Storage::fake($customDisk);

$media = MediaUploader::fromFile($file)
->setDisk($customDisk)
->upload();

$this->assertInstanceOf(Media::class, $media);
$this->assertEquals($customDisk, $media->disk);

$filesystem = Storage::disk($customDisk);

$this->assertTrue($filesystem->exists($media->getPath()));
}

/** @test */
Expand Down

0 comments on commit e3d3344

Please sign in to comment.