Skip to content

Commit

Permalink
Ability to move media back on root
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Oct 8, 2017
1 parent 8c60d82 commit 59e1fbb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions Modules/Media/Entities/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions Modules/Media/Http/Controllers/Api/MoveMediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
]);
}
}
42 changes: 42 additions & 0 deletions Modules/Media/Tests/EloquentFileRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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,
]);
}
}

0 comments on commit 59e1fbb

Please sign in to comment.