Adding logic to create files and thumbnails inside sub folder

parent 33e68a1c
...@@ -110,10 +110,13 @@ class Imagy ...@@ -110,10 +110,13 @@ class Imagy
foreach ($this->manager->all() as $thumbnail) { foreach ($this->manager->all() as $thumbnail) {
$image = $this->image->make($this->filesystem->disk($this->getConfiguredFilesystem())->get($this->getDestinationPath($path->getRelativeUrl()))); $image = $this->image->make($this->filesystem->disk($this->getConfiguredFilesystem())->get($this->getDestinationPath($path->getRelativeUrl())));
$filename = config('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name());
$filename = $this->getFilenameFor($path, $thumbnail);
foreach ($thumbnail->filters() as $manipulation => $options) { foreach ($thumbnail->filters() as $manipulation => $options) {
$image = $this->imageFactory->make($manipulation)->handle($image, $options); $image = $this->imageFactory->make($manipulation)->handle($image, $options);
} }
$image = $image->stream(pathinfo($path, PATHINFO_EXTENSION), array_get($thumbnail->filters(), 'quality', 90)); $image = $image->stream(pathinfo($path, PATHINFO_EXTENSION), array_get($thumbnail->filters(), 'quality', 90));
$this->writeImage($filename, $image); $this->writeImage($filename, $image);
} }
...@@ -241,4 +244,17 @@ class Imagy ...@@ -241,4 +244,17 @@ class Imagy
return $path; return $path;
} }
private function getFilenameFor(MediaPath $path, Thumbnail $thumbnail)
{
$filenameWithoutPrefix = str_replace(config('asgard.media.config.files-path'), '', $path->getRelativeUrl());
$filename = substr(strrchr($filenameWithoutPrefix, '/'), 1);
$folders = str_replace($filename, '' , $filenameWithoutPrefix);
if ($filename === false) {
return config('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name());
}
return config('asgard.media.config.files-path') . $folders . $this->newFilename($path, $thumbnail->name());
}
} }
...@@ -30,11 +30,12 @@ class FileService ...@@ -30,11 +30,12 @@ class FileService
/** /**
* @param UploadedFile $file * @param UploadedFile $file
* @param int $parentId
* @return mixed * @return mixed
*/ */
public function store(UploadedFile $file) public function store(UploadedFile $file, int $parentId = 0)
{ {
$savedFile = $this->file->createFromFile($file); $savedFile = $this->file->createFromFile($file, $parentId);
$path = $this->getDestinationPath($savedFile->getOriginal('path')); $path = $this->getDestinationPath($savedFile->getOriginal('path'));
$stream = fopen($file->getRealPath(), 'r+'); $stream = fopen($file->getRealPath(), 'r+');
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Modules\Media\Tests; namespace Modules\Media\Tests;
use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\FileService; use Modules\Media\Services\FileService;
final class FileServiceTest extends MediaTestCase final class FileServiceTest extends MediaTestCase
...@@ -60,4 +61,30 @@ final class FileServiceTest extends MediaTestCase ...@@ -60,4 +61,30 @@ final class FileServiceTest extends MediaTestCase
$this->assertFalse($this->app['files']->exists(public_path('assets/media/records_smallThumb.pdf'))); $this->assertFalse($this->app['files']->exists(public_path('assets/media/records_smallThumb.pdf')));
$this->assertFalse($this->app['files']->exists(public_path('assets/media/records_mediumThumb.pdf'))); $this->assertFalse($this->app['files']->exists(public_path('assets/media/records_mediumThumb.pdf')));
} }
/** @test */
public function it_can_store_a_file_in_sub_folder()
{
$folderRepository = app(FolderRepository::class);
$folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$file = \Illuminate\Http\UploadedFile::fake()->create('records.pdf');
$this->fileService->store($file, 1);
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-folder/records.pdf')));
}
/** @test */
public function it_can_store_an_image_with_thumbnails_in_sub_folder()
{
$folderRepository = app(FolderRepository::class);
$folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$this->fileService->store($file, 1);
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-folder/my-file.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-folder/my-file_smallThumb.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-folder/my-file_mediumThumb.jpg')));
}
} }
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