Ability to move files with its thumbnails

parent 0a247740
......@@ -4,6 +4,9 @@ namespace Modules\Media\Events\Handlers;
use Illuminate\Contracts\Filesystem\Factory;
use Modules\Media\Events\FileStartedMoving;
use Modules\Media\Image\Thumbnail;
use Modules\Media\Image\ThumbnailManager;
use Modules\Media\ValueObjects\MediaPath;
class MoveFileOnDisk
{
......@@ -11,21 +14,65 @@ class MoveFileOnDisk
* @var Factory
*/
private $filesystem;
/**
* All the different images types where thumbnails should be created
* @var array
*/
private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
/**
* @var ThumbnailManager
*/
private $manager;
public function __construct(Factory $filesystem)
public function __construct(Factory $filesystem, ThumbnailManager $manager)
{
$this->filesystem = $filesystem;
$this->manager = $manager;
}
public function handle(FileStartedMoving $event)
{
$this->moveOriginal($event);
if ($this->isImage($event->previousData['path']->getRelativeUrl())) {
$this->moveThumbnails($event);
}
}
private function moveOriginal($event)
{
$this->move($event->previousData['path']->getRelativeUrl(), $event->file->path->getRelativeUrl());
}
private function moveThumbnails($event)
{
foreach ($this->manager->all() as $thumbnail) {
$fromPath = $this->getFilenameFor($event->previousData['path'], $thumbnail);
$toPath = $this->getFilenameFor($event->file->path, $thumbnail);
$this->move($fromPath, $toPath);
}
}
private function move($fromPath, $toPath)
{
$this->filesystem->disk($this->getConfiguredFilesystem())
->move(
$this->getDestinationPath($event->previousData['path']->getRelativeUrl()),
$this->getDestinationPath($event->file->path->getRelativeUrl())
$this->getDestinationPath($fromPath),
$this->getDestinationPath($toPath)
);
}
/**
* Check if the given path is en image
* @param string $path
* @return bool
*/
private function isImage($path)
{
return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions);
}
private function getDestinationPath($path) : string
{
if ($this->getConfiguredFilesystem() === 'local') {
......@@ -42,4 +89,52 @@ class MoveFileOnDisk
{
return config('asgard.media.config.filesystem');
}
/**
* @param MediaPath $path
* @param Thumbnail|string $thumbnail
* @return string
*/
private function getFilenameFor(MediaPath $path, $thumbnail)
{
if ($thumbnail instanceof Thumbnail) {
$thumbnail = $thumbnail->name();
}
$filenameWithoutPrefix = $this->removeConfigPrefix($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);
}
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);
}
/**
* Prepend the thumbnail name to filename
* @param $path
* @param $thumbnail
* @return mixed|string
*/
private function newFilename($path, $thumbnail)
{
$filename = pathinfo($path, PATHINFO_FILENAME);
return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION);
}
}
......@@ -264,12 +264,13 @@ class EloquentFileRepositoryTest extends MediaTestCase
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $folderRepository->create(['name' => 'Child Folder', 'parent_id' => $parentFolder->id]);
$file = $this->createFile('my-file.jpg');
$file = \Illuminate\Http\UploadedFile::fake()->create('my-file.pdf');
$file = app(FileService::class)->store($file);
$file = $this->file->move($file, $folder);
$this->assertEquals('my-file.jpg', $file->filename);
$this->assertEquals('/assets/media/my-folder/child-folder/my-file.jpg', $file->path->getRelativeUrl());
$this->assertEquals('my-file.pdf', $file->filename);
$this->assertEquals('/assets/media/my-folder/child-folder/my-file.pdf', $file->path->getRelativeUrl());
}
/** @test */
......@@ -279,13 +280,32 @@ class EloquentFileRepositoryTest extends MediaTestCase
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $folderRepository->create(['name' => 'Child Folder', 'parent_id' => $parentFolder->id]);
$file = \Illuminate\Http\UploadedFile::fake()->create('my-file.pdf');
$file = app(FileService::class)->store($file);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.pdf')));
$this->file->move($file, $folder);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.pdf')));
}
/** @test */
public function it_can_move_file_with_thumbnails_on_disk()
{
$folderRepository = app(FolderRepository::class);
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $folderRepository->create(['name' => 'Child Folder', 'parent_id' => $parentFolder->id]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$file = app(FileService::class)->store($file);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg')));
$this->file->move($file, $folder);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file_smallThumb.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file_mediumThumb.jpg')));
}
private function createFile($fileName = 'random/name.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