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

add validator upload file #284

Merged
merged 6 commits into from Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion MediaAdmin/FileAlternatives/FileAlternativesManager.php
Expand Up @@ -35,7 +35,7 @@ public function addStrategy(FileAlternativesStrategyInterface $strategy)
*
* @param MediaInterface $media
*
* @return srtring
* @return string
*/
public function getMediaType(MediaInterface $media) {
foreach ($this->strategies as $strategy) {
Expand Down
8 changes: 6 additions & 2 deletions MediaAdminBundle/Controller/Admin/MediaController.php
Expand Up @@ -57,7 +57,9 @@ public function cropAction(Request $request, $mediaId)
$data['format']
);

$this->get('open_orchestra_media_admin.manager.save_media')->saveMedia($media);
$objectManager = $this->get('object_manager');
$objectManager->persist($media);
$objectManager->flush();

$this->dispatchEvent(MediaEvents::MEDIA_UPDATE, new MediaEvent($media));
}
Expand Down Expand Up @@ -127,7 +129,9 @@ public function overrideAction(Request $request, $format, $mediaId)
$this->get('open_orchestra_media_admin.file_alternatives.manager')
->overrideAlternative($media, $tmpDir . DIRECTORY_SEPARATOR . $tmpFileName, $format);

$this->get('open_orchestra_media_admin.manager.save_media')->saveMedia($media);
$objectManager = $this->get('object_manager');
$objectManager->persist($media);
$objectManager->flush();

$this->dispatchEvent(MediaEvents::MEDIA_UPDATE, new MediaEvent($media));
}
Expand Down
39 changes: 28 additions & 11 deletions MediaAdminBundle/Controller/Api/MediaController.php
Expand Up @@ -13,6 +13,7 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Config;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationListInterface;

