It can recursively update folders

parent 1228dd8f
......@@ -4,6 +4,7 @@ namespace Modules\Media\Events\Handlers;
use Illuminate\Contracts\Filesystem\Factory;
use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Repositories\FileRepository;
class RenameFolderOnDisk
{
......@@ -11,12 +12,32 @@ class RenameFolderOnDisk
* @var Factory
*/
private $filesystem;
/**
* @var FileRepository
*/
private $file;
public function __construct(Factory $filesystem)
public function __construct(Factory $filesystem, FileRepository $file)
{
$this->filesystem = $filesystem;
$this->file = $file;
}
public function handle(FolderWasUpdated $event)
{
$this->renameFolder($event);
$this->renameDatabaseReferences($event);
}
private function renameDatabaseReferences(FolderWasUpdated $event)
{
$previousPath = $event->previousFolderData['path']->getRelativeUrl();
$newPath = $event->folder->path->getRelativeUrl();
$this->replacePathReferences($event->folder->id, $previousPath, $newPath);
}
private function renameFolder($event)
{
$this->filesystem->disk($this->getConfiguredFilesystem())
->move(
......@@ -41,4 +62,19 @@ class RenameFolderOnDisk
{
return config('asgard.media.config.filesystem');
}
private function replacePathReferences($folderId, $previousPath, $newPath)
{
$medias = $this->file->allChildrenOf($folderId);
foreach ($medias as $media) {
$oldPath = $media->path->getRelativeUrl();
$media->update([
'path' => str_replace($previousPath, $newPath, $oldPath),
]);
if ($media->isFolder() === true) {
$this->replacePathReferences($media->id, $previousPath, $newPath);
}
}
}
}
......@@ -163,4 +163,13 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
return $media->paginate($request->get('per_page', 10));
}
/**
* @param int $folderId
* @return Collection
*/
public function allChildrenOf(int $folderId): Collection
{
return $this->model->where('folder_id', $folderId)->get();
}
}
......@@ -48,6 +48,7 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
'filename' => array_get($data, 'name'),
'path' => $this->getPath($data),
];
dd($previousData, $formattedData);
event($event = new FolderIsUpdating($formattedData));
$model->update($event->getAttributes());
......
......@@ -2,6 +2,7 @@
namespace Modules\Media\Repositories;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Modules\Core\Repositories\BaseRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
......@@ -37,4 +38,10 @@ interface FileRepository extends BaseRepository
* @return mixed
*/
public function serverPaginationFilteringFor(Request $request);
/**
* @param int $folderId
* @return Collection
*/
public function allChildrenOf(int $folderId) : Collection;
}
......@@ -189,4 +189,29 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertTrue($this->app['files']->exists(public_path('assets/media/new-name/my-file_smallThumb.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('assets/media/new-name/my-file_mediumThumb.jpg')));
}
/** @test */
public function it_renames_folder_and_database_references_to_that_folder()
{
$parentFolder = $this->folder->create(['name' => 'My Folder']);
$folderTwo = $this->folder->create(['name' => 'Child folder', 'parent_id' => $parentFolder->id]);
$file = \Illuminate\Http\UploadedFile::fake()->image('my-file.jpg');
$fileTwo = \Illuminate\Http\UploadedFile::fake()->image('my-second-file.jpg');
$fileThree = \Illuminate\Http\UploadedFile::fake()->image('my-third-file.jpg');
$fileOne = app(FileService::class)->store($file, $parentFolder->id);
$fileTwo = app(FileService::class)->store($fileTwo, $parentFolder->id);
$fileThree = app(FileService::class)->store($fileThree, $folderTwo->id);
$parentFolder = $this->folder->update($parentFolder, ['name' => 'New Name!']);
$this->assertTrue($this->app['files']->isDirectory(public_path('assets/media/new-name')));
$this->assertTrue($this->app['files']->exists(public_path('assets/media/new-name/my-file.jpg')));
$this->assertTrue($this->app['files']->exists(public_path('assets/media/new-name/child-folder/my-third-file.jpg')));
$fileOne->refresh();
$fileTwo->refresh();
$fileThree->refresh();
$this->assertEquals('/assets/media/new-name/my-file.jpg', $fileOne->path->getRelativeUrl());
$this->assertEquals('/assets/media/new-name/my-second-file.jpg', $fileTwo->path->getRelativeUrl());
$this->assertEquals('/assets/media/new-name/child-folder/my-third-file.jpg', $fileThree->path->getRelativeUrl());
}
}
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