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';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {computed, get} from '@ember/object';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2019-02-26 05:39:04 +03:00
|
|
|
import {isArray} from '@ember/array';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
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
|
|
|
|
2021-07-02 11:38:26 +03:00
|
|
|
export const IMAGE_MIME_TYPES = 'image/gif,image/jpg,image/jpeg,image/png,image/svg+xml,image/webp';
|
|
|
|
export const IMAGE_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'svg', 'webp'];
|
2019-02-26 05:39:04 +03:00
|
|
|
export const IMAGE_PARAMS = {purpose: 'image'};
|
2017-05-08 13:35:42 +03:00
|
|
|
|
2021-01-18 20:48:11 +03:00
|
|
|
export const ICON_EXTENSIONS = ['ico', 'png'];
|
|
|
|
export const ICON_MIME_TYPES = 'image/png,image/x-icon';
|
|
|
|
export const ICON_PARAMS = {purpose: 'icon'};
|
|
|
|
|
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,
|
2019-02-22 14:14:21 +03:00
|
|
|
accept: '',
|
2017-01-26 14:17:34 +03:00
|
|
|
extensions: null,
|
|
|
|
uploadUrl: null,
|
2019-02-26 05:39:04 +03:00
|
|
|
paramName: 'file',
|
|
|
|
paramsHash: null,
|
|
|
|
resourceName: 'images',
|
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,
|
2019-02-26 05:39:04 +03:00
|
|
|
_defaultUploadUrl: '/images/upload/',
|
|
|
|
_defaultParamsHash: IMAGE_PARAMS,
|
2017-08-02 10:05:59 +03:00
|
|
|
_showUnsplash: false,
|
2017-01-26 14:17:34 +03:00
|
|
|
|
2018-03-20 17:57:59 +03:00
|
|
|
// Allowed actions
|
|
|
|
fileSelected: () => {},
|
|
|
|
update: () => {},
|
|
|
|
uploadStarted: () => {},
|
|
|
|
uploadFinished: () => {},
|
|
|
|
uploadSuccess: () => {},
|
|
|
|
uploadFailed: () => {},
|
|
|
|
|
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 () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let file = this.file;
|
2016-02-26 16:25:47 +03:00
|
|
|
let formData = new FormData();
|
|
|
|
|
2019-02-26 05:39:04 +03:00
|
|
|
formData.append(this.paramName, file);
|
|
|
|
|
|
|
|
Object.keys(this.paramsHash || {}).forEach((key) => {
|
|
|
|
formData.append(key, this.paramsHash[key]);
|
|
|
|
});
|
2016-02-26 16:25:47 +03:00
|
|
|
|
|
|
|
return formData;
|
|
|
|
}),
|
|
|
|
|
2016-07-24 12:19:49 +03:00
|
|
|
description: computed('text', 'altText', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let altText = this.altText;
|
2016-07-24 12:19:49 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.text || (altText ? `Upload image of "${altText}"` : 'Upload an image');
|
2016-07-24 12:19:49 +03:00
|
|
|
}),
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
progressStyle: computed('uploadPercentage', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let percentage = this.uploadPercentage;
|
2016-02-26 16:25:47 +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-02-26 16:25:47 +03:00
|
|
|
}),
|
|
|
|
|
2019-02-22 14:14:21 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2017-01-26 14:17:34 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.accept) {
|
|
|
|
this.set('accept', this._defaultAccept);
|
2017-01-26 14:17:34 +03:00
|
|
|
}
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.extensions) {
|
|
|
|
this.set('extensions', this._defaultExtensions);
|
2017-01-26 14:17:34 +03:00
|
|
|
}
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.uploadUrl) {
|
|
|
|
this.set('uploadUrl', this._defaultUploadUrl);
|
2017-01-26 14:17:34 +03:00
|
|
|
}
|
2019-02-26 05:39:04 +03:00
|
|
|
if (!this.paramsHash) {
|
|
|
|
this.set('paramsHash', this._defaultParamsHash);
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2019-02-22 14:14:21 +03:00
|
|
|
didReceiveAttrs() {
|
2021-07-15 17:27:29 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let image = this.image;
|
2019-02-22 14:14:21 +03:00
|
|
|
this.set('url', image);
|
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
fileSelected(fileList, resetInput) {
|
|
|
|
// can't use array destructuring here as FileList is not a strict
|
|
|
|
// array and fails in Safari
|
|
|
|
let file = fileList[0];
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-09 19:55:11 +03:00
|
|
|
addUnsplashPhoto({src}) {
|
|
|
|
this.set('url', src);
|
2018-01-11 20:43:23 +03:00
|
|
|
this.send('saveUrl');
|
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('file', null);
|
|
|
|
this.set('uploadPercentage', 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
saveUrl() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let url = this.url;
|
2018-03-20 17:57:59 +03:00
|
|
|
this.update(url);
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
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-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
|
|
|
_uploadProgress(event) {
|
2016-02-26 16:25:47 +03:00
|
|
|
if (event.lengthComputable) {
|
|
|
|
run(() => {
|
|
|
|
let percentage = Math.round((event.loaded / event.total) * 100);
|
2022-01-06 13:05:15 +03:00
|
|
|
if (!this.isDestroyed && !this.isDestroying) {
|
|
|
|
this.set('uploadPercentage', percentage);
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
_uploadSuccess(response) {
|
2019-02-26 05:39:04 +03:00
|
|
|
let uploadResponse;
|
|
|
|
let responseUrl;
|
|
|
|
|
|
|
|
try {
|
|
|
|
uploadResponse = JSON.parse(response);
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof SyntaxError)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uploadResponse) {
|
|
|
|
let resource = get(uploadResponse, this.resourceName);
|
|
|
|
if (resource && isArray(resource) && resource[0]) {
|
|
|
|
responseUrl = get(resource[0], 'url');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('url', responseUrl);
|
2016-02-26 16:25:47 +03:00
|
|
|
this.send('saveUrl');
|
|
|
|
this.send('reset');
|
2019-02-26 05:39:04 +03:00
|
|
|
this.uploadSuccess(responseUrl);
|
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)) {
|
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)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let validExtensions = this.extensions.join(', .').toUpperCase();
|
2017-01-26 14:17:34 +03:00
|
|
|
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.';
|
2017-11-04 01:59:39 +03:00
|
|
|
} else if (error.payload.errors && !isBlank(error.payload.errors[0].message)) {
|
|
|
|
message = error.payload.errors[0].message;
|
2016-02-26 16:25:47 +03:00
|
|
|
} else {
|
|
|
|
message = 'Something went wrong :(';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('failureMessage', message);
|
2018-03-20 17:57:59 +03:00
|
|
|
this.uploadFailed(error);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
generateRequest() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let ajax = this.ajax;
|
|
|
|
let formData = this.formData;
|
|
|
|
let uploadUrl = this.uploadUrl;
|
2017-01-26 14:17:34 +03:00
|
|
|
// 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
|
|
|
|
2018-03-20 17:57:59 +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) => {
|
2019-02-26 05:39:04 +03:00
|
|
|
this._uploadSuccess(response);
|
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(() => {
|
2018-03-20 17:57:59 +03:00
|
|
|
this.uploadFinished();
|
2016-02-26 16:25:47 +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) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let extensions = this.extensions;
|
2016-09-14 11:54:16 +03:00
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2022-01-21 22:25:47 +03:00
|
|
|
if (!isArray(extensions)) {
|
2016-09-14 11:54:16 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|