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-09-21 18:01:40 +03:00
|
|
|
import Ember from 'ember';
|
2017-05-29 21:50:03 +03:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
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';
|
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'];
|
|
|
|
|
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(),
|
2015-10-23 12:03:38 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
importErrors: null,
|
|
|
|
importSuccessful: false,
|
|
|
|
showDeleteAllModal: false,
|
|
|
|
submitting: false,
|
|
|
|
uploadButtonText: 'Import',
|
2017-09-21 18:01:40 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
importMimeType: null,
|
|
|
|
jsonExtension: null,
|
|
|
|
jsonMimeType: null,
|
2017-05-24 16:36:59 +03:00
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.importMimeType = IMPORT_MIME_TYPES;
|
|
|
|
this.jsonExtension = JSON_EXTENSION;
|
|
|
|
this.jsonMimeType = JSON_MIME_TYPE;
|
|
|
|
},
|
|
|
|
|
2014-04-08 02:01:46 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
onUpload(file) {
|
|
|
|
let formData = new FormData();
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
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
|
|
|
|
2016-09-01 16:08:37 +03:00
|
|
|
return this.get('ajax').post(dbUrl, {
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false
|
|
|
|
});
|
2017-05-24 16:36:59 +03:00
|
|
|
}).then((response) => {
|
2017-04-19 19:57:56 +03:00
|
|
|
let store = this.get('store');
|
|
|
|
|
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
|
|
|
|
this.set('session.user', store.findRecord('user', 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
|
|
|
|
notifications.showNotification('Import successful.', {key: 'import.upload.success'});
|
2017-05-24 13:58:06 +03:00
|
|
|
|
|
|
|
// reload settings
|
2017-06-22 13:51:38 +03:00
|
|
|
return this.get('settings').reload().then((settings) => {
|
2018-01-11 17:16:42 +03:00
|
|
|
this.get('feature').fetch();
|
2017-06-22 13:51:38 +03:00
|
|
|
this.get('config').set('blogTitle', settings.get('title'));
|
|
|
|
});
|
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
|
|
|
|
2017-09-21 18:01:40 +03:00
|
|
|
downloadFile(url) {
|
|
|
|
let dbUrl = this.get('ghostPaths.url').api(url);
|
2015-10-28 14:36:45 +03:00
|
|
|
let accessToken = this.get('session.data.authenticated.access_token');
|
|
|
|
let downloadURL = `${dbUrl}?access_token=${accessToken}`;
|
|
|
|
let iframe = $('#iframeDownload');
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2014-07-25 19:14:48 +04:00
|
|
|
if (iframe.length === 0) {
|
2014-10-25 01:09:50 +04:00
|
|
|
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
2014-07-25 19:14:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
iframe.attr('src', downloadURL);
|
2014-06-23 18:24:37 +04:00
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
toggleDeleteAllModal() {
|
|
|
|
this.toggleProperty('showDeleteAllModal');
|
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();
|
2014-04-08 02:01:46 +04: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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let accept = this.get('importMimeType');
|
|
|
|
|
|
|
|
if (!isBlank(accept) && file && accept.indexOf(file.type) === -1) {
|
|
|
|
return RSVP.reject(new UnsupportedMediaTypeError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return RSVP.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
sendTestEmail: task(function* () {
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
let emailUrl = this.get('ghostPaths.url').api('mail', 'test');
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield this.get('ajax').post(emailUrl);
|
|
|
|
notifications.showAlert('Check your email for the test message.', {type: 'info', key: 'test-email.send.success'});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'test-email:send'});
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
redirectUploadResult: task(function* (success) {
|
|
|
|
this.set('redirectSuccess', success);
|
|
|
|
this.set('redirectFailure', !success);
|
|
|
|
|
|
|
|
yield timeout(Ember.testing ? 100 : 5000); // eslint-disable-line
|
|
|
|
|
|
|
|
this.set('redirectSuccess', null);
|
|
|
|
this.set('redirectFailure', null);
|
|
|
|
return true;
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('importErrors', null);
|
|
|
|
this.set('importSuccessful', false);
|
2014-04-08 02:01:46 +04:00
|
|
|
}
|
|
|
|
});
|