2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
2016-09-01 16:08:37 +03:00
|
|
|
import RSVP from 'rsvp';
|
2016-06-30 13:21:47 +03:00
|
|
|
import Controller from 'ember-controller';
|
|
|
|
import injectService from 'ember-service/inject';
|
2016-08-22 14:45:33 +03:00
|
|
|
import {isBlank} from 'ember-utils';
|
2016-06-30 13:21:47 +03:00
|
|
|
import {isEmberArray} from 'ember-array/utils';
|
2017-03-08 16:55:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2016-09-01 16:08:37 +03:00
|
|
|
import {UnsupportedMediaTypeError, isUnsupportedMediaTypeError} from 'ghost-admin/services/ajax';
|
|
|
|
|
|
|
|
const {Promise} = RSVP;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2014-04-08 02:01:46 +04:00
|
|
|
uploadButtonText: 'Import',
|
2014-07-29 01:41:45 +04:00
|
|
|
importErrors: '',
|
2015-08-10 18:43:49 +03:00
|
|
|
submitting: false,
|
2015-11-18 13:50:48 +03:00
|
|
|
showDeleteAllModal: false,
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2016-09-01 09:50:34 +03:00
|
|
|
importMimeType: ['application/json', 'application/zip', 'application/x-zip-compressed'],
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
notifications: injectService(),
|
|
|
|
session: injectService(),
|
|
|
|
ajax: injectService(),
|
2015-10-23 12:03:38 +03:00
|
|
|
|
2016-09-01 16:08:37 +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();
|
|
|
|
},
|
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
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(),
|
|
|
|
|
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');
|
2014-12-11 19:55:14 +03:00
|
|
|
this.set('importErrors', '');
|
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
|
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).then(() => {
|
2017-04-19 19:57:56 +03:00
|
|
|
let store = this.get('store');
|
|
|
|
|
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();
|
2015-07-03 14:27:36 +03:00
|
|
|
// Reload currentUser and set session
|
2017-04-19 19:57:56 +03:00
|
|
|
this.set('session.user', store.findRecord('user', currentUserId));
|
2015-06-19 00:56:18 +03:00
|
|
|
// TODO: keep as notification, add link to view content
|
2015-11-18 13:50:48 +03:00
|
|
|
notifications.showNotification('Import successful.', {key: 'import.upload.success'});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((response) => {
|
2016-09-01 16:08:37 +03:00
|
|
|
if (isUnsupportedMediaTypeError(response)) {
|
|
|
|
this.set('importErrors', [response]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
if (response && response.errors && isEmberArray(response.errors)) {
|
2016-01-18 18:37:14 +03:00
|
|
|
this.set('importErrors', response.errors);
|
2014-07-29 01:41:45 +04:00
|
|
|
}
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAlert('Import Failed', {type: 'error', key: 'import.upload.failed'});
|
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
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
exportData() {
|
|
|
|
let dbUrl = this.get('ghostPaths.url').api('db');
|
|
|
|
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');
|
2014-04-08 02:01:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|