diff --git a/Modules/Media/Entities/File.php b/Modules/Media/Entities/File.php index d1b1a62a0..3d273da13 100644 --- a/Modules/Media/Entities/File.php +++ b/Modules/Media/Entities/File.php @@ -28,6 +28,7 @@ class File extends Model implements TaggableInterface protected $table = 'media__files'; public $translatedAttributes = ['description', 'alt_attribute', 'keywords']; protected $fillable = [ + 'id', 'is_folder', 'description', 'alt_attribute', diff --git a/Modules/Media/Http/Controllers/Api/MoveMediaController.php b/Modules/Media/Http/Controllers/Api/MoveMediaController.php index 7afba00f3..2d4b19a0d 100644 --- a/Modules/Media/Http/Controllers/Api/MoveMediaController.php +++ b/Modules/Media/Http/Controllers/Api/MoveMediaController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Modules\Media\Entities\File; use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FolderRepository; @@ -31,6 +32,9 @@ public function __invoke(Request $request) if ($file->is_folder === false) { $destination = $this->folder->findFolder($request->get('destinationFolder')); + if ($destination === null) { + $destination = $this->makeRootFolder(); + } $this->file->move($file, $destination); } @@ -41,4 +45,12 @@ public function __invoke(Request $request) 'message' => 'Files moved successfully', ]); } + + private function makeRootFolder() : File + { + return new File([ + 'id' => 0, + 'folder_id' => 0, + ]); + } } diff --git a/Modules/Media/Tests/EloquentFileRepositoryTest.php b/Modules/Media/Tests/EloquentFileRepositoryTest.php index 66ef8799e..78ad3e1e8 100644 --- a/Modules/Media/Tests/EloquentFileRepositoryTest.php +++ b/Modules/Media/Tests/EloquentFileRepositoryTest.php @@ -309,6 +309,40 @@ public function it_can_move_file_with_thumbnails_on_disk() $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file_mediumThumb.jpg'))); } + /** @test */ + public function it_can_move_file_back_to_root_folder() + { + $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()->create('my-file.pdf'); + + $file = app(FileService::class)->store($file, $folder->id); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.pdf'))); + + $this->file->move($file, $this->makeRootFolder()); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.pdf'))); + } + + /** @test */ + public function it_can_move_file_with_thumbnails_back_to_root_folder() + { + $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, $folder->id); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.jpg'))); + + $this->file->move($file, $this->makeRootFolder()); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg'))); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file_smallThumb.jpg'))); + $this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file_mediumThumb.jpg'))); + } + private function createFile($fileName = 'random/name.jpg') { return File::create([ @@ -320,4 +354,12 @@ private function createFile($fileName = 'random/name.jpg') 'folder_id' => 0, ]); } + + private function makeRootFolder() : File + { + return new File([ + 'id' => 0, + 'folder_id' => 0, + ]); + } }