Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MediaBundle] Fix for deleting media #3043

Open
wants to merge 4 commits into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Kunstmaan/MediaBundle/Command/RenameSoftDeletedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -43,6 +48,7 @@ public function __construct(/* EntityManagerInterface */ $em = null, /* MediaMan

$this->em = $em;
$this->mediaManager = $mediaManager;
$this->slugifier = $slugifier;
}

protected function configure()
Expand Down Expand Up @@ -70,10 +76,14 @@ 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');
$medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAll();
$medias = $this->em->getRepository('KunstmaanMediaBundle:Media')->findAllDeleted();
$updates = 0;
$fileRenameQueue = [];

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down