Ability to move media back on root

parent 8c60d825
......@@ -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',
......
......@@ -4,6 +4,7 @@ namespace Modules\Media\Http\Controllers\Api;
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 @@ class MoveMediaController extends Controller
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 @@ class MoveMediaController extends Controller
'message' => 'Files moved successfully',
]);
}
private function makeRootFolder() : File
{
return new File([
'id' => 0,
'folder_id' => 0,
]);
}
}
......@@ -309,6 +309,40 @@ class EloquentFileRepositoryTest extends MediaTestCase
$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 @@ class EloquentFileRepositoryTest extends MediaTestCase
'folder_id' => 0,
]);
}
private function makeRootFolder() : File
{
return new File([
'id' => 0,
'folder_id' => 0,
]);
}
}
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