2016-06-30 21:14:25 +03:00
|
|
|
import Component from 'ember-component';
|
|
|
|
import computed from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import {htmlSafe} from 'ember-string';
|
|
|
|
import {isBlank} from 'ember-utils';
|
|
|
|
import run from 'ember-runloop';
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
import {invokeAction} from 'ember-invoke-action';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2016-05-22 11:20:02 +03:00
|
|
|
import {
|
|
|
|
isRequestEntityTooLargeError,
|
2016-06-30 17:45:02 +03:00
|
|
|
isUnsupportedMediaTypeError,
|
|
|
|
isVersionMismatchError
|
2016-05-22 11:20:02 +03:00
|
|
|
} from 'ghost-admin/services/ajax';
|
2016-02-26 16:25:47 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: 'section',
|
|
|
|
classNames: ['gh-image-uploader'],
|
|
|
|
classNameBindings: ['dragClass'],
|
|
|
|
|
|
|
|
image: null,
|
2016-07-14 15:27:18 +03:00
|
|
|
text: '',
|
2016-02-26 16:25:47 +03:00
|
|
|
saveButton: true,
|
|
|
|
|
|
|
|
dragClass: null,
|
|
|
|
failureMessage: null,
|
|
|
|
file: null,
|
|
|
|
formType: 'upload',
|
|
|
|
url: null,
|
|
|
|
uploadPercentage: 0,
|
|
|
|
|
2016-06-30 21:14:25 +03:00
|
|
|
ajax: injectService(),
|
|
|
|
config: injectService(),
|
2016-06-30 17:45:02 +03:00
|
|
|
notifications: injectService(),
|
2016-02-26 16:25:47 +03:00
|
|
|
|
|
|
|
// TODO: this wouldn't be necessary if the server could accept direct
|
|
|
|
// file uploads
|
|
|
|
formData: computed('file', function () {
|
|
|
|
let file = this.get('file');
|
|
|
|
let formData = new FormData();
|
|
|
|
|
|
|
|
formData.append('uploadimage', 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-02-26 16:25:47 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
canShowUploadForm: computed('config.fileStorage', function () {
|
|
|
|
return this.get('config.fileStorage') !== false;
|
|
|
|
}),
|
|
|
|
|
|
|
|
showUploadForm: computed('formType', function () {
|
|
|
|
let canShowUploadForm = this.get('canShowUploadForm');
|
|
|
|
let formType = this.get('formType');
|
|
|
|
|
|
|
|
return formType === 'upload' && canShowUploadForm;
|
|
|
|
}),
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
let image = this.get('image');
|
|
|
|
this.set('url', image);
|
|
|
|
},
|
|
|
|
|
|
|
|
dragOver(event) {
|
|
|
|
let showUploadForm = this.get('showUploadForm');
|
|
|
|
|
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-02-26 16:25:47 +03:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (showUploadForm) {
|
|
|
|
this.set('dragClass', '--drag-over');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
let showUploadForm = this.get('showUploadForm');
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (showUploadForm) {
|
|
|
|
this.set('dragClass', null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
let showUploadForm = this.get('showUploadForm');
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.set('dragClass', null);
|
|
|
|
|
|
|
|
if (showUploadForm) {
|
|
|
|
if (event.dataTransfer.files) {
|
|
|
|
this.send('fileSelected', event.dataTransfer.files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadStarted() {
|
|
|
|
invokeAction(this, 'uploadStarted');
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadProgress(event) {
|
2016-02-26 16:25:47 +03:00
|
|
|
if (event.lengthComputable) {
|
|
|
|
run(() => {
|
|
|
|
let percentage = Math.round((event.loaded / event.total) * 100);
|
|
|
|
this.set('uploadPercentage', percentage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadFinished() {
|
|
|
|
invokeAction(this, 'uploadFinished');
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadSuccess(response) {
|
2016-02-26 16:25:47 +03:00
|
|
|
this.set('url', response);
|
|
|
|
this.send('saveUrl');
|
|
|
|
this.send('reset');
|
2016-06-30 17:45:02 +03:00
|
|
|
invokeAction(this, 'uploadSuccess', response);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadFailed(error) {
|
2016-02-26 16:25:47 +03:00
|
|
|
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-02-26 16:25:47 +03:00
|
|
|
message = 'The image type you uploaded is not supported. Please use .PNG, .JPG, .GIF, .SVG.';
|
2016-05-22 11:20:02 +03:00
|
|
|
} else if (isRequestEntityTooLargeError(error)) {
|
2016-02-26 16:25:47 +03:00
|
|
|
message = 'The image you uploaded was larger than the maximum file size your server allows.';
|
|
|
|
} else if (error.errors && !isBlank(error.errors[0].message)) {
|
|
|
|
message = error.errors[0].message;
|
|
|
|
} else {
|
|
|
|
message = 'Something went wrong :(';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('failureMessage', message);
|
2016-06-30 17:45:02 +03:00
|
|
|
invokeAction(this, 'uploadFailed', error);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
generateRequest() {
|
|
|
|
let ajax = this.get('ajax');
|
|
|
|
let formData = this.get('formData');
|
|
|
|
let url = `${ghostPaths().apiRoot}/uploads/`;
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
this._uploadStarted();
|
2016-02-26 16:25:47 +03:00
|
|
|
|
|
|
|
ajax.post(url, {
|
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text',
|
|
|
|
xhr: () => {
|
|
|
|
let xhr = new window.XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.upload.addEventListener('progress', (event) => {
|
2016-06-30 17:45:02 +03:00
|
|
|
this._uploadProgress(event);
|
2016-02-26 16:25:47 +03:00
|
|
|
}, false);
|
|
|
|
|
|
|
|
return xhr;
|
|
|
|
}
|
|
|
|
}).then((response) => {
|
|
|
|
let url = JSON.parse(response);
|
2016-06-30 17:45:02 +03:00
|
|
|
this._uploadSuccess(url);
|
2016-02-26 16:25:47 +03:00
|
|
|
}).catch((error) => {
|
2016-06-30 17:45:02 +03:00
|
|
|
this._uploadFailed(error);
|
2016-02-26 16:25:47 +03:00
|
|
|
}).finally(() => {
|
2016-06-30 17:45:02 +03:00
|
|
|
this._uploadFinished();
|
2016-02-26 16:25:47 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
fileSelected(fileList) {
|
|
|
|
this.set('file', fileList[0]);
|
|
|
|
run.schedule('actions', this, function () {
|
|
|
|
this.generateRequest();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onInput(url) {
|
|
|
|
this.set('url', url);
|
2016-06-30 17:45:02 +03:00
|
|
|
invokeAction(this, 'onInput', url);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('file', null);
|
|
|
|
this.set('uploadPercentage', 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
switchForm(formType) {
|
|
|
|
this.set('formType', formType);
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
run.scheduleOnce('afterRender', this, function () {
|
|
|
|
invokeAction(this, 'formChanged', formType);
|
|
|
|
});
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
saveUrl() {
|
|
|
|
let url = this.get('url');
|
2016-06-30 17:45:02 +03:00
|
|
|
invokeAction(this, 'update', url);
|
2016-02-26 16:25:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|