Ability to move a folder and its content back to root

parent f5001e8d
......@@ -51,7 +51,7 @@ class MoveFolderOnDisk
$oldPath = $media->path->getRelativeUrl();
$media->update([
'path' => str_replace($previousPath, $newPath, $oldPath),
'path' => $this->removeDoubleSlashes(str_replace($previousPath, $newPath, $oldPath)),
]);
if ($media->isFolder() === true) {
$this->replacePathReferences($media->id, $previousPath, $newPath);
......@@ -84,4 +84,9 @@ class MoveFolderOnDisk
{
return config('asgard.media.config.filesystem');
}
private function removeDoubleSlashes(string $string) : string
{
return str_replace('//', '/', $string);
}
}
......@@ -109,7 +109,12 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
private function getNewPathFor(string $filename, File $folder)
{
return $folder->path->getRelativeUrl() . '/' . str_slug($filename);
return $this->removeDoubleSlashes($folder->path->getRelativeUrl() . '/' . str_slug($filename));
}
private function removeDoubleSlashes(string $string) : string
{
return str_replace('//', '/', $string);
}
/**
......
......@@ -381,6 +381,28 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertEquals('/assets/media/my-folder/second-folder/third-folder/my-other-file.jpg', $fileTwo->path->getRelativeUrl());
}
/** @test */
public function it_can_move_folder_back_to_root_folder()
{
$parentFolder = $this->folder->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $this->folder->create(['name' => 'Child Folder', 'parent_id' => $parentFolder->id]);
$file = app(FileService::class)->store(\Illuminate\Http\UploadedFile::fake()->image('my-file.jpg'), $folder->id);
$this->assertEquals('/assets/media/my-folder/child-folder', $folder->path->getRelativeUrl());
$this->assertEquals('/assets/media/my-folder/child-folder/my-file.jpg', $file->path->getRelativeUrl());
$this->assertTrue($this->app['files']->isDirectory(public_path('/assets/media/my-folder/child-folder')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.jpg')));
$this->folder->move($folder, $this->makeRootFolder());
$this->assertTrue($this->app['files']->isDirectory(public_path('/assets/media/child-folder')));
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/child-folder/my-file.jpg')));
$this->assertEquals('/assets/media/child-folder', $folder->path->getRelativeUrl());
$file->refresh();
$this->assertEquals('/assets/media/child-folder/my-file.jpg', $file->path->getRelativeUrl());
}
/** @test */
public function it_can_move_folder_with_folders_and_files_in_it_disk()
{
......@@ -414,4 +436,13 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
'folder_id' => 0,
]);
}
private function makeRootFolder() : File
{
return new File([
'id' => 0,
'folder_id' => 0,
'path' => config('asgard.media.config.files-path'),
]);
}
}
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