2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
2016-04-15 17:45:50 +03:00
|
|
|
import {
|
2017-05-29 21:50:03 +03:00
|
|
|
UnsupportedMediaTypeError,
|
2016-05-22 11:20:02 +03:00
|
|
|
isRequestEntityTooLargeError,
|
2016-08-22 14:45:33 +03:00
|
|
|
isUnsupportedMediaTypeError,
|
2017-05-29 21:50:03 +03:00
|
|
|
isVersionMismatchError
|
2016-05-24 15:06:59 +03:00
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {invokeAction} from 'ember-invoke-action';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: 'section',
|
|
|
|
classNames: ['gh-image-uploader'],
|
|
|
|
classNameBindings: ['dragClass'],
|
|
|
|
|
|
|
|
labelText: 'Select or drag-and-drop a file',
|
|
|
|
url: null,
|
|
|
|
paramName: 'file',
|
2016-09-01 16:08:37 +03:00
|
|
|
accept: ['text/csv'],
|
|
|
|
extensions: ['csv'],
|
2016-08-22 14:45:33 +03:00
|
|
|
validate: null,
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
file: null,
|
|
|
|
response: null,
|
|
|
|
|
|
|
|
dragClass: null,
|
|
|
|
failureMessage: null,
|
|
|
|
uploadPercentage: 0,
|
|
|
|
|
2017-10-30 12:38:01 +03:00
|
|
|
ajax: service(),
|
|
|
|
eventBus: service(),
|
|
|
|
notifications: service(),
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
formData: computed('file', function () {
|
|
|
|
let paramName = this.get('paramName');
|
|
|
|
let file = this.get('file');
|
|
|
|
let formData = new FormData();
|
|
|
|
|
|
|
|
formData.append(paramName, file);
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}),
|
|
|
|
|
|
|
|
progressStyle: computed('uploadPercentage', function () {
|
|
|
|
let percentage = this.get('uploadPercentage');
|
|
|
|
let width = '';
|
|
|
|
|
|
|
|
if (percentage > 0) {
|
|
|
|
width = `${percentage}%`;
|
|
|
|
} else {
|
|
|
|
width = '0';
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
return htmlSafe(`width: ${width}`);
|
2016-04-15 17:45:50 +03:00
|
|
|
}),
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
// we can optionally listen to a named event bus channel so that the upload
|
|
|
|
// process can be triggered externally
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let listenTo = this.get('listenTo');
|
|
|
|
|
|
|
|
if (listenTo) {
|
|
|
|
this.get('eventBus').subscribe(`${listenTo}:upload`, this, function (file) {
|
|
|
|
if (file) {
|
|
|
|
this.set('file', file);
|
|
|
|
}
|
|
|
|
this.send('upload');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-01 16:08:37 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let accept = this.get('accept');
|
|
|
|
let extensions = this.get('extensions');
|
|
|
|
|
|
|
|
this._accept = (!isBlank(accept) && !isEmberArray(accept)) ? accept.split(',') : accept;
|
|
|
|
this._extensions = (!isBlank(extensions) && !isEmberArray(extensions)) ? extensions.split(',') : extensions;
|
|
|
|
},
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
willDestroyElement() {
|
|
|
|
let listenTo = this.get('listenTo');
|
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (listenTo) {
|
|
|
|
this.get('eventBus').unsubscribe(`${listenTo}:upload`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
dragOver(event) {
|
2016-05-18 11:04:30 +03:00
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is needed to work around inconsistencies with dropping files
|
|
|
|
// from Chrome's downloads bar
|
|
|
|
let eA = event.dataTransfer.effectAllowed;
|
|
|
|
event.dataTransfer.dropEffect = (eA === 'move' || eA === 'linkMove') ? 'move' : 'copy';
|
|
|
|
|
|
|
|
event.stopPropagation();
|
2016-04-15 17:45:50 +03:00
|
|
|
event.preventDefault();
|
2016-05-18 11:04:30 +03:00
|
|
|
|
2016-09-24 12:10:08 +03:00
|
|
|
this.set('dragClass', '-drag-over');
|
2016-04-15 17:45:50 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('dragClass', null);
|
|
|
|
},
|
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('dragClass', null);
|
|
|
|
if (event.dataTransfer.files) {
|
2017-03-08 19:33:39 +03:00
|
|
|
this.send('fileSelected', event.dataTransfer.files);
|
2016-04-15 17:45:50 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
generateRequest() {
|
|
|
|
let ajax = this.get('ajax');
|
|
|
|
let formData = this.get('formData');
|
|
|
|
let url = this.get('url');
|
|
|
|
|
|
|
|
invokeAction(this, 'uploadStarted');
|
|
|
|
|
|
|
|
ajax.post(url, {
|
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text',
|
|
|
|
xhr: () => {
|
|
|
|
let xhr = new window.XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.upload.addEventListener('progress', (event) => {
|
|
|
|
this._uploadProgress(event);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
return xhr;
|
|
|
|
}
|
|
|
|
}).then((response) => {
|
|
|
|
this._uploadSuccess(JSON.parse(response));
|
|
|
|
}).catch((error) => {
|
|
|
|
this._uploadFailed(error);
|
|
|
|
}).finally(() => {
|
|
|
|
invokeAction(this, 'uploadFinished');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_uploadProgress(event) {
|
|
|
|
if (event.lengthComputable) {
|
|
|
|
run(() => {
|
|
|
|
let percentage = Math.round((event.loaded / event.total) * 100);
|
|
|
|
this.set('uploadPercentage', percentage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_uploadSuccess(response) {
|
|
|
|
invokeAction(this, 'uploadSuccess', response);
|
2017-03-08 19:33:39 +03:00
|
|
|
this.send('reset');
|
2016-04-15 17:45:50 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_uploadFailed(error) {
|
|
|
|
let message;
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
}
|
|
|
|
|
2016-05-22 11:20:02 +03:00
|
|
|
if (isUnsupportedMediaTypeError(error)) {
|
2016-04-15 17:45:50 +03:00
|
|
|
message = 'The file type you uploaded is not supported.';
|
2016-05-22 11:20:02 +03:00
|
|
|
} else if (isRequestEntityTooLargeError(error)) {
|
2016-04-15 17:45:50 +03:00
|
|
|
message = 'The file you uploaded was larger than the maximum file size your server allows.';
|
|
|
|
} else if (error.errors && !isBlank(error.errors[0].message)) {
|
2016-08-17 18:01:46 +03:00
|
|
|
message = htmlSafe(error.errors[0].message);
|
2016-04-15 17:45:50 +03:00
|
|
|
} else {
|
|
|
|
message = 'Something went wrong :(';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('failureMessage', message);
|
|
|
|
invokeAction(this, 'uploadFailed', error);
|
|
|
|
},
|
|
|
|
|
2016-08-22 14:45:33 +03:00
|
|
|
_validate(file) {
|
|
|
|
if (this.get('validate')) {
|
|
|
|
return invokeAction(this, 'validate', file);
|
|
|
|
} else {
|
|
|
|
return this._defaultValidator(file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_defaultValidator(file) {
|
2016-09-14 11:54:16 +03:00
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
|
|
|
let extensions = this._extensions;
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-09-14 11:54:16 +03:00
|
|
|
if (!extension || extensions.indexOf(extension.toLowerCase()) === -1) {
|
2016-08-22 14:45:33 +03:00
|
|
|
return new UnsupportedMediaTypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
actions: {
|
|
|
|
fileSelected(fileList) {
|
2016-08-22 14:45:33 +03:00
|
|
|
// can't use array destructuring here as FileList is not a strict
|
|
|
|
// array and fails in Safari
|
2016-11-14 16:16:51 +03:00
|
|
|
// eslint-disable-next-line ember-suave/prefer-destructuring
|
2016-08-22 14:45:33 +03:00
|
|
|
let file = fileList[0];
|
|
|
|
let validationResult = this._validate(file);
|
|
|
|
|
|
|
|
this.set('file', file);
|
2016-08-19 14:42:47 +03:00
|
|
|
invokeAction(this, 'fileSelected', file);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
|
|
|
if (validationResult === true) {
|
|
|
|
run.schedule('actions', this, function () {
|
|
|
|
this.generateRequest();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._uploadFailed(validationResult);
|
|
|
|
}
|
2016-04-15 17:45:50 +03:00
|
|
|
},
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
upload() {
|
|
|
|
if (this.get('file')) {
|
|
|
|
this.generateRequest();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
reset() {
|
|
|
|
this.set('file', null);
|
|
|
|
this.set('uploadPercentage', 0);
|
|
|
|
this.set('failureMessage', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|