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';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
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
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
const DEFAULTS = {
|
|
|
|
accept: ['text/csv'],
|
|
|
|
extensions: ['csv']
|
|
|
|
};
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
export default Component.extend({
|
2018-01-11 20:43:23 +03:00
|
|
|
ajax: service(),
|
|
|
|
eventBus: service(),
|
|
|
|
notifications: service(),
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
tagName: 'section',
|
|
|
|
classNames: ['gh-image-uploader'],
|
|
|
|
classNameBindings: ['dragClass'],
|
|
|
|
|
|
|
|
labelText: 'Select or drag-and-drop a file',
|
|
|
|
url: null,
|
|
|
|
paramName: 'file',
|
2017-11-24 21:53:19 +03:00
|
|
|
accept: null,
|
|
|
|
extensions: null,
|
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,
|
|
|
|
|
2018-03-20 17:57:59 +03:00
|
|
|
// Allowed actions
|
|
|
|
fileSelected: () => {},
|
|
|
|
uploadStarted: () => {},
|
|
|
|
uploadFinished: () => {},
|
|
|
|
uploadSuccess: () => {},
|
|
|
|
uploadFailed: () => {},
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
formData: computed('file', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let paramName = this.paramName;
|
|
|
|
let file = this.file;
|
2016-04-15 17:45:50 +03:00
|
|
|
let formData = new FormData();
|
|
|
|
|
|
|
|
formData.append(paramName, file);
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}),
|
|
|
|
|
|
|
|
progressStyle: computed('uploadPercentage', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let percentage = this.uploadPercentage;
|
2016-04-15 17:45:50 +03:00
|
|
|
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);
|
2019-03-06 16:53:54 +03:00
|
|
|
let listenTo = this.listenTo;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
this.accept = this.accept || DEFAULTS.accept;
|
|
|
|
this.extensions = this.extensions || DEFAULTS.extensions;
|
|
|
|
|
2019-07-22 15:16:00 +03:00
|
|
|
this._uploadEventHandler = function (file) {
|
|
|
|
if (file) {
|
|
|
|
this.set('file', file);
|
|
|
|
}
|
|
|
|
this.send('upload');
|
|
|
|
};
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
if (listenTo) {
|
2019-07-22 15:16:00 +03:00
|
|
|
this.eventBus.subscribe(`${listenTo}:upload`, this, this._uploadEventHandler);
|
2016-08-17 18:01:46 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-01 16:08:37 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
2019-03-06 16:53:54 +03:00
|
|
|
let accept = this.accept;
|
|
|
|
let extensions = this.extensions;
|
2016-09-01 16:08:37 +03:00
|
|
|
|
|
|
|
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() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let listenTo = this.listenTo;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (listenTo) {
|
2019-07-22 15:16:00 +03:00
|
|
|
this.eventBus.unsubscribe(`${listenTo}:upload`, this, this._uploadEventHandler);
|
2016-08-17 18:01:46 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
fileSelected(fileList, resetInput) {
|
|
|
|
let [file] = Array.from(fileList);
|
|
|
|
let validationResult = this._validate(file);
|
|
|
|
|
|
|
|
this.set('file', file);
|
2018-03-20 17:57:59 +03:00
|
|
|
this.fileSelected(file);
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
if (validationResult === true) {
|
|
|
|
run.schedule('actions', this, function () {
|
|
|
|
this.generateRequest();
|
|
|
|
|
|
|
|
if (resetInput) {
|
|
|
|
resetInput();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._uploadFailed(validationResult);
|
|
|
|
|
|
|
|
if (resetInput) {
|
|
|
|
resetInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
upload() {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.file) {
|
2018-01-11 20:43:23 +03:00
|
|
|
this.generateRequest();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('file', null);
|
|
|
|
this.set('uploadPercentage', 0);
|
|
|
|
this.set('failureMessage', null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
2017-12-12 14:53:35 +03:00
|
|
|
if (navigator.userAgent.indexOf('Chrome') > -1) {
|
|
|
|
let eA = event.dataTransfer.effectAllowed;
|
|
|
|
event.dataTransfer.dropEffect = (eA === 'move' || eA === 'linkMove') ? 'move' : 'copy';
|
|
|
|
}
|
2016-05-18 11:04:30 +03:00
|
|
|
|
|
|
|
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() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let ajax = this.ajax;
|
|
|
|
let formData = this.formData;
|
|
|
|
let url = this.url;
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2018-03-20 17:57:59 +03:00
|
|
|
this.uploadStarted();
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
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(() => {
|
2018-03-20 17:57:59 +03:00
|
|
|
this.uploadFinished();
|
2016-04-15 17:45:50 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_uploadProgress(event) {
|
|
|
|
if (event.lengthComputable) {
|
|
|
|
run(() => {
|
|
|
|
let percentage = Math.round((event.loaded / event.total) * 100);
|
|
|
|
this.set('uploadPercentage', percentage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_uploadSuccess(response) {
|
2018-03-20 17:57:59 +03:00
|
|
|
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)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
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.';
|
2017-11-04 01:59:39 +03:00
|
|
|
} else if (error.payload && error.payload.errors && !isBlank(error.payload.errors[0].message)) {
|
|
|
|
message = htmlSafe(error.payload.errors[0].message);
|
2016-04-15 17:45:50 +03:00
|
|
|
} else {
|
|
|
|
message = 'Something went wrong :(';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('failureMessage', message);
|
2018-03-20 17:57:59 +03:00
|
|
|
this.uploadFailed(error);
|
2016-04-15 17:45:50 +03:00
|
|
|
},
|
|
|
|
|
2016-08-22 14:45:33 +03:00
|
|
|
_validate(file) {
|
2018-03-20 17:57:59 +03:00
|
|
|
if (this.validate) {
|
|
|
|
return this.validate(file);
|
2016-08-22 14:45:33 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
});
|