Skip to content

Commit

Permalink
EZP-27213/EZP-27214: Fixed content type detection (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunpietro authored and lserwatka committed Apr 11, 2017
1 parent 96f00b3 commit 8e94d63
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 61 deletions.
68 changes: 16 additions & 52 deletions bundle/Resources/public/js/plugins/mfu-fileupload-plugin.js
Expand Up @@ -229,14 +229,6 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
initializer: function () {
const host = this.get('host');

/**
* Detected content type mapping
*
* @property _detectedContentTypeMapping
* @type {Object}
*/
this._detectedContentTypeMapping = {};

this._setContentTypeMappings();

host.on('mfuFileItemView:mfuUploadFile', this._initFileUpload, this);
Expand Down Expand Up @@ -510,9 +502,7 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
* @return {Promise}
*/
_detectContentType: function (file) {
this._detectedContentTypeMapping = this._detectContentTypeMapping(file);

return this._loadContentTypeByIdentifier(this._detectedContentTypeMapping.contentTypeIdentifier);
return this._loadContentTypeByIdentifier(this._detectContentTypeMapping(file).contentTypeIdentifier);
},

/**
Expand Down Expand Up @@ -593,50 +583,36 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
* @return {Promise}
*/
_transformFileToBase64: function (data) {
const fileReader = this.get('fileReader');
const fileReader = new FileReader();

return new Promise((resolve, reject) => {
fileReader.addEventListener('load', resolve.bind(this, data), false);
fileReader.addEventListener('load', resolve.bind(this, {
fileReader,
data
}), false);
fileReader.addEventListener('error', reject.bind(this), false);
fileReader.readAsDataURL(data.file);
});
},

/**
* Discovers data field identifier where the file meta data will be stored
*
* @method _discoverDataFieldIdentifier
* @protected
* @return {String} data field identifier
*/
_discoverDataFieldIdentifier: function () {
return this._detectedContentTypeMapping.contentFieldIdentifier;
},

/**
* Discovers name field identifier
*
* @method _discoverNameFieldIdentifier
* @protected
* @return {String} name field identifier
*/
_discoverNameFieldIdentifier: function () {
return this._detectContentTypeMapping.nameFieldIdentifier || 'name';
},

/**
* Updates a content struct with file meta data
*
* @method _updateContentStructWithFileMeta
* @protected
* @param data {Object} data hash
* @param input {Object} file input
* @param input.data {Object} data hash
* @param input.fileReader {FileReader} FileReader object
* @return {Object} updated data hash
*/
_updateContentStructWithFileMeta: function (data) {
data.struct.addField(this._discoverNameFieldIdentifier(), data.file.name);
data.struct.addField(this._discoverDataFieldIdentifier(), {
_updateContentStructWithFileMeta: function (input) {
const data = input.data;
const detectedMapping = this._detectContentTypeMapping(input.data.file);

data.struct.addField((detectedMapping.nameFieldIdentifier || 'name'), data.file.name);
data.struct.addField(detectedMapping.contentFieldIdentifier, {
fileName: data.file.name,
data: this.get('fileReader').result.replace(/^.*;base64,/, ''),
data: input.fileReader.result.replace(/^.*;base64,/, ''),
});

return data;
Expand Down Expand Up @@ -799,18 +775,6 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
value: [],
readOnly: true
},

/**
* FileReader object instance
*
* @attribute fileReader
* @type {FileReader}
* @readOnly
*/
fileReader: {
valueFn: () => new FileReader(),
readOnly: true
},
}
});

Expand Down
53 changes: 46 additions & 7 deletions bundle/Resources/public/js/views/mfu-fileitem-view.js
Expand Up @@ -224,15 +224,31 @@ YUI.add('mfu-fileitem-view', function (Y) {
* @param event {Object} event facade
*/
_showFileUploadError: function (event) {
const errorText = event.message ?
this.get('fileUploadErrorText').replace('{message}', event.message) :
this.get('fileUploadFailedText')
.replace('{statusCode}', event.target.status)
.replace('{statusText}', event.target.statusText);
let errorText = this.get('fileUploadUnexpectedErrorText');

if (event.message) {
errorText = this.get('fileUploadErrorText')
.replace('{message}', `${this._getFileName()} - ${event.message}`);
} else if (event.target && event.target.hasOwnProperty('status') && event.target.hasOwnProperty('statusText')) {
let status = event.target.status ?
event.target.status :
'N/A';
let message = event.target.statusText ?
event.target.statusText :
this.get('fileUploadUknownContentTypeText');

errorText = this.get('fileUploadFailedText')
.replace('{statusCode}', status)
.replace('{statusText}', `${this._getFileName()} - ${message}`);
} else if (event.document) {
errorText = this.get('fileUploadFailedText')
.replace('{statusCode}', event.document.ErrorMessage.errorCode)
.replace('{statusText}', `${this._getFileName()} - ${event.document.ErrorMessage.errorDescription}`);
}

this._fireNotifyEvent({
text: errorText,
identifier: 'mfu-upload-failed',
identifier: 'mfu-upload-failed-' + this._yuid,
state: 'error',
timeout: 0,
});
Expand Down Expand Up @@ -632,6 +648,30 @@ YUI.add('mfu-fileitem-view', function (Y) {
readOnly: true,
},

/**
* File upload unknown content type text
*
* @attribute fileUploadUknownContentTypeText
* @type {String}
* @readOnly
*/
fileUploadUknownContentTypeText: {
valueFn: () => Y.eZ.trans('file.upload.content.type.uknown', {}, 'fileuploaditem'),
readOnly: true,
},

/**
* File upload unexpected error text
*
* @attribute fileUploadUnexpectedErrorText
* @type {String}
* @readOnly
*/
fileUploadUnexpectedErrorText: {
valueFn: () => Y.eZ.trans('file.upload.unexpected.error', {}, 'fileuploaditem'),
readOnly: true,
},

/**
* File type not allowed error text
*
Expand All @@ -655,7 +695,6 @@ YUI.add('mfu-fileitem-view', function (Y) {
valueFn: () => Y.eZ.trans('file.size.not.allowed', {}, 'fileuploaditem'),
readOnly: true,
},

}
});
});
1 change: 0 additions & 1 deletion bundle/Resources/public/js/views/mfu-uploadform-view.js
Expand Up @@ -26,7 +26,6 @@ YUI.add('mfu-uploadform-view', function (Y) {

EVENTS[SELECTOR_BTN] = {tap: '_uiSelectFiles'};
EVENTS[SELECTOR_INPUT] = {
input: '_uploadFiles',
change: '_uploadFiles'
};
EVENTS[SELECTOR_FORM_ACTIVE] = {
Expand Down
14 changes: 13 additions & 1 deletion bundle/Resources/translations/fileuploaditem.en.xlf
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
<file date="2017-03-29T12:06:37Z" source-language="en" target-language="en" datatype="plaintext" original="not.available">
<file date="2017-04-07T13:32:59Z" source-language="en" target-language="en" datatype="plaintext" original="not.available">
<header>
<tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
Expand Down Expand Up @@ -42,6 +42,12 @@
<note>key: file.upload.aborted</note>
<jms:reference-file>./Resources/public/js/views/mfu-fileitem-view.js</jms:reference-file>
</trans-unit>
<trans-unit id="341815cb4eb951014d0172c0d6b84c18710a5785" resname="file.upload.content.type.uknown">
<source>The file has unknown MIME type</source>
<target>The file has unknown MIME type</target>
<note>key: file.upload.content.type.uknown</note>
<jms:reference-file>./Resources/public/js/views/mfu-fileitem-view.js</jms:reference-file>
</trans-unit>
<trans-unit id="6760bdb1386b6a55665ee566d1dc52d0333b081a" resname="file.upload.done">
<source>{total} uploaded</source>
<target>{total} uploaded</target>
Expand All @@ -66,6 +72,12 @@
<note>key: file.upload.status</note>
<jms:reference-file>./Resources/public/js/views/mfu-fileitem-view.js</jms:reference-file>
</trans-unit>
<trans-unit id="8948c06e80e91b86d92708c2a3b05bd3118d5561" resname="file.upload.unexpected.error">
<source>Unexpected error</source>
<target state="new">Unexpected error</target>
<note>key: file.upload.unexpected.error</note>
<jms:reference-file>./Resources/public/js/views/mfu-fileitem-view.js</jms:reference-file>
</trans-unit>
<trans-unit id="3b5c9a1c511d6150f8e567ea63ec8658609f7c94" resname="publishing.file">
<source>Publishing file: {filename}</source>
<target>Publishing file: {filename}</target>
Expand Down

0 comments on commit 8e94d63

Please sign in to comment.