forked from projects/fipamo
ro
c5afbb9131
removed all remaining API requests from the front end and removed the FipamoAdminAPI js file, changing it to ContentRequest as it now handles posting data to the system directly, authenticating it self by checking the embedded CSRF token that regulary rotates also renamed MaintenanceManager to Maintenance request and moved it to the same dir as ContentRequest as they are both libraries that connect to the backend
303 lines
7.3 KiB
JavaScript
303 lines
7.3 KiB
JavaScript
//** REQUEST TYPES **//
|
|
export const REQUEST_TYPE_POST = 'POST';
|
|
export const REQUEST_TYPE_GET = 'GET';
|
|
export const REQUEST_TYPE_PUT = 'PUT';
|
|
export const REQUEST_TYPE_DELETE = 'DELETE';
|
|
//** POST CONTENT TYPES **//
|
|
export const CONTENT_TYPE_JSON = 'json';
|
|
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
|
|
//** API URLS **//
|
|
export const API_INIT = '/init/fresh';
|
|
export const API_RESTORE = '/init/restore';
|
|
export const API_RESET = '/init/reset';
|
|
export const API_CREATE_BACKUP = '/backup/create';
|
|
export const API_DOWNLOAD_BACKUP = '/backup/download';
|
|
export const API_RESTORE_BACKUP = '/backup/restore';
|
|
export const API_FILES_UPLOAD = '/upload/files';
|
|
|
|
//export const API_RESET_PASS = '/api/v1/reset-password';
|
|
|
|
//** API TASKS **//
|
|
export const TASK_SITE_INIT = 'blogInit';
|
|
export const TASK_BACKUP_RESTORE = 'restoreBackup';
|
|
export const TASK_BACKUP_CREATE = 'createBackup';
|
|
export const TASK_GET_SECRET = 'retrieveSecret';
|
|
export const TASK_RESET_PASS = 'resetPassword';
|
|
export const TASK_UPLOAD_FILES = 'uploadFiles';
|
|
//** API STATUS **//
|
|
export const API_ACCESS_GOOD = 'apiUseAuthorized';
|
|
export const API_ACCESS_BAD = 'apiUseNotAuthorized';
|
|
|
|
/**
|
|
* A tub of methods for creating/restoring installs, resetting passwords and uploading images.
|
|
*/
|
|
class MaintenanceManager {
|
|
/**
|
|
* @constructor
|
|
* @param {string} baseURL - url of site; uses local when empty
|
|
* @param {string} key - user api key
|
|
*/
|
|
constructor(baseURL = null, key = null) {
|
|
this.accetableFiles = [
|
|
'image/jpeg',
|
|
'image/gif',
|
|
'image/png',
|
|
'image/svg',
|
|
'audio/mpeg',
|
|
'video/mp4',
|
|
'application/pdf',
|
|
'text/plain',
|
|
'text/rtf'
|
|
];
|
|
this.percentComplete = 0; //for later
|
|
this.token = null;
|
|
this.baseURL = null;
|
|
this.key = null;
|
|
if (key) this.key = key;
|
|
if (baseURL) this.baseURL = baseURL;
|
|
}
|
|
|
|
/**
|
|
* Promise method used create new site from scratch. For local use only.
|
|
* @param {object} data - json object that contains data for set up
|
|
* @property {string} new_member_handle - handle for new user
|
|
* @property {string} new_member_email - email for new user
|
|
* @property {string} new_member_pass - password for new user
|
|
* @property {string} new_member_title - title for new user
|
|
*/
|
|
create(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
API_INIT,
|
|
null,
|
|
TASK_SITE_INIT,
|
|
REQUEST_TYPE_POST,
|
|
CONTENT_TYPE_JSON,
|
|
data
|
|
)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Promise method for restoring site from a previous back up. For local use only.
|
|
* @param {object} form - form object that contains restore data and files
|
|
* @property {string} restore_member_handle - handle for site user
|
|
* @property {string} restore_member_pass - password for site user
|
|
* @property {file} backup-upload - backup zip file
|
|
*/
|
|
restore(form) {
|
|
return new Promise((resolve, reject) => {
|
|
var url, event, method, type, data;
|
|
|
|
url = API_RESTORE;
|
|
event = TASK_BACKUP_RESTORE;
|
|
method = REQUEST_TYPE_POST;
|
|
type = CONTENT_TYPE_FORM;
|
|
data = new FormData(form);
|
|
this._request(url, null, event, method, type, data)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Promise method for restoring site to default state
|
|
*/
|
|
reset(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
API_RESET,
|
|
null,
|
|
TASK_SITE_INIT,
|
|
REQUEST_TYPE_POST,
|
|
CONTENT_TYPE_JSON,
|
|
data
|
|
)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Promise method for creating a zip back up of current site. For local use only.
|
|
*/
|
|
|
|
backup(backup_task) {
|
|
return new Promise((resolve, reject) => {
|
|
var url, event, method, type, data;
|
|
|
|
url = API_CREATE_BACKUP;
|
|
event = TASK_BACKUP_CREATE;
|
|
method = REQUEST_TYPE_PUT;
|
|
type = CONTENT_TYPE_JSON;
|
|
data = backup_task;
|
|
this._request(url, null, event, method, type, data)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Promise method for retrieving user secret key. For local use only.
|
|
* @param {object} data - json object that contains data for set up
|
|
* @property {string} email - email for site user
|
|
*/
|
|
|
|
secret(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
API_GET_SECRET,
|
|
TASK_GET_SECRET,
|
|
REQUEST_TYPE_POST,
|
|
CONTENT_TYPE_JSON,
|
|
data
|
|
)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Promise method for resetting password for user. For local use only.
|
|
* @param {object} data - json object that contains data for set up
|
|
* @property {string} new_password - password for user
|
|
* @property {string} new_password2 - confirm password for user
|
|
* @property {string} secret - secret key for user
|
|
*/
|
|
|
|
newPass(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
API_RESET_PASS,
|
|
TASK_RESET_PASS,
|
|
REQUEST_TYPE_POST,
|
|
CONTENT_TYPE_JSON,
|
|
data
|
|
)
|
|
.then(result => {
|
|
resolve(result);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Promise method for uploading files [todo: change to uploading files]
|
|
* @param {string} type - type of upload
|
|
* @param {input} files - form input containing files
|
|
*/
|
|
filesUpload(type, files, progress = null) {
|
|
return new Promise((resolve, reject) => {
|
|
let url = API_FILES_UPLOAD;
|
|
|
|
if (this.baseURL) {
|
|
files.append('key', this.key);
|
|
files.append('remote', true);
|
|
} else {
|
|
files.append('remote', false);
|
|
}
|
|
|
|
this._request(
|
|
url,
|
|
progress,
|
|
TASK_UPLOAD_FILES,
|
|
REQUEST_TYPE_POST,
|
|
CONTENT_TYPE_FORM,
|
|
files
|
|
)
|
|
.then(r => {
|
|
resolve(r);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
//--------------------------
|
|
// private
|
|
//--------------------------
|
|
_request(
|
|
requestURL,
|
|
progressBar = null,
|
|
eventType,
|
|
requestType = REQUEST_TYPE_GET,
|
|
contentType = CONTENT_TYPE_JSON,
|
|
requestData = null
|
|
) {
|
|
var self = this;
|
|
return new Promise(function (resolve, reject) {
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.upload.addEventListener('progress', e =>
|
|
self.handleLoadProgress(e, progressBar)
|
|
);
|
|
request.open(requestType, requestURL, true);
|
|
request.setRequestHeader(
|
|
'X-CSRF-TOKEN',
|
|
document.querySelector('meta[name="csrf-token"]').content
|
|
);
|
|
request.onload = () => {
|
|
if (request.status == 200) {
|
|
let response = JSON.parse(request['response']);
|
|
resolve(response);
|
|
} else {
|
|
let error = JSON.parse(request['response']);
|
|
reject(error);
|
|
}
|
|
};
|
|
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
|
switch (contentType) {
|
|
case CONTENT_TYPE_JSON:
|
|
request.setRequestHeader(
|
|
'Content-type',
|
|
'application/' + contentType
|
|
);
|
|
request.send(JSON.stringify(requestData));
|
|
break;
|
|
case CONTENT_TYPE_FORM:
|
|
request.send(requestData);
|
|
break;
|
|
}
|
|
} else {
|
|
request.send();
|
|
}
|
|
});
|
|
}
|
|
|
|
//--------------------------
|
|
// event handlers
|
|
//--------------------------
|
|
handleLoadProgress(e, progressBar) {
|
|
let percent = Math.ceil((e.loaded / e.total) * 100);
|
|
//if a progress bar element is present, talk to it
|
|
if (progressBar != null) {
|
|
progressBar.style.width = percent + '%';
|
|
}
|
|
}
|
|
}
|
|
|
|
export { MaintenanceManager as default };
|