Fetching image thumbnails from subfolders

parent c00db947
...@@ -93,7 +93,7 @@ class Imagy ...@@ -93,7 +93,7 @@ class Imagy
return (new MediaPath($originalImage))->getRelativeUrl(); return (new MediaPath($originalImage))->getRelativeUrl();
} }
$path = config('asgard.media.config.files-path') . $this->newFilename($originalImage, $thumbnail); $path = $this->getFilenameFor($originalImage, $thumbnail);
return (new MediaPath($path))->getUrl(); return (new MediaPath($path))->getUrl();
} }
...@@ -245,16 +245,38 @@ class Imagy ...@@ -245,16 +245,38 @@ class Imagy
return $path; return $path;
} }
private function getFilenameFor(MediaPath $path, Thumbnail $thumbnail) /**
* @param MediaPath $path
* @param Thumbnail|string $thumbnail
* @return string
*/
private function getFilenameFor(MediaPath $path, $thumbnail)
{ {
$filenameWithoutPrefix = str_replace(config('asgard.media.config.files-path'), '', $path->getRelativeUrl()); if ($thumbnail instanceof Thumbnail) {
$thumbnail = $thumbnail->name();
}
$filenameWithoutPrefix = $this->removeConfigPrefix($path->getRelativeUrl());
$filename = substr(strrchr($filenameWithoutPrefix, '/'), 1); $filename = substr(strrchr($filenameWithoutPrefix, '/'), 1);
$folders = str_replace($filename, '' , $filenameWithoutPrefix); $folders = str_replace($filename, '' , $filenameWithoutPrefix);
if ($filename === false) { if ($filename === false) {
return config('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name()); return config('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail);
} }
return config('asgard.media.config.files-path') . $folders . $this->newFilename($path, $thumbnail->name()); return config('asgard.media.config.files-path') . $folders . $this->newFilename($path, $thumbnail);
}
/**
* @param string $path
* @return string
*/
private function removeConfigPrefix(string $path) : string
{
$configAssetPath = config('asgard.media.config.files-path');
return str_replace([
$configAssetPath,
ltrim($configAssetPath, '/')
], '', $path);
} }
} }
...@@ -70,7 +70,7 @@ class FileTest extends MediaTestCase ...@@ -70,7 +70,7 @@ class FileTest extends MediaTestCase
{ {
$file = $this->createFile('my/file/name.jpg'); $file = $this->createFile('my/file/name.jpg');
$this->assertEquals('http://localhost/name_smallThumb.jpg', $file->getThumbnail('smallThumb')); $this->assertEquals('http://localhost/my/file/name_smallThumb.jpg', $file->getThumbnail('smallThumb'));
} }
/** @test */ /** @test */
......
...@@ -8,6 +8,8 @@ use Illuminate\Support\Facades\App; ...@@ -8,6 +8,8 @@ use Illuminate\Support\Facades\App;
use Modules\Media\Image\Imagy; use Modules\Media\Image\Imagy;
use Modules\Media\Image\Intervention\InterventionFactory; use Modules\Media\Image\Intervention\InterventionFactory;
use Modules\Media\Image\ThumbnailManager; use Modules\Media\Image\ThumbnailManager;
use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\FileService;
use Modules\Media\ValueObjects\MediaPath; use Modules\Media\ValueObjects\MediaPath;
class ImagyTest extends MediaTestCase class ImagyTest extends MediaTestCase
...@@ -36,6 +38,7 @@ class ImagyTest extends MediaTestCase ...@@ -36,6 +38,7 @@ class ImagyTest extends MediaTestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->app['config']->set('asgard.media.config.files-path', '/assets/media/');
$this->config = App::make(Repository::class); $this->config = App::make(Repository::class);
$this->finder = App::make(Filesystem::class); $this->finder = App::make(Filesystem::class);
$this->imagy = new Imagy(new InterventionFactory(), app(ThumbnailManager::class), $this->config); $this->imagy = new Imagy(new InterventionFactory(), app(ThumbnailManager::class), $this->config);
...@@ -49,6 +52,9 @@ class ImagyTest extends MediaTestCase ...@@ -49,6 +52,9 @@ class ImagyTest extends MediaTestCase
{ {
$this->finder->delete("{$this->testbenchPublicPath}google-map.png"); $this->finder->delete("{$this->testbenchPublicPath}google-map.png");
$this->finder->delete("{$this->testbenchPublicPath}google-map_smallThumb.png"); $this->finder->delete("{$this->testbenchPublicPath}google-map_smallThumb.png");
if ($this->app['files']->isDirectory(public_path('assets')) === true) {
$this->app['files']->deleteDirectory(public_path('assets'));
}
} }
public function it_should_create_a_file() public function it_should_create_a_file()
...@@ -70,13 +76,38 @@ class ImagyTest extends MediaTestCase ...@@ -70,13 +76,38 @@ class ImagyTest extends MediaTestCase
/** @test */ /** @test */
public function it_should_return_thumbnail_path() public function it_should_return_thumbnail_path()
{ {
$path = $this->imagy->getThumbnail("{$this->mediaPath}google-map.png", 'smallThumb'); $this->resetDatabase();
$expected = config('app.url') . DIRECTORY_SEPARATOR . config('asgard.media.config.files-path') . 'google-map_smallThumb.png'; $file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$file = app(FileService::class)->store($file);
$expected = config('app.url') . config('asgard.media.config.files-path') . 'my-file_smallThumb.jpg';
$path = $this->imagy->getThumbnail($file->path, 'smallThumb');
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-file.jpg')));
$this->assertEquals($expected, $path); $this->assertEquals($expected, $path);
} }
/** @test */
public function it_should_return_thumbnail_path_to_sub_folders()
{
$this->resetDatabase();
app(FolderRepository::class)->create(['name' => 'My Folder', 'parent_id' => 0]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$file = app(FileService::class)->store($file, 1);
$this->assertTrue($this->app['files']->exists(public_path('assets/media/my-folder/my-file.jpg')));
$smallThumbPath = $this->imagy->getThumbnail($file->path, 'smallThumb');
$expected = config('app.url') . config('asgard.media.config.files-path') . 'my-folder/my-file_smallThumb.jpg';
$this->assertEquals($expected, $smallThumbPath);
}
/** @test */ /** @test */
public function it_should_return_same_path_for_non_images() public function it_should_return_same_path_for_non_images()
{ {
......
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