-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding login to move folders to new locations
- Loading branch information
Showing
6 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Modules\Media\Events; | ||
|
||
use Modules\Media\Entities\File; | ||
|
||
class FolderStartedMoving | ||
{ | ||
/** | ||
* @var File | ||
*/ | ||
public $folder; | ||
/** | ||
* @var array | ||
*/ | ||
public $previousData; | ||
|
||
public function __construct(File $folder, array $previousData) | ||
{ | ||
$this->folder = $folder; | ||
$this->previousData = $previousData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Modules\Media\Events\Handlers; | ||
|
||
use Illuminate\Contracts\Filesystem\Factory; | ||
use Modules\Media\Events\FolderStartedMoving; | ||
use Modules\Media\Repositories\FileRepository; | ||
|
||
class MoveFolderOnDisk | ||
{ | ||
/** | ||
* @var Factory | ||
*/ | ||
private $filesystem; | ||
/** | ||
* @var FileRepository | ||
*/ | ||
private $file; | ||
|
||
public function __construct(Factory $filesystem, FileRepository $file) | ||
{ | ||
$this->filesystem = $filesystem; | ||
$this->file = $file; | ||
} | ||
|
||
public function handle(FolderStartedMoving $event) | ||
{ | ||
$this->moveOriginal($event); | ||
|
||
$this->renameDatabaseReferences($event); | ||
} | ||
|
||
private function moveOriginal(FolderStartedMoving $event) | ||
{ | ||
$this->move($event->previousData['path']->getRelativeUrl(), $event->folder->path->getRelativeUrl()); | ||
} | ||
|
||
private function renameDatabaseReferences(FolderStartedMoving $event) | ||
{ | ||
$previousPath = $event->previousData['path']->getRelativeUrl(); | ||
$newPath = $event->folder->path->getRelativeUrl(); | ||
|
||
$this->replacePathReferences($event->folder->id, $previousPath, $newPath); | ||
} | ||
|
||
private function replacePathReferences($folderId, $previousPath, $newPath) | ||
{ | ||
$medias = $this->file->allChildrenOf($folderId); | ||
|
||
foreach ($medias as $media) { | ||
$oldPath = $media->path->getRelativeUrl(); | ||
|
||
$media->update([ | ||
'path' => str_replace($previousPath, $newPath, $oldPath), | ||
]); | ||
if ($media->isFolder() === true) { | ||
$this->replacePathReferences($media->id, $previousPath, $newPath); | ||
} | ||
} | ||
} | ||
|
||
private function move($fromPath, $toPath) | ||
{ | ||
$this->filesystem->disk($this->getConfiguredFilesystem()) | ||
->move( | ||
$this->getDestinationPath($fromPath), | ||
$this->getDestinationPath($toPath) | ||
); | ||
} | ||
|
||
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters