From 1624b2130a7181c516d801d4eece45d59a2586b0 Mon Sep 17 00:00:00 2001 From: An Truong Date: Wed, 4 Aug 2021 10:58:08 +0200 Subject: [PATCH 1/3] add slugifier for renaming media urls Fix removing dirs --- .../Command/RenameSoftDeletedCommand.php | 17 ++++++++++++++++- .../MediaBundle/Helper/File/FileHandler.php | 1 - 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php index 9c754b0c08..4bf422b034 100644 --- a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php +++ b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php @@ -25,11 +25,16 @@ class RenameSoftDeletedCommand extends ContainerAwareCommand */ private $mediaManager; + /** + * @var SlugifierInterface + */ + private $slugifier; + /** * @param EntityManagerInterface|null $em * @param MediaManager|null $mediaManager */ - public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null) + public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null, $slugifier = null) { parent::__construct(); @@ -43,6 +48,7 @@ public function __construct(/* EntityManagerInterface */ $em = null, /* MediaMan $this->em = $em; $this->mediaManager = $mediaManager; + $this->slugifier = $slugifier; } protected function configure() @@ -70,6 +76,10 @@ public function execute(InputInterface $input, OutputInterface $output) $this->mediaManager = $this->getContainer()->get('kunstmaan_media.media_manager'); } + if (null === $this->slugifier) { + $this->slugifier = $this->getContainer()->get('kunstmaan_utilities.slugifier'); + } + $output->writeln('Renaming soft-deleted media...'); $original = $input->getOption('original'); @@ -85,6 +95,11 @@ public function execute(InputInterface $input, OutputInterface $output) if ($media->isDeleted() && $media->getLocation() === 'local' && $handler instanceof FileHandler) { $oldFileUrl = $media->getUrl(); $newFileName = ($original ? $media->getOriginalFilename() : uniqid() . '.' . pathinfo($oldFileUrl, PATHINFO_EXTENSION)); + $parts = pathinfo($newFileName); + $newFileName = $this->slugifier->slugify($parts['filename']); + if (\array_key_exists('extension', $parts)) { + $newFileName .= '.'.strtolower($parts['extension']); + } $newFileUrl = \dirname($oldFileUrl) . '/' . $newFileName; $fileRenameQueue[] = array($oldFileUrl, $newFileUrl, $handler); $media->setUrl($newFileUrl); diff --git a/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php b/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php index 480c1a487e..6207cf8556 100644 --- a/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php +++ b/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php @@ -214,7 +214,6 @@ public function removeMedia(Media $media) if ($adapter->exists($fileKey)) { $adapter->delete($fileKey); } - // Remove the files containing folder if there's nothing left $folderPath = $this->getFileFolderPath($media); if ($adapter->exists($folderPath) && $adapter->isDirectory($folderPath) && !empty($folderPath)) { From 190ce8f15bda2aa1f94419ce57a3626f49a96547 Mon Sep 17 00:00:00 2001 From: An Truong Date: Wed, 4 Aug 2021 10:58:08 +0200 Subject: [PATCH 2/3] add slugifier for renaming media urls Fix removing dirs --- .../Command/RenameSoftDeletedCommand.php | 17 ++++++++++++++++- .../MediaBundle/Helper/File/FileHandler.php | 1 - 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php index 5274af1c3b..bb3230cd1a 100644 --- a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php +++ b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php @@ -25,11 +25,16 @@ class RenameSoftDeletedCommand extends ContainerAwareCommand */ private $mediaManager; + /** + * @var SlugifierInterface + */ + private $slugifier; + /** * @param EntityManagerInterface|null $em * @param MediaManager|null $mediaManager */ - public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null) + public function __construct(/* EntityManagerInterface */ $em = null, /* MediaManager */ $mediaManager = null, $slugifier = null) { parent::__construct(); @@ -43,6 +48,7 @@ public function __construct(/* EntityManagerInterface */ $em = null, /* MediaMan $this->em = $em; $this->mediaManager = $mediaManager; + $this->slugifier = $slugifier; } protected function configure() @@ -70,6 +76,10 @@ public function execute(InputInterface $input, OutputInterface $output) $this->mediaManager = $this->getContainer()->get('kunstmaan_media.media_manager'); } + if (null === $this->slugifier) { + $this->slugifier = $this->getContainer()->get('kunstmaan_utilities.slugifier'); + } + $output->writeln('Renaming soft-deleted media...'); $original = $input->getOption('original'); @@ -85,6 +95,11 @@ public function execute(InputInterface $input, OutputInterface $output) if ($media->isDeleted() && $media->getLocation() === 'local' && $handler instanceof FileHandler) { $oldFileUrl = $media->getUrl(); $newFileName = ($original ? $media->getOriginalFilename() : uniqid() . '.' . pathinfo($oldFileUrl, PATHINFO_EXTENSION)); + $parts = pathinfo($newFileName); + $newFileName = $this->slugifier->slugify($parts['filename']); + if (\array_key_exists('extension', $parts)) { + $newFileName .= '.'.strtolower($parts['extension']); + } $newFileUrl = \dirname($oldFileUrl) . '/' . $newFileName; $fileRenameQueue[] = [$oldFileUrl, $newFileUrl, $handler]; $media->setUrl($newFileUrl); diff --git a/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php b/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php index fa3ea57f5e..1692933ba9 100644 --- a/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php +++ b/src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php @@ -198,7 +198,6 @@ public function removeMedia(Media $media) if ($adapter->exists($fileKey)) { $adapter->delete($fileKey); } - // Remove the files containing folder if there's nothing left $folderPath = $this->getFileFolderPath($media); if ($adapter->exists($folderPath) && $adapter->isDirectory($folderPath) && !empty($folderPath)) { From 259ee68893afbedae4fa90af784dfb4b56ed233e Mon Sep 17 00:00:00 2001 From: An Truong Date: Wed, 19 Jan 2022 13:17:18 +0100 Subject: [PATCH 3/3] fixed for renaming soft deleted media. Get only soft deleted media, excluding the removed from file system --- src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php index bb3230cd1a..dbd4e8d517 100644 --- a/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php +++ b/src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php @@ -83,7 +83,7 @@ public function execute(InputInterface $input, OutputInterface $output) $output->writeln('Renaming soft-deleted media...'); $original = $input->getOption('original'); - $medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAll(); + $medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAllDeleted(); $updates = 0; $fileRenameQueue = [];