/**
* Class MediaController
Expand Down Expand Up @@ -120,27 +121,28 @@ public function uploadAction($folderId, Request $request)
$uploadedFile = $request->files->get('file');
$saveMediaManager = $this->get('open_orchestra_media_admin.manager.save_media');

if ($uploadedFile && $filename = $saveMediaManager->getFilenameFromChunks($uploadedFile)) {
if ($uploadedFile && $uploadedFile = $saveMediaManager->getFileFromChunks($uploadedFile)) {
$media = $saveMediaManager->initializeMediaFromUploadedFile($uploadedFile, $folderId);
$violations = $this->get('validator')->validate($media);

if ($saveMediaManager->isFileAllowed($filename)) {
$media = $saveMediaManager->createMediaFromUploadedFile($uploadedFile, $filename, $folderId);

return $this->get('open_orchestra_api.transformer_manager')->get('media')->transform($media);
if (count($violations) !== 0) {
return new Response(
implode('.', $this->getViolationsMessage($violations)),
Copy link
Member

Choose a reason for hiding this comment

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

@alavieille : '.' or . or include ul/li ?

403
);
}

$translator = $this->container->get('translator');
$saveMediaManager->saveMedia($media);

return $this->get('open_orchestra_api.transformer_manager')->get('media')->transform($media);

return new Response(
$translator->trans('open_orchestra_media_admin.form.upload.not_allowed'),
403
);
}

return new Response('', 202);
}

/**
* @param string folderId
* @param string $folderId
*
* @Config\Route("/{folderId}/media-types", name="open_orchestra_api_media_type_list")
* Config\Method({"POST"})
Expand All @@ -158,4 +160,19 @@ public function mediaTypeListAction($folderId)
return $this->get('open_orchestra_api.transformer_manager')
->get('media_type_collection')->transform($mediaCollection, $folderId);
}

/**
* @param ConstraintViolationListInterface $violations
*
* @return array
*/
protected function getViolationsMessage(ConstraintViolationListInterface $violations)
{
$messages = array();
foreach ($violations as $violation) {
$messages[] = $violation->getMessage();
}

return $messages;
}
}
4 changes: 3 additions & 1 deletion MediaAdminBundle/DataFixtures/MongoDB/LoadMediaData.php
Expand Up @@ -94,7 +94,7 @@ protected function generateMedia(
$uploadedFile = new UploadedFile($tmpFilePath, $fileName, $mimeType);

$saveMediaManager = $this->container->get('open_orchestra_media_admin.manager.save_media');
$media = $saveMediaManager->createMediaFromUploadedFile($uploadedFile, $fileName, $folderId);
$media = $saveMediaManager->initializeMediaFromUploadedFile($uploadedFile, $folderId);

$media->setName($name);
foreach ($keywordReferencesArray as $keywordReference) {
Expand All @@ -105,6 +105,8 @@ protected function generateMedia(
$media->addTitle($language, $labels['title']);
}

$saveMediaManager->saveMedia($media);

return $media;
}

Expand Down
Expand Up @@ -24,13 +24,13 @@ public function process(ContainerBuilder $container)
$formResources[] = 'OpenOrchestraMediaAdminBundle:Form:form_div_layout.html.twig';
$container->setParameter('twig.form.resources', $formResources);

if ($container->hasParameter('open_orchestra_media_admin.allowed_mime_type')) {
if ($container->hasParameter('open_orchestra_media.allowed_mime_type')) {
$twig = $container->getDefinition('twig');
$twig->addMethodCall(
'addGlobal',
array(
'media_allowed_mime_types',
$container->getParameter('open_orchestra_media_admin.allowed_mime_type')
$container->getParameter('open_orchestra_media.allowed_mime_type')
)
);
}
Expand Down
Expand Up @@ -51,7 +51,6 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('display.yml');
$loader->load('icon.yml');
$loader->load('file_utils.yml');
$loader->load('mime_type.yml');
$loader->load('file_alternatives.yml');
$loader->load('voter.yml');
$loader->load('role_parameter.yml');
Expand Down
90 changes: 61 additions & 29 deletions MediaAdminBundle/Manager/SaveMediaManager.php
Expand Up @@ -57,30 +57,53 @@ public function __construct(
* If yes, return the name of the reassembled temporary file
*
* @param UploadedFile $uploadedFile
*
*
* @deprecated will be remove in 2.0 use getFileFromChunks
*
* @return string|null
*/
public function getFilenameFromChunks(UploadedFile $uploadedFile)
{
$filename = time() . '-' . $uploadedFile->getClientOriginalName();
@trigger_error('The '.__NAMESPACE__.'\getFilenameFromChunks method is deprecated since version 1.2.0 and will be removed in 2.0', E_USER_DEPRECATED);

$filename = time() . '-' . $uploadedFile->getClientOriginalName();
if (FlowBasic::save($this->tmpDir . DIRECTORY_SEPARATOR . $filename, $this->tmpDir)) {

return $filename;
}

return null;
}

/**
* Return true if the file is allowed to be uploaded based on its mime type
*
* @param $filename
*
* Check if all chunks of a file being uploaded have been received
* If yes, return the name of the reassembled temporary file
*
* @param UploadedFile $uploadedFile
*
* @return UploadedFile|null
*/
public function getFileFromChunks(UploadedFile $uploadedFile)
{
$filename = time() . '-' . $uploadedFile->getClientOriginalName();
$path = $this->tmpDir . DIRECTORY_SEPARATOR . $filename;

if (FlowBasic::save($path, $this->tmpDir)) {
return new UploadedFile($path, $uploadedFile->getClientOriginalName(), $uploadedFile->getClientMimeType());
}

return null;
}

/**
* @param UploadedFile $filename
*
* @return bool
*
* @deprecated will be remove in 2.0
*/
public function isFileAllowed($filename)
{
@trigger_error('The '.__NAMESPACE__.'\isFileAllowed method is deprecated since version 1.2.0 and will be removed in 2.0', E_USER_DEPRECATED);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileMimeType = finfo_file($finfo, $this->tmpDir . '/' . $filename);

Expand All @@ -89,57 +112,66 @@ public function isFileAllowed($filename)

/**
* Create a media to fit an uploaded file
*
*
* @param UploadedFile $uploadedFile
* @param string $filename
* @param string $folderId
*
*
* @return MediaInterface
* @deprecated will be remove in 2.0, use initializeMediaFromUploadedFile
*/
public function createMediaFromUploadedFile(UploadedFile $uploadedFile, $filename, $folderId)
{
@trigger_error('The '.__NAMESPACE__.'\createMediaFromUploadedFile method is deprecated since version 1.2.0 and will be removed in 2.0', E_USER_DEPRECATED);

$media = new $this->mediaClass();
$media->setFile($uploadedFile);
$media->setFilesystemName($filename);
$media->setMediaFolder($this->folderRepository->find($folderId));
$media = $this->processMedia($media, $uploadedFile, $filename);
$media->setName($uploadedFile->getClientOriginalName());
$media->setMimeType($uploadedFile->getClientMimeType());

$this->saveMedia($media);

$event = new MediaEvent($media);
$this->dispatcher->dispatch(MediaEvents::MEDIA_ADD, $event);

return $media;
}

/**
* Save a media in database
*
* @param MediaInterface $media
* initialize a media to fit an uploaded file
*
* @param UploadedFile $uploadedFile
* @param string $folderId
*
* @return MediaInterface
*/
public function saveMedia($media)
public function initializeMediaFromUploadedFile(UploadedFile $uploadedFile, $folderId)
{
$this->objectManager->persist($media);
$this->objectManager->flush();
/** @var MediaInterface $media */
$media = new $this->mediaClass();
$media->setFile($uploadedFile);
$media->setFilesystemName($uploadedFile->getFilename());
$media->setMediaFolder($this->folderRepository->find($folderId));
$media->setName($uploadedFile->getClientOriginalName());
$media->setMimeType($uploadedFile->getMimeType());

return $media;
}

/**
* Process $uploadedFile (thumbnail + storage) and attach it to $media
* Save a media in database
*
* @param MediaInterface $media
* @param UploadedFile $uploadedFile
* @param string $filename
*
* @return MediaInterface
*/
protected function processMedia($media, $uploadedFile, $filename)
public function saveMedia($media)
{
$media->setName($uploadedFile->getClientOriginalName());
$media->setMimeType($uploadedFile->getClientMimeType());
$file = $media->getFile();
$this->mediaStorageManager->uploadFile($file->getFilename(), $file->getRealPath(), false);

$this->mediaStorageManager->uploadFile($filename, $this->tmpDir . '/' . $filename, false);
$this->objectManager->persist($media);
$this->objectManager->flush();

return $media;
$event = new MediaEvent($media);
$this->dispatcher->dispatch(MediaEvents::MEDIA_ADD, $event);
}
}

31 changes: 28 additions & 3 deletions MediaAdminBundle/Manager/SaveMediaManagerInterface.php
Expand Up @@ -12,31 +12,56 @@ interface SaveMediaManagerInterface
{
/**
* @param UploadedFile $uploadedFile
*
*
* @deprecated will be remove in 2.0 use getFileFromChunks
*
* @return string|null
*/
public function getFilenameFromChunks(UploadedFile $uploadedFile);

/**
* Check if all chunks of a file being uploaded have been received
* If yes, return the name of the reassembled temporary file
*
* @param UploadedFile $uploadedFile
*
* @return UploadedFile|null
*/
public function getFileFromChunks(UploadedFile $uploadedFile);

/**
* Return true if the file is allowed to be uploaded based on its mime type
*
* @param string $filename
*
* @return bool
*
* @deprecated will be remove in 2.0
*/
public function isFileAllowed($filename);

/**
* Create a media to fit an uploaded file
*
*
* @param UploadedFile $uploadedFile
* @param string $filename
* @param string $folderId
*
*
* @return MediaInterface
* @deprecated will be remove in 2.0, use initializeMediaFromUploadedFile
*/
public function createMediaFromUploadedFile(UploadedFile $uploadedFile, $filename, $folderId);

/**
* initialize a media to fit an uploaded file
*
* @param UploadedFile $uploadedFile
* @param string $folderId
*
* @return MediaInterface
*/
public function initializeMediaFromUploadedFile(UploadedFile $uploadedFile, $folderId);

/**
* Save a media in database
*
Expand Down
2 changes: 1 addition & 1 deletion MediaAdminBundle/Resources/config/form.yml
Expand Up @@ -20,7 +20,7 @@ services:
class: '%open_orchestra_media_admin.type.media.class%'
arguments:
- '%open_orchestra_media.document.media.class%'
- '%open_orchestra_media_admin.allowed_mime_type%'
- '%open_orchestra_media.allowed_mime_type%'
tags:
- { name: form.type, alias: oo_media}
open_orchestra_media_admin.type.folder:
Expand Down
2 changes: 1 addition & 1 deletion MediaAdminBundle/Resources/config/manager.yml
Expand Up @@ -12,7 +12,7 @@ services:
arguments:
- '%open_orchestra_media_admin.tmp_dir%'
- '@open_orchestra_media_file.manager.storage'
- '%open_orchestra_media_admin.allowed_mime_type%'
- '%open_orchestra_media.allowed_mime_type%'
- '@object_manager'
- '@open_orchestra_media.repository.media_folder'
- '%open_orchestra_media.document.media.class%'
Expand Down
14 changes: 0 additions & 14 deletions MediaAdminBundle/Resources/config/mime_type.yml

This file was deleted.

1 change: 0 additions & 1 deletion MediaAdminBundle/Resources/translations/messages.en.yml
Expand Up @@ -74,7 +74,6 @@ open_orchestra_media_admin:
failed: "file could not be uploaded:"
remaining: remaining
completed: Completed
not_allowed: Format not allowed
group:
media_folder_role_edition: Media role list
media_folder_list: Media folder list
Expand Down
1 change: 0 additions & 1 deletion MediaAdminBundle/Resources/translations/messages.fr.yml
Expand Up @@ -73,7 +73,6 @@ open_orchestra_media_admin:
failed: "le fichier n'a pu être uploadé :"
remaining: restantes
completed: Achevé
not_allowed: Format non autorisé
group:
media_folder_role_edition: Liste des rôles par dossier de média
media_folder_list: Liste des dossiers de média
Expand Down