2022-02-01 12:34:03 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
import {inject as service} from '@ember/service';
|
2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
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';
|
2022-05-16 19:37:00 +03:00
|
|
|
import {action} from '@ember/object';
|
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-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'
|
|
|
|
];
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@classic
|
|
|
|
export default class LabsController extends Controller {
|
2022-02-01 20:03:45 +03:00
|
|
|
@service ajax;
|
|
|
|
@service config;
|
|
|
|
@service feature;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
@service utils;
|
2022-02-01 12:34:03 +03:00
|
|
|
|
|
|
|
importErrors = null;
|
|
|
|
importSuccessful = false;
|
|
|
|
showDeleteAllModal = false;
|
|
|
|
showEarlyAccessModal = false;
|
|
|
|
submitting = false;
|
|
|
|
uploadButtonText = 'Import';
|
|
|
|
importMimeType = null;
|
|
|
|
redirectsFileExtensions = null;
|
|
|
|
redirectsFileMimeTypes = null;
|
|
|
|
yamlExtension = null;
|
|
|
|
yamlMimeType = null;
|
|
|
|
yamlAccept = null;
|
2021-04-22 20:41:41 +03:00
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
init() {
|
2022-02-01 12:34:03 +03:00
|
|
|
super.init(...arguments);
|
2018-03-19 14:54:54 +03:00
|
|
|
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)];
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onUpload(file) {
|
|
|
|
let formData = new FormData();
|
|
|
|
let notifications = this.notifications;
|
|
|
|
let currentUserId = this.get('session.user.id');
|
|
|
|
let dbUrl = this.get('ghostPaths.url').api('db');
|
|
|
|
|
|
|
|
this.set('uploadButtonText', 'Importing');
|
|
|
|
this.set('importErrors', null);
|
|
|
|
this.set('importSuccessful', false);
|
|
|
|
|
|
|
|
return this._validate(file).then(() => {
|
|
|
|
formData.append('importfile', file);
|
|
|
|
|
|
|
|
return this.ajax.post(dbUrl, {
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false
|
2014-06-23 18:24:37 +04:00
|
|
|
});
|
2022-02-01 12:34:03 +03:00
|
|
|
}).then((response) => {
|
|
|
|
let store = this.store;
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
this.set('importSuccessful', true);
|
2014-06-23 18:24:37 +04:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
if (response.problems) {
|
|
|
|
this.set('importErrors', response.problems);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the store, so that all the new data gets fetched correctly.
|
|
|
|
store.unloadAll();
|
|
|
|
|
|
|
|
// 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.session.populateUser({id: currentUserId});
|
|
|
|
|
|
|
|
// TODO: keep as notification, add link to view content
|
|
|
|
notifications.showNotification('Import successful', {key: 'import.upload.success'});
|
|
|
|
|
|
|
|
// reload settings
|
|
|
|
return this.settings.reload().then((settings) => {
|
|
|
|
this.feature.fetch();
|
|
|
|
this.config.set('blogTitle', settings.get('title'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch((response) => {
|
|
|
|
if (isUnsupportedMediaTypeError(response) || isRequestEntityTooLargeError(response)) {
|
|
|
|
this.set('importErrors', [response]);
|
|
|
|
} else if (response && response.payload.errors && isEmberArray(response.payload.errors)) {
|
|
|
|
this.set('importErrors', response.payload.errors);
|
2021-04-22 20:41:41 +03:00
|
|
|
} else {
|
2022-02-01 12:34:03 +03:00
|
|
|
this.set('importErrors', [{message: 'Import failed due to an unknown error. Check the Web Inspector console and network tabs for errors.'}]);
|
2021-04-22 20:41:41 +03:00
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
|
|
|
|
throw response;
|
|
|
|
}).finally(() => {
|
|
|
|
this.set('uploadButtonText', 'Import');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
downloadFile(endpoint) {
|
|
|
|
this.utils.downloadFile(this.ghostPaths.url.api(endpoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleDeleteAllModal() {
|
|
|
|
this.toggleProperty('showDeleteAllModal');
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleEarlyAccessModal() {
|
|
|
|
this.toggleProperty('showEarlyAccessModal');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
@action
|
|
|
|
triggerFileDialog(event) {
|
|
|
|
// simulate click to open file dialog
|
2022-05-29 13:18:49 +03:00
|
|
|
event?.target.closest('.gh-setting-action')?.querySelector('input[type="file"]')?.click();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
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();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@(task(function* (success) {
|
2018-01-11 20:43:23 +03:00
|
|
|
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;
|
2022-02-01 12:34:03 +03:00
|
|
|
}).drop())
|
2022-02-10 13:41:36 +03:00
|
|
|
redirectUploadResult;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@(task(function* (success) {
|
2018-07-24 14:20:53 +03:00
|
|
|
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;
|
2022-02-01 12:34:03 +03:00
|
|
|
}).drop())
|
2022-02-10 13:41:36 +03:00
|
|
|
routesUploadResult;
|
2018-07-24 14:20:53 +03:00
|
|
|
|
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
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|