2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import RSVP from 'rsvp';
|
2018-05-03 19:52:39 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {
|
|
|
|
UnsupportedMediaTypeError,
|
2017-09-22 22:59:05 +03:00
|
|
|
isRequestEntityTooLargeError,
|
2017-05-29 21:50:03 +03:00
|
|
|
isUnsupportedMediaTypeError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
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';
|
2021-07-21 00:18:45 +03:00
|
|
|
import {set} from '@ember/object';
|
2017-09-21 18:01:40 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2016-09-01 16:08:37 +03:00
|
|
|
|
|
|
|
const {Promise} = RSVP;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
const IMPORT_MIME_TYPES = [
|
2017-11-24 21:53:19 +03:00
|
|
|
'application/json',
|
|
|
|
'application/zip',
|
|
|
|
'application/x-zip-compressed'
|
|
|
|
];
|
|
|
|
|
|
|
|
const JSON_EXTENSION = ['json'];
|
|
|
|
const JSON_MIME_TYPE = ['application/json'];
|
|
|
|
|
2019-04-03 21:54:05 +03:00
|
|
|
const YAML_EXTENSION = ['yaml'];
|
2018-07-24 14:20:53 +03:00
|
|
|
const YAML_MIME_TYPE = [
|
|
|
|
'text/vnd.yaml',
|
|
|
|
'application/vnd.yaml',
|
|
|
|
'text/x-yaml',
|
|
|
|
'application/x-yaml'
|
|
|
|
];
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
ajax: service(),
|
|
|
|
config: service(),
|
2018-01-11 17:16:42 +03:00
|
|
|
feature: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
settings: service(),
|
2021-10-05 16:21:07 +03:00
|
|
|
utils: service(),
|
2015-10-23 12:03:38 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
importErrors: null,
|
|
|
|
importSuccessful: false,
|
|
|
|
showDeleteAllModal: false,
|
2021-09-07 14:36:17 +03:00
|
|
|
showEarlyAccessModal: false,
|
2021-09-08 11:40:52 +03:00
|
|
|
showEnableTiersModal: false,
|
2018-01-11 20:43:23 +03:00
|
|
|
submitting: false,
|
|
|
|
uploadButtonText: 'Import',
|
2017-09-21 18:01:40 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
importMimeType: null,
|
2021-02-22 03:27:00 +03:00
|
|
|
redirectsFileExtensions: null,
|
|
|
|
redirectsFileMimeTypes: null,
|
2018-07-24 14:20:53 +03:00
|
|
|
yamlExtension: null,
|
|
|
|
yamlMimeType: null,
|
2019-11-15 16:10:45 +03:00
|
|
|
|
2020-05-08 14:05:55 +03:00
|
|
|
yamlAccept: null,
|
|
|
|
|
2021-04-22 20:41:41 +03:00
|
|
|
isOAuthConfigurationOpen: false,
|
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.importMimeType = IMPORT_MIME_TYPES;
|
2021-02-22 03:27:00 +03:00
|
|
|
this.redirectsFileExtensions = [...JSON_EXTENSION, ...YAML_EXTENSION];
|
|
|
|
// .yaml is added below for file dialogs to show .yaml by default.
|
|
|
|
this.redirectsFileMimeTypes = [...JSON_MIME_TYPE, ...YAML_MIME_TYPE, '.yaml'];
|
2018-07-24 14:20:53 +03:00
|
|
|
this.yamlExtension = YAML_EXTENSION;
|
|
|
|
this.yamlMimeType = YAML_MIME_TYPE;
|
2020-05-08 14:05:55 +03:00
|
|
|
// (macOS) Safari only allows files with the `yml` extension to be selected with the specified MIME types
|
|
|
|
// so explicitly allow the `yaml` extension.
|
|
|
|
this.yamlAccept = [...this.yamlMimeType, ...Array.from(this.yamlExtension, extension => '.' + extension)];
|
2018-03-19 14:54:54 +03:00
|
|
|
},
|
|
|
|
|
2014-04-08 02:01:46 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
onUpload(file) {
|
|
|
|
let formData = new FormData();
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2015-10-28 14:36:45 +03:00
|
|
|
let currentUserId = this.get('session.user.id');
|
2016-01-18 18:37:14 +03:00
|
|
|
let dbUrl = this.get('ghostPaths.url').api('db');
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2014-04-08 02:01:46 +04:00
|
|
|
this.set('uploadButtonText', 'Importing');
|
2017-05-24 16:36:59 +03:00
|
|
|
this.set('importErrors', null);
|
|
|
|
this.set('importSuccessful', false);
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2016-09-01 16:08:37 +03:00
|
|
|
return this._validate(file).then(() => {
|
|
|
|
formData.append('importfile', file);
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.ajax.post(dbUrl, {
|
2016-09-01 16:08:37 +03:00
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false
|
|
|
|
});
|
2017-05-24 16:36:59 +03:00
|
|
|
}).then((response) => {
|
2019-03-06 16:53:54 +03:00
|
|
|
let store = this.store;
|
2017-04-19 19:57:56 +03:00
|
|
|
|
2017-05-24 16:36:59 +03:00
|
|
|
this.set('importSuccessful', true);
|
|
|
|
|
|
|
|
if (response.problems) {
|
|
|
|
this.set('importErrors', response.problems);
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:42:45 +03:00
|
|
|
// Clear the store, so that all the new data gets fetched correctly.
|
2017-04-19 19:57:56 +03:00
|
|
|
store.unloadAll();
|
2017-05-23 11:17:12 +03:00
|
|
|
|
|
|
|
// NOTE: workaround for behaviour change in Ember 2.13
|
|
|
|
// store.unloadAll has some async tendencies so we need to schedule
|
|
|
|
// the reload of the current user once the unload has finished
|
|
|
|
// https://github.com/emberjs/data/issues/4963
|
|
|
|
run.schedule('destroy', this, () => {
|
|
|
|
// Reload currentUser and set session
|
2021-07-08 16:37:31 +03:00
|
|
|
this.session.populateUser({id: currentUserId});
|
2017-05-24 13:58:06 +03:00
|
|
|
|
2017-05-23 11:17:12 +03:00
|
|
|
// TODO: keep as notification, add link to view content
|
2020-02-27 12:19:29 +03:00
|
|
|
notifications.showNotification('Import successful', {key: 'import.upload.success'});
|
2017-05-24 13:58:06 +03:00
|
|
|
|
|
|
|
// reload settings
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.settings.reload().then((settings) => {
|
|
|
|
this.feature.fetch();
|
|
|
|
this.config.set('blogTitle', settings.get('title'));
|
2017-06-22 13:51:38 +03:00
|
|
|
});
|
2017-05-23 11:17:12 +03:00
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((response) => {
|
2017-09-22 22:59:05 +03:00
|
|
|
if (isUnsupportedMediaTypeError(response) || isRequestEntityTooLargeError(response)) {
|
2018-01-03 15:30:42 +03:00
|
|
|
this.set('importErrors', [response]);
|
|
|
|
} else if (response && response.payload.errors && isEmberArray(response.payload.errors)) {
|
|
|
|
this.set('importErrors', response.payload.errors);
|
|
|
|
} else {
|
|
|
|
this.set('importErrors', [{message: 'Import failed due to an unknown error. Check the Web Inspector console and network tabs for errors.'}]);
|
2014-07-29 01:41:45 +04:00
|
|
|
}
|
2017-11-16 17:04:33 +03:00
|
|
|
|
|
|
|
throw response;
|
2015-10-28 14:36:45 +03:00
|
|
|
}).finally(() => {
|
|
|
|
this.set('uploadButtonText', 'Import');
|
2014-06-23 18:24:37 +04:00
|
|
|
});
|
2014-04-08 02:01:46 +04:00
|
|
|
},
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2018-09-17 13:48:01 +03:00
|
|
|
downloadFile(endpoint) {
|
2021-10-05 16:21:07 +03:00
|
|
|
this.utils.downloadFile(this.ghostPaths.url.api(endpoint));
|
2014-06-23 18:24:37 +04:00
|
|
|
},
|
|
|
|
|
2021-04-22 20:41:41 +03:00
|
|
|
async saveOAuthSettings() {
|
|
|
|
await this.settings.save();
|
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
toggleDeleteAllModal() {
|
|
|
|
this.toggleProperty('showDeleteAllModal');
|
2017-09-21 18:01:40 +03:00
|
|
|
},
|
|
|
|
|
2021-09-07 14:36:17 +03:00
|
|
|
toggleEarlyAccessModal() {
|
|
|
|
this.toggleProperty('showEarlyAccessModal');
|
|
|
|
},
|
|
|
|
|
2021-09-08 11:40:52 +03:00
|
|
|
toggleEnableTiersModal() {
|
|
|
|
this.toggleProperty('showEnableTiersModal');
|
|
|
|
},
|
|
|
|
|
2021-04-22 20:41:41 +03:00
|
|
|
async toggleIsOAuthEnabled() {
|
|
|
|
if (this.isOAuthEnabled) {
|
|
|
|
this.settings.set('oauthClientId', '');
|
|
|
|
this.settings.set('oauthClientSecret', '');
|
|
|
|
set(this, 'isOAuthConfigurationOpen', false);
|
|
|
|
await this.settings.save();
|
|
|
|
} else {
|
|
|
|
set(this, 'isOAuthConfigurationOpen', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-21 18:01:40 +03:00
|
|
|
/**
|
|
|
|
* Opens a file selection dialog - Triggered by "Upload x" buttons,
|
|
|
|
* searches for the hidden file input within the .gh-setting element
|
|
|
|
* containing the clicked button then simulates a click
|
|
|
|
* @param {MouseEvent} event - MouseEvent fired by the button click
|
|
|
|
*/
|
|
|
|
triggerFileDialog(event) {
|
2017-11-22 20:04:48 +03:00
|
|
|
// simulate click to open file dialog
|
|
|
|
// using jQuery because IE11 doesn't support MouseEvent
|
|
|
|
$(event.target)
|
2017-11-30 10:00:25 +03:00
|
|
|
.closest('.gh-setting-action')
|
2017-11-22 20:04:48 +03:00
|
|
|
.find('input[type="file"]')
|
|
|
|
.click();
|
2019-02-26 06:29:57 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
// TODO: convert to ember-concurrency task
|
|
|
|
_validate(file) {
|
|
|
|
// Windows doesn't have mime-types for json files by default, so we
|
|
|
|
// need to have some additional checking
|
|
|
|
if (file.type === '') {
|
|
|
|
// First check file extension so we can early return
|
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
|
|
|
|
|
|
|
if (!extension || extension.toLowerCase() !== 'json') {
|
|
|
|
return RSVP.reject(new UnsupportedMediaTypeError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Extension is correct, so check the contents of the file
|
|
|
|
let reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = function () {
|
|
|
|
let {result} = reader;
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSON.parse(result);
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
} catch (e) {
|
|
|
|
return reject(new UnsupportedMediaTypeError());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let accept = this.importMimeType;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
if (!isBlank(accept) && file && accept.indexOf(file.type) === -1) {
|
|
|
|
return RSVP.reject(new UnsupportedMediaTypeError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return RSVP.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
redirectUploadResult: task(function* (success) {
|
|
|
|
this.set('redirectSuccess', success);
|
|
|
|
this.set('redirectFailure', !success);
|
|
|
|
|
2018-05-03 19:52:39 +03:00
|
|
|
yield timeout(config.environment === 'test' ? 100 : 5000);
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
this.set('redirectSuccess', null);
|
|
|
|
this.set('redirectFailure', null);
|
|
|
|
return true;
|
|
|
|
}).drop(),
|
|
|
|
|
2018-07-24 14:20:53 +03:00
|
|
|
routesUploadResult: task(function* (success) {
|
|
|
|
this.set('routesSuccess', success);
|
|
|
|
this.set('routesFailure', !success);
|
|
|
|
|
|
|
|
yield timeout(config.environment === 'test' ? 100 : 5000);
|
|
|
|
|
|
|
|
this.set('routesSuccess', null);
|
|
|
|
this.set('routesFailure', null);
|
|
|
|
return true;
|
|
|
|
}).drop(),
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
reset() {
|
|
|
|
this.set('importErrors', null);
|
|
|
|
this.set('importSuccessful', false);
|
2014-04-08 02:01:46 +04:00
|
|
|
}
|
|
|
|
});
|