-
Notifications
You must be signed in to change notification settings - Fork 56
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
EZP-29104: Implemented UI for ImageAsset Field Type #580
Merged
alongosz
merged 1 commit into
ezsystems:master
from
adamwojs:EZP-29104-imageasset_clean
Sep 12, 2018
+1,066
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,164 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformAdminUiBundle\Controller; | ||
|
||
use Exception; | ||
use eZ\Publish\Core\FieldType\Image\Value as ImageValue; | ||
use eZ\Publish\Core\FieldType\ImageAsset\AssetMapper as ImageAssetMapper; | ||
use EzSystems\EzPlatformAdminUi\Form\Data\Asset\ImageAssetUploadData; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Csrf\CsrfToken; | ||
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
class AssetController extends Controller | ||
{ | ||
const CSRF_TOKEN_HEADER = 'X-CSRF-Token'; | ||
|
||
const LANGUAGE_CODE_KEY = 'languageCode'; | ||
const FILE_KEY = 'file'; | ||
|
||
/** @var \Symfony\Component\Validator\Validator\ValidatorInterface */ | ||
private $validator; | ||
|
||
/** @var \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface */ | ||
private $csrfTokenManager; | ||
|
||
/** @var \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper */ | ||
private $imageAssetMapper; | ||
|
||
/** @var \Symfony\Component\Translation\TranslatorInterface */ | ||
private $translator; | ||
|
||
/** | ||
* @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator | ||
* @param \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface $csrfTokenManager | ||
* @param \eZ\Publish\Core\FieldType\ImageAsset\AssetMapper $imageAssetMapper | ||
* @param \Symfony\Component\Translation\TranslatorInterface $translator | ||
*/ | ||
public function __construct( | ||
ValidatorInterface $validator, | ||
CsrfTokenManagerInterface $csrfTokenManager, | ||
ImageAssetMapper $imageAssetMapper, | ||
TranslatorInterface $translator) | ||
{ | ||
$this->validator = $validator; | ||
$this->csrfTokenManager = $csrfTokenManager; | ||
$this->imageAssetMapper = $imageAssetMapper; | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\HttpFoundation\Request $request | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* | ||
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType | ||
*/ | ||
public function uploadImageAction(Request $request): Response | ||
{ | ||
if ($this->isValidCsrfToken($request)) { | ||
$data = new ImageAssetUploadData( | ||
$request->files->get(self::FILE_KEY), | ||
$request->request->get(self::LANGUAGE_CODE_KEY) | ||
); | ||
|
||
$errors = $this->validator->validate($data); | ||
if ($errors->count() === 0) { | ||
try { | ||
$file = $data->getFile(); | ||
|
||
$content = $this->imageAssetMapper->createAsset( | ||
$file->getClientOriginalName(), | ||
new ImageValue([ | ||
'path' => $file->getRealPath(), | ||
'fileSize' => $file->getSize(), | ||
'fileName' => $file->getClientOriginalName(), | ||
'alternativeText' => null, | ||
]), | ||
$data->getLanguageCode() | ||
); | ||
|
||
return new JsonResponse([ | ||
'destinationContent' => [ | ||
'id' => $content->contentInfo->id, | ||
'name' => $content->getName(), | ||
'locationId' => $content->contentInfo->mainLocationId, | ||
], | ||
'value' => $this->imageAssetMapper->getAssetValue($content), | ||
]); | ||
} catch (Exception $e) { | ||
return $this->createGenericErrorResponse($e->getMessage()); | ||
} | ||
} else { | ||
return $this->createInvalidInputResponse($errors); | ||
} | ||
} | ||
|
||
return $this->createInvalidCsrfResponse(); | ||
} | ||
|
||
/** | ||
* @return \Symfony\Component\HttpFoundation\JsonResponse | ||
*/ | ||
private function createInvalidCsrfResponse(): JsonResponse | ||
{ | ||
$errorMessage = $this->translator->trans( | ||
/** @Desc("Missing or invalid CSRF token") */ 'asset.upload.invalid_csrf', [], 'assets' | ||
); | ||
|
||
return $this->createGenericErrorResponse($errorMessage); | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Validator\ConstraintViolationListInterface $errors | ||
* | ||
* @return \Symfony\Component\HttpFoundation\JsonResponse | ||
*/ | ||
private function createInvalidInputResponse(ConstraintViolationListInterface $errors): JsonResponse | ||
{ | ||
$errorMessages = []; | ||
foreach ($errors as $error) { | ||
$errorMessages[] = $error->getMessage(); | ||
} | ||
|
||
return $this->createGenericErrorResponse(implode(', ', $errorMessages)); | ||
} | ||
|
||
/** | ||
* @param string $errorMessage | ||
* | ||
* @return \Symfony\Component\HttpFoundation\JsonResponse | ||
*/ | ||
private function createGenericErrorResponse(string $errorMessage): JsonResponse | ||
{ | ||
return new JsonResponse([ | ||
'status' => 'failed', | ||
'error' => $errorMessage, | ||
]); | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\HttpFoundation\Request $request | ||
* | ||
* @return bool | ||
*/ | ||
private function isValidCsrfToken(Request $request): bool | ||
{ | ||
$csrfTokenValue = $request->headers->get(self::CSRF_TOKEN_HEADER); | ||
|
||
return $this->csrfTokenManager->isTokenValid( | ||
new CsrfToken('authenticate', $csrfTokenValue) | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
active_domains: | ||
- 'universal_discovery_widget' | ||
- 'alloy_editor' | ||
- 'fieldtypes_edit' |
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
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 |
---|---|---|
|
@@ -14,6 +14,11 @@ system: | |
controller: 'EzPlatformAdminUiBundle:ContentView:locationView' | ||
template: '@ezdesign\fieldtypes\preview\ezobjectrelationlist_row.html.twig' | ||
match: true | ||
preview_ezimageasset: | ||
default: | ||
controller: 'EzPlatformAdminUiBundle:ContentView:locationView' | ||
template: '@ezdesign\fieldtypes\preview\ezimageasset.html.twig' | ||
match: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing empty line |
||
|
||
content_edit_view: | ||
full: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you should not be able to create content while picking from UDW?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I going to check this with PM team 😉