Adding feature to create files (db) in a folder

parent a8f63696
......@@ -12,6 +12,7 @@ use Modules\Media\Events\FileWasCreated;
use Modules\Media\Events\FileWasUpdated;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class EloquentFileRepository extends EloquentBaseRepository implements FileRepository
......@@ -37,9 +38,10 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
/**
* Create a file row from the given file
* @param UploadedFile $file
* @param int $parentId
* @return mixed
*/
public function createFromFile(UploadedFile $file)
public function createFromFile(UploadedFile $file, int $parentId = 0)
{
$fileName = FileHelper::slug($file->getClientOriginalName());
......@@ -51,7 +53,7 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$data = [
'filename' => $fileName,
'path' => config('asgard.media.config.files-path') . "{$fileName}",
'path' => $this->getPathFor($fileName, $parentId),
'extension' => substr(strrchr($fileName, '.'), 1),
'mimetype' => $file->getClientMimeType(),
'filesize' => $file->getFileInfo()->getSize(),
......@@ -66,6 +68,18 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return $file;
}
private function getPathFor(string $filename, int $folderId)
{
if ($folderId !== 0) {
$parent = app(FolderRepository::class)->findFolder($folderId);
if ($parent !== null) {
return $parent->path->getRelativeUrl() . '/' . $filename;
}
}
return config('asgard.media.config.files-path') . $filename;
}
public function destroy($file)
{
$file->delete();
......
......@@ -11,9 +11,10 @@ interface FileRepository extends BaseRepository
/**
* Create a file row from the given file
* @param UploadedFile $file
* @param int $parentId
* @return mixed
*/
public function createFromFile(UploadedFile $file);
public function createFromFile(UploadedFile $file, int $parentId = 0);
/**
* Find a file for the entity by zone
......
......@@ -10,6 +10,7 @@ use Modules\Media\Events\FileIsUpdating;
use Modules\Media\Events\FileWasCreated;
use Modules\Media\Events\FileWasUpdated;
use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
......@@ -27,6 +28,14 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this->resetDatabase();
$this->file = app(FileRepository::class);
$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 */
......@@ -212,6 +221,29 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this->assertEquals('Hello World', $file->translate('en')->description);
}
/** @test */
public function it_can_create_a_file_in_a_folder()
{
$folder = app(FolderRepository::class)->create(['name' => 'My Folder', 'parent_id' => 0]);
$file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $folder->id);
$this->assertCount(2, $this->file->all());
$this->assertEquals('/assets/media/my-folder/my-file.jpg', $file->path->getRelativeUrl());
}
/** @test */
public function it_can_create_a_file_in_sub_sub_folder()
{
$folderRepository = app(FolderRepository::class);
$folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$nestedFolder = $folderRepository->create(['name' => 'Nested Folder', 'parent_id' => 1]);
$file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $nestedFolder->id);
$this->assertEquals('/assets/media/my-folder/nested-folder/my-file.jpg', $file->path->getRelativeUrl());
}
private function createFile($fileName = 'random/name.jpg')
{
return File::create([
......
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