-
Notifications
You must be signed in to change notification settings - Fork 61
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 media field to custom entity #172
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Pim\Bundle\CustomEntityBundle\Controller; | ||
|
||
use Akeneo\Component\FileStorage\PathGeneratorInterface; | ||
use Symfony\Component\HttpFoundation\File\Exception\FileException; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* Media REST controller | ||
* | ||
* @author Simon CARRE <[email protected]> | ||
* @package Pim\Bundle\CustomEntityBundle\Controller | ||
*/ | ||
class MediaRestController | ||
{ | ||
/** | ||
* Validator interface | ||
* | ||
* @var ValidatorInterface | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* Path generator | ||
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. Not needed comment line |
||
* | ||
* @var PathGeneratorInterface | ||
*/ | ||
protected $pathGenerator; | ||
|
||
/** | ||
* Upload directory | ||
* | ||
* @var string | ||
*/ | ||
protected $uploadDir; | ||
|
||
/** | ||
* @param ValidatorInterface $validator | ||
* @param PathGeneratorInterface $pathGenerator | ||
* @param string $uploadDir | ||
*/ | ||
public function __construct(ValidatorInterface $validator, PathGeneratorInterface $pathGenerator, $uploadDir) | ||
{ | ||
$this->validator = $validator; | ||
$this->pathGenerator = $pathGenerator; | ||
$this->uploadDir = $uploadDir; | ||
} | ||
|
||
/** | ||
* Post a new media and return original filename and path | ||
* | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function postAction(Request $request) | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ | ||
$file = $request->files->get('file'); | ||
$violations = $this->validator->validate($file); | ||
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. Could you add an example of file validation in the AcmeCustomBundle ? |
||
|
||
if (count($violations) > 0) { | ||
$errors = []; | ||
foreach ($violations as $violation) { | ||
$errors[$violation->getPropertyPath()] = [ | ||
'message' => $violation->getMessage(), | ||
'invalid_value' => $violation->getInvalidValue(), | ||
]; | ||
} | ||
|
||
return new JsonResponse($errors, 400); | ||
} | ||
$pathData = $this->pathGenerator->generate($file); | ||
|
||
try { | ||
$movedFile = $file->move( | ||
$this->uploadDir . DIRECTORY_SEPARATOR . $pathData['path'] . DIRECTORY_SEPARATOR . $pathData['uuid'], | ||
$file->getClientOriginalName() | ||
); | ||
} catch (FileException $e) { | ||
return new JsonResponse("Unable to create target-directory, or moving file.", 400); | ||
} | ||
$filePath = $movedFile->getPathname(); | ||
$filePathWithoutDirectory = str_replace($this->uploadDir . '/', '', $filePath); | ||
|
||
return new JsonResponse( | ||
[ | ||
'originalFilename' => $file->getClientOriginalName(), | ||
'filePath' => $filePath, | ||
'shortFilePath' => $filePathWithoutDirectory | ||
] | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,3 +66,8 @@ pim_customentity_history: | |
actionType: history | ||
requirements: | ||
id: \d+ | ||
|
||
pim_customentity_media_rest_post: | ||
path: '/rest/customentity_media' | ||
defaults: { _controller: pim_custom_entity.media_rest_controller.class:postAction } | ||
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. @simoncarre here should be the name of the service |
||
methods: [POST] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
define([ | ||
'jquery', | ||
'underscore', | ||
'pim/form/common/fields/field', | ||
'pim/media-url-generator', | ||
'routing', | ||
'custom_entity/template/form/common/fields/media' | ||
], | ||
function ($, | ||
_, | ||
BaseField, | ||
MediaUrlGenerator, | ||
Routing, | ||
template) { | ||
return BaseField.extend({ | ||
template: _.template(template), | ||
|
||
events: { | ||
'change input[type="file"]': function (event) { | ||
this.errors = []; | ||
this.updateModel(this.getFieldValue(event.target)); | ||
this.render(); | ||
}, | ||
'click .clear-field': 'clearField' | ||
}, | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
renderInput: function (templateContext) { | ||
var modelValue = this.getModelValue(); | ||
var originalFilename = null; | ||
var filePath = null; | ||
var shortFilePath = null; | ||
if (modelValue != null) { | ||
var modelValueAsJson = JSON.parse(modelValue); | ||
originalFilename = modelValueAsJson.originalFilename; | ||
filePath = modelValueAsJson.filePath; | ||
shortFilePath = modelValueAsJson.shortFilePath; | ||
} | ||
|
||
return this.template(_.extend(templateContext, { | ||
value: modelValue, | ||
originalFilename: originalFilename, | ||
filePath: filePath, | ||
shortFilePath: shortFilePath, | ||
mediaUrlGenerator: MediaUrlGenerator | ||
})); | ||
}, | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
getFieldValue: function (field) { | ||
var self = this; | ||
var fieldValue = null; | ||
|
||
var input = this.$(field).get(0); | ||
if (!input || 0 === input.files.length) { | ||
return null; | ||
} | ||
var formData = new FormData(); | ||
formData.append('file', input.files[0]); | ||
|
||
$.ajax({ | ||
url: Routing.generate('pim_customentity_media_rest_post'), | ||
type: 'POST', | ||
data: formData, | ||
contentType: false, | ||
cache: false, | ||
processData: false, | ||
async: false | ||
}).done(function (data) { | ||
fieldValue = JSON.stringify(data); | ||
}).fail(function () { | ||
self.errors.push({ | ||
code: self.fieldName, | ||
message: _.__('pim_enrich.entity.product.error.upload'), | ||
global: false | ||
}); | ||
}); | ||
|
||
return fieldValue; | ||
}, | ||
|
||
/** | ||
* Clear media field | ||
*/ | ||
clearField: function () { | ||
this.updateModel(null); | ||
this.render(); | ||
} | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="AknMediaField <%- filePath ? 'has-file' : '' %>" > | ||
<% if (!filePath) { %> | ||
<input class="AknMediaField-fileUploaderInput" id="upload_field_<%- fieldId %>" type="file"/> | ||
<div class="AknMediaField-emptyContainer"> | ||
<span title="upload icon" class="AknMediaField-uploadIcon"/> | ||
<span><%- _.__('pim_enrich.entity.product.media.upload')%></span> | ||
</div> | ||
<% } else { %> | ||
<div class="AknMediaField-preview preview"> | ||
<% mediaThumbnailUrl = mediaUrlGenerator.getMediaShowUrl(filePath, 'thumbnail_small') %> | ||
<% mediaDownloadUrl = mediaUrlGenerator.getMediaDownloadUrl(shortFilePath) %> | ||
<div class="AknMediaField-thumb file"><img src="<%- mediaThumbnailUrl %>" class="AknMediaField-image"/></div> | ||
<div class="AknMediaField-info info"> | ||
<div class="filename" title="<%- originalFilename %>"><%- originalFilename %></div> | ||
<div class="AknButtonList AknButtonList--centered actions"> | ||
<a href="<%- mediaDownloadUrl %>" class="AknButtonList-item AknIconButton AknIconButton--grey download-file" download><i class="icon icon-cloud-download"></i></a> | ||
<span class="AknButtonList-item AknIconButton AknIconButton--grey clear-field"><i class="icon icon-trash"></i></span> | ||
</div> | ||
</div> | ||
</div> | ||
<% } %> | ||
</div> |
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.
Not needed comment line