Adding logic to move files

parent ac8e803d
<?php
namespace Modules\Media\Events;
use Modules\Media\Entities\File;
class FileStartedMoving
{
/**
* @var File
*/
public $file;
/**
* @var array
*/
public $previousData;
public function __construct(File $file, array $previousData)
{
$this->file = $file;
$this->previousData = $previousData;
}
}
<?php
namespace Modules\Media\Events\Handlers;
use Illuminate\Contracts\Filesystem\Factory;
use Modules\Media\Events\FileStartedMoving;
class MoveFileOnDisk
{
/**
* @var Factory
*/
private $filesystem;
public function __construct(Factory $filesystem)
{
$this->filesystem = $filesystem;
}
public function handle(FileStartedMoving $event)
{
$this->filesystem->disk($this->getConfiguredFilesystem())
->move(
$this->getDestinationPath($event->previousData['path']->getRelativeUrl()),
$this->getDestinationPath($event->file->path->getRelativeUrl())
);
}
private function getDestinationPath($path) : string
{
if ($this->getConfiguredFilesystem() === 'local') {
return basename(public_path()) . $path;
}
return $path;
}
/**
* @return string
*/
private function getConfiguredFilesystem() : string
{
return config('asgard.media.config.filesystem');
}
}
...@@ -20,7 +20,9 @@ use Modules\Media\Events\FolderWasUpdated; ...@@ -20,7 +20,9 @@ use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Events\Handlers\CreateFolderOnDisk; use Modules\Media\Events\Handlers\CreateFolderOnDisk;
use Modules\Media\Events\Handlers\DeleteAllChildrenOfFolder; use Modules\Media\Events\Handlers\DeleteAllChildrenOfFolder;
use Modules\Media\Events\Handlers\DeleteFolderOnDisk; use Modules\Media\Events\Handlers\DeleteFolderOnDisk;
use Modules\Media\Events\FileStartedMoving;
use Modules\Media\Events\Handlers\HandleMediaStorage; use Modules\Media\Events\Handlers\HandleMediaStorage;
use Modules\Media\Events\Handlers\MoveFileOnDisk;
use Modules\Media\Events\Handlers\RegisterMediaSidebar; use Modules\Media\Events\Handlers\RegisterMediaSidebar;
use Modules\Media\Events\Handlers\RemovePolymorphicLink; use Modules\Media\Events\Handlers\RemovePolymorphicLink;
use Modules\Media\Events\Handlers\RenameFolderOnDisk; use Modules\Media\Events\Handlers\RenameFolderOnDisk;
...@@ -80,6 +82,7 @@ class MediaServiceProvider extends ServiceProvider ...@@ -80,6 +82,7 @@ class MediaServiceProvider extends ServiceProvider
$events->listen(FolderWasUpdated::class, RenameFolderOnDisk::class); $events->listen(FolderWasUpdated::class, RenameFolderOnDisk::class);
$events->listen(FolderIsDeleting::class, DeleteFolderOnDisk::class); $events->listen(FolderIsDeleting::class, DeleteFolderOnDisk::class);
$events->listen(FolderIsDeleting::class, DeleteAllChildrenOfFolder::class); $events->listen(FolderIsDeleting::class, DeleteAllChildrenOfFolder::class);
$events->listen(FileStartedMoving::class, MoveFileOnDisk::class);
$this->app[TagManager::class]->registerNamespace(new File()); $this->app[TagManager::class]->registerNamespace(new File());
$this->registerThumbnails(); $this->registerThumbnails();
......
...@@ -8,6 +8,7 @@ use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; ...@@ -8,6 +8,7 @@ use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Entities\File; use Modules\Media\Entities\File;
use Modules\Media\Events\FileIsCreating; use Modules\Media\Events\FileIsCreating;
use Modules\Media\Events\FileIsUpdating; use Modules\Media\Events\FileIsUpdating;
use Modules\Media\Events\FileStartedMoving;
use Modules\Media\Events\FileWasCreated; use Modules\Media\Events\FileWasCreated;
use Modules\Media\Events\FileWasUpdated; use Modules\Media\Events\FileWasUpdated;
use Modules\Media\Helpers\FileHelper; use Modules\Media\Helpers\FileHelper;
...@@ -193,4 +194,20 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos ...@@ -193,4 +194,20 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
{ {
return $this->model->where('is_folder', 0)->get(); return $this->model->where('is_folder', 0)->get();
} }
public function move(File $file, File $destination) : File
{
$previousData = [
'filename' => $file->filename,
'path' => $file->path,
];
$this->update($file, [
'path' => $this->getPathFor($file->filename, $destination->id),
]);
event(new FileStartedMoving($file, $previousData));
return $file;
}
} }
...@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories; ...@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Modules\Core\Repositories\BaseRepository; use Modules\Core\Repositories\BaseRepository;
use Modules\Media\Entities\File;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
interface FileRepository extends BaseRepository interface FileRepository extends BaseRepository
...@@ -48,4 +49,6 @@ interface FileRepository extends BaseRepository ...@@ -48,4 +49,6 @@ interface FileRepository extends BaseRepository
public function findForVirtualPath(string $path); public function findForVirtualPath(string $path);
public function allForGrid() : Collection; public function allForGrid() : Collection;
public function move(File $file, File $destination) : File;
} }
...@@ -11,6 +11,7 @@ use Modules\Media\Events\FileWasCreated; ...@@ -11,6 +11,7 @@ use Modules\Media\Events\FileWasCreated;
use Modules\Media\Events\FileWasUpdated; use Modules\Media\Events\FileWasUpdated;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository; use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\FileService;
use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
...@@ -256,6 +257,37 @@ class EloquentFileRepositoryTest extends MediaTestCase ...@@ -256,6 +257,37 @@ class EloquentFileRepositoryTest extends MediaTestCase
$this->assertCount(2, $this->file->allForGrid()); $this->assertCount(2, $this->file->allForGrid());
} }
/** @test */
public function it_can_move_a_file_database()
{
$folderRepository = app(FolderRepository::class);
$parentFolder = $folderRepository->create(['name' => 'My Folder', 'parent_id' => 0]);
$folder = $folderRepository->create(['name' => 'Child Folder', 'parent_id' => $parentFolder->id]);
$file = $this->createFile('my-file.jpg');
$file = $this->file->move($file, $folder);
$this->assertEquals('my-file.jpg', $file->filename);
$this->assertEquals('/assets/media/my-folder/child-folder/my-file.jpg', $file->path->getRelativeUrl());
}
/** @test */
public function it_can_move_file_on_disk()
{
$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);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-file.jpg')));
$this->file->move($file, $folder);
$this->assertTrue($this->app['files']->exists(public_path('/assets/media/my-folder/child-folder/my-file.jpg')));
}
private function createFile($fileName = 'random/name.jpg') private function createFile($fileName = 'random/name.jpg')
{ {
return File::create([ return File::create([
......
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