Create Folder on disk

parent 6457696e
<?php
namespace Modules\Media\Events\Handlers;
use Illuminate\Contracts\Filesystem\Factory;
use Modules\Media\Events\FolderWasCreated;
class CreateFolderOnDisk
{
/**
* @var Factory
*/
private $filesystem;
public function __construct(Factory $filesystem)
{
$this->filesystem = $filesystem;
}
public function handle(FolderWasCreated $event)
{
$this->filesystem->disk($this->getConfiguredFilesystem())->makeDirectory($event->folder->path->getRelativeUrl());
}
/**
* @return string
*/
private function getConfiguredFilesystem()
{
return config('asgard.media.config.filesystem');
}
}
......@@ -14,6 +14,8 @@ use Modules\Media\Console\RefreshThumbnailCommand;
use Modules\Media\Contracts\DeletingMedia;
use Modules\Media\Contracts\StoringMedia;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\Handlers\CreateFolderOnDisk;
use Modules\Media\Events\Handlers\HandleMediaStorage;
use Modules\Media\Events\Handlers\RegisterMediaSidebar;
use Modules\Media\Events\Handlers\RemovePolymorphicLink;
......@@ -69,6 +71,7 @@ class MediaServiceProvider extends ServiceProvider
$events->listen(StoringMedia::class, HandleMediaStorage::class);
$events->listen(DeletingMedia::class, RemovePolymorphicLink::class);
$events->listen(FolderWasCreated::class, CreateFolderOnDisk::class);
$this->app[TagManager::class]->registerNamespace(new File());
$this->registerThumbnails();
......
......@@ -5,9 +5,7 @@ namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\Eloquent\EloquentFolderRepository;
use Modules\Media\Repositories\FolderRepository;
use PHPUnit\Framework\TestCase;
final class EloquentFolderRepositoryTest extends MediaTestCase
{
......@@ -26,6 +24,13 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->app['config']->set('asgard.media.config.files-path', '/assets/media/');
}
public function tearDown()
{
if ($this->app['files']->isDirectory(public_path('assets')) === true) {
$this->app['files']->deleteDirectory(public_path('assets'));
}
}
/** @test */
public function it_can_create_a_folder_in_database()
{
......@@ -76,6 +81,14 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertEquals('MY FOLDER', $folder->filename);
}
/** @test */
public function it_can_create_folder_on_disk()
{
$this->folder->create(['name' => 'My Folder']);
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/my-folder')));
}
private function resetDatabase()
{
// Makes sure the migrations table is created
......
......@@ -65,5 +65,6 @@ abstract class MediaTestCase extends TestCase
$app['config']->set('app.url', 'http://localhost');
$app['config']->set('filesystems.disks.local.url', 'http://localhost');
$app['config']->set('filesystems.disks.local.visibility', 'public');
$app['config']->set('filesystems.disks.local.root', public_path());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment