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]: check if file exists #1396

Merged
merged 1 commit into from
Dec 9, 2016
Merged
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
17 changes: 11 additions & 6 deletions src/Kunstmaan/MediaBundle/EventListener/DoctrineMediaListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function preUpdate(PreUpdateEventArgs $eventArgs)
if ($deleted || $reverted) {
$oldFileUrl = $entity->getUrl();
$newFileName = ($reverted ? $entity->getOriginalFilename() : uniqid());
$newFileUrl = dirname($oldFileUrl) . '/' . $newFileName . '.' . pathinfo($oldFileUrl, PATHINFO_EXTENSION);
$newFileUrl = dirname($oldFileUrl).'/'.$newFileName.'.'.pathinfo($oldFileUrl, PATHINFO_EXTENSION);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we are changing code style fort dot operand wrapping? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that's the coding standard ;)

Add a single space around binary operators (==, &&, ...), with the exception of the concatenation (.) operator;

$entity->setUrl($newFileUrl);
$this->fileUrlMap[$newFileUrl] = $oldFileUrl;
}
Expand All @@ -98,7 +98,7 @@ public function postPersist(LifecycleEventArgs $eventArgs)

/**
* @param object $entity The entity
* @param bool $new Is new
* @param bool $new Is new
*/
private function saveMedia($entity, $new = false)
{
Expand All @@ -110,10 +110,15 @@ private function saveMedia($entity, $new = false)
$url = $entity->getUrl();
$handler = $this->mediaManager->getHandler($entity);
if (isset($this->fileUrlMap[$url]) && $handler instanceof FileHandler) {
$handler->fileSystem->rename(
preg_replace('~^' . preg_quote($handler->mediaPath, '~') . '~', '/', $this->fileUrlMap[$url]),
preg_replace('~^' . preg_quote($handler->mediaPath, '~') . '~', '/', $url)
);
$regex = '~^'.preg_quote($handler->mediaPath, '~').'~';
$originalFileName = preg_replace($regex, '/', $this->fileUrlMap[$url]);
// Check if file exists on filesystem.
if ($handler->fileSystem->has($originalFileName)) {
$handler->fileSystem->rename(
$originalFileName,
preg_replace($regex, '/', $url)
);
}
unset($this->fileUrlMap[$url]);
}
}
Expand Down