2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2016-05-22 11:20:02 +03:00
|
|
|
import {
|
2017-05-29 21:50:03 +03:00
|
|
|
UnsupportedMediaTypeError,
|
2016-05-22 11:20:02 +03:00
|
|
|
isRequestEntityTooLargeError,
|
2016-06-30 17:45:02 +03:00
|
|
|
isUnsupportedMediaTypeError,
|
2017-05-29 21:50:03 +03:00
|
|
|
isVersionMismatchError
|
2016-05-22 11:20:02 +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-02-26 16:25:47 +03:00
|
|
|
|
2017-05-08 13:35:42 +03:00
|
|
|
export const IMAGE_MIME_TYPES = 'image/gif,image/jpg,image/jpeg,image/png,image/svg+xml';
|
|
|
|
export const IMAGE_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'svg'];
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
export default Component.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
ajax: service(),
|
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
|
|
|
settings: service(),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
tagName: 'section',
|
|
|
|
classNames: ['gh-image-uploader'],
|
|
|
|
classNameBindings: ['dragClass'],
|
|
|
|
|
|
|
|
image: null,
|
2016-07-14 15:27:18 +03:00
|
|
|
text: '',
|
2016-07-24 12:19:49 +03:00
|
|
|
altText: '',
|
2016-02-26 16:25:47 +03:00
|
|
|
saveButton: true,
|
2017-01-26 14:17:34 +03:00
|
|
|
accept: null,
|
|
|
|
extensions: null,
|
|
|
|
uploadUrl: null,
|
2016-08-22 14:45:33 +03:00
|
|
|
validate: null,
|
2017-08-02 10:05:59 +03:00
|
|
|
allowUnsplash: false,
|
2016-02-26 16:25:47 +03:00
|
|
|
|
|
|
|
dragClass: null,
|
|
|
|
failureMessage: null,
|
|
|
|
file: null,
|
|
|
|
url: null,
|
|
|
|
uploadPercentage: 0,
|
|
|
|
|
2017-05-08 13:35:42 +03:00
|
|
|
_defaultAccept: IMAGE_MIME_TYPES,
|
|
|
|
_defaultExtensions: IMAGE_EXTENSIONS,
|
2017-01-26 14:17:34 +03:00
|
|
|
_defaultUploadUrl: '/uploads/',
|
2017-08-02 10:05:59 +03:00
|
|
|
_showUnsplash: false,
|
2017-01-26 14:17:34 +03:00
|
|
|
|
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;
|
|
|
|
}),
|
|
|
|
|
2016-07-24 12:19:49 +03:00
|
|
|
description: computed('text', 'altText', function () {
|
|
|
|
let altText = this.get('altText');
|
|
|
|
|
|
|
|
return this.get('text') || (altText ? `Upload image of "${altText}"` : 'Upload an image');
|
|
|
|
}),
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
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
|
|
|
}),
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
let image = this.get('image');
|
|
|
|
this.set('url', image);
|
2017-01-26 14:17:34 +03:00
|
|
|
|
|
|
|
if (!this.get('accept')) {
|
|
|
|
this.set('accept', this.get('_defaultAccept'));
|
|
|
|
}
|
|
|
|
if (!this.get('extensions')) {
|
|
|
|
this.set('extensions', this.get('_defaultExtensions'));
|
|
|
|
}
|
|
|
|
if (!this.get('uploadUrl')) {
|
|
|
|
this.set('uploadUrl', this.get('_defaultUploadUrl'));
|
|
|
|
}
|
2016-02-26 16:25:47 +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-02-26 16:25:47 +03:00
|
|
|
event.preventDefault();
|
|
|
|
|
2017-03-03 11:59:01 +03:00
|
|
|
this.set('dragClass', '-drag-over');
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
event.preventDefault();
|
2017-03-03 11:59:01 +03:00
|
|
|
this.set('dragClass', null);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.set('dragClass', null);
|
|
|
|
|
2017-03-03 11:59:01 +03:00
|
|
|
if (event.dataTransfer.files) {
|
|
|
|
this.send('fileSelected', event.dataTransfer.files);
|
2016-02-26 16:25:47 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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)) {
|
2017-01-26 14:17:34 +03:00
|
|
|
let validExtensions = this.get('extensions').join(', .').toUpperCase();
|
|
|
|
validExtensions = `.${validExtensions}`;
|
|
|
|
|
|
|
|
message = `The image type you uploaded is not supported. Please use ${validExtensions}`;
|
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');
|
2017-01-26 14:17:34 +03:00
|
|
|
let uploadUrl = this.get('uploadUrl');
|
|
|
|
// CASE: we want to upload an icon and we have to POST it to a different endpoint, expecially for icons
|
|
|
|
let url = `${ghostPaths().apiRoot}${uploadUrl}`;
|
2016-02-26 16:25:47 +03:00
|
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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 extensions = this.get('extensions');
|
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-09-14 11:54:16 +03:00
|
|
|
if (!isEmberArray(extensions)) {
|
|
|
|
extensions = extensions.split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!extension || extensions.indexOf(extension.toLowerCase()) === -1) {
|
2016-08-22 14:45:33 +03:00
|
|
|
return new UnsupportedMediaTypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2016-02-26 16:25:47 +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-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
addUnsplashPhoto(photo) {
|
|
|
|
this.set('url', photo.urls.regular);
|
|
|
|
this.send('saveUrl');
|
|
|
|
},
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
reset() {
|
|
|
|
this.set('file', null);
|
|
|
|
this.set('uploadPercentage', 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|