forked from projects/fipamo
fixes for site init
This commit is contained in:
parent
2b7db3cc88
commit
6279ad4730
4 changed files with 8840 additions and 312 deletions
|
@ -17,7 +17,7 @@ class SetUp
|
||||||
|
|
||||||
public static function init($body)
|
public static function init($body)
|
||||||
{
|
{
|
||||||
//grab template files
|
//grab template files
|
||||||
$newFolks = json_decode(
|
$newFolks = json_decode(
|
||||||
file_get_contents("../config/init/folks-template.json"),
|
file_get_contents("../config/init/folks-template.json"),
|
||||||
true
|
true
|
||||||
|
@ -26,15 +26,15 @@ class SetUp
|
||||||
file_get_contents("../config/init/settings-template.json"),
|
file_get_contents("../config/init/settings-template.json"),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
//get form values
|
//get form values
|
||||||
//$body = $request->getParsedBody();
|
//$body = $request->getParsedBody();
|
||||||
$handle = $body["new_member_handle"];
|
$handle = $body["new_member_handle"];
|
||||||
$email = $body["new_member_email"];
|
$email = $body["new_member_email"];
|
||||||
$pass = $body["new_member_pass"];
|
$pass = $body["new_member_pass"];
|
||||||
$title = $body["new_member_title"];
|
$title = $body["new_member_title"];
|
||||||
|
|
||||||
$now = new \Moment\Moment();
|
$now = new \Moment\Moment();
|
||||||
//setup folks config
|
//setup folks config
|
||||||
$hash = password_hash($pass, PASSWORD_DEFAULT);
|
$hash = password_hash($pass, PASSWORD_DEFAULT);
|
||||||
$newFolks[0]["id"] = 0;
|
$newFolks[0]["id"] = 0;
|
||||||
$newFolks[0]["handle"] = $handle;
|
$newFolks[0]["handle"] = $handle;
|
||||||
|
@ -45,17 +45,18 @@ class SetUp
|
||||||
$newFolks[0]["role"] = "hnic";
|
$newFolks[0]["role"] = "hnic";
|
||||||
$newFolks[0]["created"] = $now->format("Y-m-d\TH:i:sP");
|
$newFolks[0]["created"] = $now->format("Y-m-d\TH:i:sP");
|
||||||
$newFolks[0]["updated"] = $now->format("Y-m-d\TH:i:sP");
|
$newFolks[0]["updated"] = $now->format("Y-m-d\TH:i:sP");
|
||||||
//set up settings config
|
//set up settings config
|
||||||
$newSettings["global"]["title"] = $title;
|
$newSettings["global"]["title"] = $title;
|
||||||
|
|
||||||
//create index file
|
//create index file
|
||||||
//$rightNow = $now->format("Y-m-d\TH:i:sP");
|
//$rightNow = $now->format("Y-m-d\TH:i:sP");
|
||||||
//var_dump($now->format("Y-m-d\TH:i:sP"));
|
//var_dump($now->format("Y-m-d\TH:i:sP"));
|
||||||
$index = [
|
$index = [
|
||||||
"id" => 1,
|
"id" => 1,
|
||||||
"uuid" => StringTools::createUUID(),
|
"uuid" => StringTools::createUUID(),
|
||||||
"title" => "FIRST!",
|
"title" => "FIRST!",
|
||||||
"feature" => "/assets/images/global/default-bg.jpg",
|
"feature" => "/assets/images/global/default-bg.jpg",
|
||||||
|
"files" => "",
|
||||||
"path" => "content/pages/start",
|
"path" => "content/pages/start",
|
||||||
"layout" => "index",
|
"layout" => "index",
|
||||||
"tags" => "start, welcome",
|
"tags" => "start, welcome",
|
||||||
|
@ -73,7 +74,7 @@ class SetUp
|
||||||
|
|
||||||
$freshIndex = DocTools::objectToMD($index);
|
$freshIndex = DocTools::objectToMD($index);
|
||||||
|
|
||||||
//once all files created, write down
|
//once all files created, write down
|
||||||
|
|
||||||
DocTools::writeSettings("../config/settings.json", $newSettings);
|
DocTools::writeSettings("../config/settings.json", $newSettings);
|
||||||
DocTools::writeSettings("../config/folks.json", $newFolks);
|
DocTools::writeSettings("../config/folks.json", $newFolks);
|
||||||
|
@ -85,7 +86,7 @@ class SetUp
|
||||||
$freshIndex
|
$freshIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
//if there is an older session file, get rid of it
|
//if there is an older session file, get rid of it
|
||||||
if (is_file("../content/.session")) {
|
if (is_file("../content/.session")) {
|
||||||
unlink("../content/.session");
|
unlink("../content/.session");
|
||||||
}
|
}
|
||||||
|
@ -108,16 +109,16 @@ class SetUp
|
||||||
$size = $file->getSize();
|
$size = $file->getSize();
|
||||||
$name = $file->getClientFileName();
|
$name = $file->getClientFileName();
|
||||||
|
|
||||||
//park it so it can be read
|
//park it so it can be read
|
||||||
$file->moveTo("../content" . "/" . $name);
|
$file->moveTo("../content" . "/" . $name);
|
||||||
|
|
||||||
//open it and get files to verify user
|
//open it and get files to verify user
|
||||||
$zip = new ZipArchive();
|
$zip = new ZipArchive();
|
||||||
if ($zip->open("../content" . "/" . $name) === true) {
|
if ($zip->open("../content" . "/" . $name) === true) {
|
||||||
$folks = json_decode($zip->getFromName("settings/folks.json"), true);
|
$folks = json_decode($zip->getFromName("settings/folks.json"), true);
|
||||||
$found = find($folks, ["handle" => $body["restore_member_handle"]]);
|
$found = find($folks, ["handle" => $body["restore_member_handle"]]);
|
||||||
|
|
||||||
//if member is found in back up, check pass
|
//if member is found in back up, check pass
|
||||||
if ($found) {
|
if ($found) {
|
||||||
if (password_verify($body["restore_member_pass"], $found["password"])) {
|
if (password_verify($body["restore_member_pass"], $found["password"])) {
|
||||||
//backup verified, restore site
|
//backup verified, restore site
|
||||||
|
@ -144,25 +145,25 @@ class SetUp
|
||||||
file_get_contents("../content/settings/settings.json"),
|
file_get_contents("../content/settings/settings.json"),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
//check for key, add if not there
|
//check for key, add if not there
|
||||||
if (!isset($newConfig["global"]["externalAPI"])) {
|
if (!isset($newConfig["global"]["externalAPI"])) {
|
||||||
$newConfig["global"]["externalAPI"] = "false";
|
$newConfig["global"]["externalAPI"] = "false";
|
||||||
}
|
}
|
||||||
//write new config file
|
//write new config file
|
||||||
DocTools::writeSettings("../config/settings.json", $newConfig);
|
DocTools::writeSettings("../config/settings.json", $newConfig);
|
||||||
|
|
||||||
//rename("../content/settings/folks.json", "../config/folks.json");
|
//rename("../content/settings/folks.json", "../config/folks.json");
|
||||||
DocTools::writeSettings("../config/folks.json", $newFolks);
|
DocTools::writeSettings("../config/folks.json", $newFolks);
|
||||||
|
|
||||||
rename("../content/settings/tags.json", "../config/tags.json");
|
rename("../content/settings/tags.json", "../config/tags.json");
|
||||||
|
|
||||||
//images path for blog and user
|
//images path for blog and user
|
||||||
$blogImagePath = "../public/assets/images/blog";
|
$blogImagePath = "../public/assets/images/blog";
|
||||||
$userImagePath = "../public/assets/images/user";
|
$userImagePath = "../public/assets/images/user";
|
||||||
|
|
||||||
//check to see if image dirs are empty, if not chill
|
//check to see if image dirs are empty, if not chill
|
||||||
if ($globs = glob($blogImagePath . "/*")) {
|
if ($globs = glob($blogImagePath . "/*")) {
|
||||||
//directory not empty, relax
|
//directory not empty, relax
|
||||||
} else {
|
} else {
|
||||||
rename("../content/public/assets/images/blog", $blogImagePath);
|
rename("../content/public/assets/images/blog", $blogImagePath);
|
||||||
}
|
}
|
||||||
|
@ -175,22 +176,22 @@ class SetUp
|
||||||
|
|
||||||
rename("../content/content/pages/", "../content/pages");
|
rename("../content/content/pages/", "../content/pages");
|
||||||
|
|
||||||
//legacy check for old file structure
|
//legacy check for old file structure
|
||||||
if (is_file("../content/pages/index.md")) {
|
if (is_file("../content/pages/index.md")) {
|
||||||
if (!is_dir("../content/pages/start")) {
|
if (!is_dir("../content/pages/start")) {
|
||||||
//Directory does not exist, so lets create it.
|
//Directory does not exist, so lets create it.
|
||||||
mkdir("../content/pages/start", 0755, true);
|
mkdir("../content/pages/start", 0755, true);
|
||||||
//move start page to appropriate spot
|
//move start page to appropriate spot
|
||||||
rename(
|
rename(
|
||||||
"../content/pages/index.md",
|
"../content/pages/index.md",
|
||||||
"../content/pages/start/index.md"
|
"../content/pages/start/index.md"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//chill
|
//chill
|
||||||
}
|
}
|
||||||
|
|
||||||
//clean up
|
//clean up
|
||||||
|
|
||||||
DocTools::deleteFolder("../content/settings");
|
DocTools::deleteFolder("../content/settings");
|
||||||
DocTools::deleteFolder("../content/public");
|
DocTools::deleteFolder("../content/public");
|
||||||
|
@ -214,7 +215,7 @@ class SetUp
|
||||||
|
|
||||||
$zip->close();
|
$zip->close();
|
||||||
$zipPath = "../content/" . $name;
|
$zipPath = "../content/" . $name;
|
||||||
//trash zip when done
|
//trash zip when done
|
||||||
unlink($zipPath);
|
unlink($zipPath);
|
||||||
} else {
|
} else {
|
||||||
$result = [
|
$result = [
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<div id="dash-index">
|
<div id="dash-index">
|
||||||
<div id="dash-index-wrapper">
|
<div id="dash-index-wrapper">
|
||||||
<div id="dash-init" class="dash-init">
|
<div id="dash-init" class="dash-init">
|
||||||
<form id="init-form">
|
<form id="init-form" method="POST">
|
||||||
<img id="the-logo" src="/assets/images/global/fipamo-logo.svg"/>
|
<img id="the-logo" src="/assets/images/global/fipamo-logo.svg"/>
|
||||||
<input type="text" name="new_member_handle" id="new_member_handle" placeholder="handle"/>
|
<input type="text" name="new_member_handle" id="new_member_handle" placeholder="handle"/>
|
||||||
<input type="text" name="new_member_email" id="new_member_email" placeholder="email"/>
|
<input type="text" name="new_member_email" id="new_member_email" placeholder="email"/>
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div id="dash-restore" class="dash-restore">
|
<div id="dash-restore" class="dash-restore">
|
||||||
<form id="init-restore">
|
<form id="init-restore" method="POST">
|
||||||
<img id="the-logo" src="/assets/images/global/fipamo-logo.svg"/>
|
<img id="the-logo" src="/assets/images/global/fipamo-logo.svg"/>
|
||||||
<input type="text" name="restore_member_handle" id="restore_member_handle" placeholder="handle"/><input type="password" name="restore_member_pass" id="restore_member_pass" placeholder="password"/>
|
<input type="text" name="restore_member_handle" id="restore_member_handle" placeholder="handle"/><input type="password" name="restore_member_pass" id="restore_member_pass" placeholder="password"/>
|
||||||
<div>
|
<div>
|
||||||
|
@ -44,5 +44,5 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block javascripts %}
|
{% block javascripts %}
|
||||||
<script src="/assets/scripts/Start.js" type="text/javascript"></script>
|
<script src="/assets/scripts/Start.js?=sdfsdf" type="text/javascript"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
File diff suppressed because it is too large
Load diff
|
@ -1,304 +1,305 @@
|
||||||
//** REQUEST TYPES **//
|
//** REQUEST TYPES **//
|
||||||
export const REQUEST_TYPE_POST = "POST";
|
export const REQUEST_TYPE_POST = 'POST';
|
||||||
export const REQUEST_TYPE_GET = "GET";
|
export const REQUEST_TYPE_GET = 'GET';
|
||||||
export const REQUEST_TYPE_PUT = "PUT";
|
export const REQUEST_TYPE_PUT = 'PUT';
|
||||||
export const REQUEST_TYPE_DELETE = "DELETE";
|
export const REQUEST_TYPE_DELETE = 'DELETE';
|
||||||
//** POST CONTENT TYPES **//
|
//** POST CONTENT TYPES **//
|
||||||
export const CONTENT_TYPE_JSON = "json";
|
export const CONTENT_TYPE_JSON = 'json';
|
||||||
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
|
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
|
||||||
//** API URLS **//
|
//** API URLS **//
|
||||||
export const API_STATUS = "/api/v1/status";
|
export const API_STATUS = '/api/v1/status';
|
||||||
export const API_INIT = "/api/v1/init";
|
export const API_INIT = '/api/v1/init';
|
||||||
export const API_RESTORE = "/api/v1/restore";
|
export const API_RESTORE = '/api/v1/restore';
|
||||||
export const API_GET_SECRET = "/api/v1/get-secret";
|
export const API_GET_SECRET = '/api/v1/get-secret';
|
||||||
export const API_RESET_PASS = "/api/v1/reset-password";
|
export const API_RESET_PASS = '/api/v1/reset-password';
|
||||||
export const API_CREATE_BACKUP = "/api/v1/backup";
|
export const API_CREATE_BACKUP = '/api/v1/backup';
|
||||||
export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download";
|
export const API_DOWNLOAD_BACKUP = '/api/v1/backup/download';
|
||||||
export const API_RESTORE_BACKUP = "/api/v1/backup/restore";
|
export const API_RESTORE_BACKUP = '/api/v1/backup/restore';
|
||||||
export const API_UPLOAD_AVATAR = "/api/v1/settings/add-avatar";
|
export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar';
|
||||||
export const API_UPLOAD_BACKGROUND = "/api/v1/settings/add-feature-background";
|
export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background';
|
||||||
export const API_IMAGE_UPLOAD = "/api/v1/page/add-entry-image";
|
export const API_IMAGE_UPLOAD = '/api/v1/page/add-entry-image';
|
||||||
//** API TASKS **//
|
//** API TASKS **//
|
||||||
export const TASK_SITE_INIT = "blogInit";
|
export const TASK_SITE_INIT = 'blogInit';
|
||||||
export const TASK_BACKUP_RESTORE = "restoreBackup";
|
export const TASK_BACKUP_RESTORE = 'restoreBackup';
|
||||||
export const TASK_BACKUP_CREATE = "createBackup";
|
export const TASK_BACKUP_CREATE = 'createBackup';
|
||||||
export const TASK_GET_SECRET = "retrieveSecret";
|
export const TASK_GET_SECRET = 'retrieveSecret';
|
||||||
export const TASK_RESET_PASS = "resetPassword";
|
export const TASK_RESET_PASS = 'resetPassword';
|
||||||
export const TASK_UPLOAD_FILES = "uploadFiles";
|
export const TASK_UPLOAD_FILES = 'uploadFiles';
|
||||||
//** API STATUS **//
|
//** API STATUS **//
|
||||||
export const API_ACCESS_GOOD = "apiUseAuthorized";
|
export const API_ACCESS_GOOD = 'apiUseAuthorized';
|
||||||
export const API_ACCESS_BAD = "apiUseNotAuthorized";
|
export const API_ACCESS_BAD = 'apiUseNotAuthorized';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A tub of methods for creating/restoring installs, resetting passwords and uploading images.
|
* A tub of methods for creating/restoring installs, resetting passwords and uploading images.
|
||||||
*/
|
*/
|
||||||
class MaintenanceManager {
|
class MaintenanceManager {
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {string} baseURL - url of site; uses local when empty
|
* @param {string} baseURL - url of site; uses local when empty
|
||||||
* @param {string} key - user api key
|
* @param {string} key - user api key
|
||||||
*/
|
*/
|
||||||
constructor(baseURL = null, key = null, progressBar = null) {
|
constructor(baseURL = null, key = null, progressBar = null) {
|
||||||
this.percentComplete = 0; //for later
|
this.percentComplete = 0; //for later
|
||||||
this.token = null;
|
this.token = null;
|
||||||
this.baseURL = null;
|
this.baseURL = null;
|
||||||
this.progressBar = progressBar;
|
this.progressBar = progressBar;
|
||||||
this.key = null;
|
this.key = null;
|
||||||
if (key) this.key = key;
|
if (key) this.key = key;
|
||||||
if (baseURL) this.baseURL = baseURL;
|
if (baseURL) this.baseURL = baseURL;
|
||||||
//if key is valid, checks to see if a session is active and returns
|
//if key is valid, checks to see if a session is active and returns
|
||||||
this._request(
|
this._request(
|
||||||
this.baseURL
|
this.baseURL
|
||||||
? this.baseURL + API_STATUS + "?key=" + this.key
|
? this.baseURL + API_STATUS + '?key=' + this.key
|
||||||
: API_STATUS + "?key=" + this.key
|
: API_STATUS + '?key=' + this.key
|
||||||
).then((response) => {
|
).then(response => {
|
||||||
if (response.type === API_ACCESS_GOOD) {
|
if (response.type === API_ACCESS_GOOD) {
|
||||||
this.token = response.token;
|
this.token = response.token;
|
||||||
} else {
|
} else {
|
||||||
//don't set token
|
//don't set token
|
||||||
//console.log("NO TOKEN");
|
//console.log("NO TOKEN");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promise method used create new site from scratch. For local use only.
|
* Promise method used create new site from scratch. For local use only.
|
||||||
* @param {object} data - json object that contains data for set up
|
* @param {object} data - json object that contains data for set up
|
||||||
* @property {string} new_member_handle - handle for new user
|
* @property {string} new_member_handle - handle for new user
|
||||||
* @property {string} new_member_email - email 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_pass - password for new user
|
||||||
* @property {string} new_member_title - title for new user
|
* @property {string} new_member_title - title for new user
|
||||||
*/
|
*/
|
||||||
create(data) {
|
create(data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._request(
|
this._request(
|
||||||
API_INIT,
|
API_INIT,
|
||||||
TASK_SITE_INIT,
|
TASK_SITE_INIT,
|
||||||
REQUEST_TYPE_POST,
|
REQUEST_TYPE_POST,
|
||||||
CONTENT_TYPE_JSON,
|
CONTENT_TYPE_JSON,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
.then((result) => {
|
.then(result => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Promise method for restoring site from a previous back up. For local use only.
|
* 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
|
* @param {object} form - form object that contains restore data and files
|
||||||
* @property {string} restore_member_handle - handle for site user
|
* @property {string} restore_member_handle - handle for site user
|
||||||
* @property {string} restore_member_pass - password for site user
|
* @property {string} restore_member_pass - password for site user
|
||||||
* @property {file} backup-upload - backup zip file
|
* @property {file} backup-upload - backup zip file
|
||||||
*/
|
*/
|
||||||
restore(form) {
|
restore(form) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var url, event, method, type, data;
|
var url, event, method, type, data;
|
||||||
|
|
||||||
url = API_RESTORE;
|
url = API_RESTORE;
|
||||||
event = TASK_BACKUP_RESTORE;
|
event = TASK_BACKUP_RESTORE;
|
||||||
method = REQUEST_TYPE_POST;
|
method = REQUEST_TYPE_POST;
|
||||||
type = CONTENT_TYPE_FORM;
|
type = CONTENT_TYPE_FORM;
|
||||||
data = new FormData(form);
|
data = new FormData(form);
|
||||||
this._request(url, event, method, type, data)
|
return;
|
||||||
.then((result) => {
|
this._request(url, event, method, type, data)
|
||||||
resolve(result);
|
.then(result => {
|
||||||
})
|
resolve(result);
|
||||||
.catch((err) => {
|
})
|
||||||
reject(err);
|
.catch(err => {
|
||||||
});
|
reject(err);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
/**
|
}
|
||||||
* Promise method for creating a zip back up of current site. For local use only.
|
/**
|
||||||
*/
|
* Promise method for creating a zip back up of current site. For local use only.
|
||||||
|
*/
|
||||||
|
|
||||||
backup() {
|
backup() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var url, event, method, type, data;
|
var url, event, method, type, data;
|
||||||
|
|
||||||
url = API_CREATE_BACKUP;
|
url = API_CREATE_BACKUP;
|
||||||
event = TASK_BACKUP_CREATE;
|
event = TASK_BACKUP_CREATE;
|
||||||
method = REQUEST_TYPE_POST;
|
method = REQUEST_TYPE_POST;
|
||||||
type = CONTENT_TYPE_JSON;
|
type = CONTENT_TYPE_JSON;
|
||||||
data = { task: "create_backup" };
|
data = { task: 'create_backup' };
|
||||||
|
|
||||||
this._request(url, event, method, type, data)
|
this._request(url, event, method, type, data)
|
||||||
.then((result) => {
|
.then(result => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promise method for retrieving user secret key. For local use only.
|
* Promise method for retrieving user secret key. For local use only.
|
||||||
* @param {object} data - json object that contains data for set up
|
* @param {object} data - json object that contains data for set up
|
||||||
* @property {string} email - email for site user
|
* @property {string} email - email for site user
|
||||||
*/
|
*/
|
||||||
|
|
||||||
secret(data) {
|
secret(data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._request(
|
this._request(
|
||||||
API_GET_SECRET,
|
API_GET_SECRET,
|
||||||
TASK_GET_SECRET,
|
TASK_GET_SECRET,
|
||||||
REQUEST_TYPE_POST,
|
REQUEST_TYPE_POST,
|
||||||
CONTENT_TYPE_JSON,
|
CONTENT_TYPE_JSON,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
.then((result) => {
|
.then(result => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Promise method for resetting password for user. For local use only.
|
* Promise method for resetting password for user. For local use only.
|
||||||
* @param {object} data - json object that contains data for set up
|
* @param {object} data - json object that contains data for set up
|
||||||
* @property {string} new_password - password for user
|
* @property {string} new_password - password for user
|
||||||
* @property {string} new_password2 - confirm password for user
|
* @property {string} new_password2 - confirm password for user
|
||||||
* @property {string} secret - secret key for user
|
* @property {string} secret - secret key for user
|
||||||
*/
|
*/
|
||||||
|
|
||||||
newPass(data) {
|
newPass(data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._request(
|
this._request(
|
||||||
API_RESET_PASS,
|
API_RESET_PASS,
|
||||||
TASK_RESET_PASS,
|
TASK_RESET_PASS,
|
||||||
REQUEST_TYPE_POST,
|
REQUEST_TYPE_POST,
|
||||||
CONTENT_TYPE_JSON,
|
CONTENT_TYPE_JSON,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
.then((result) => {
|
.then(result => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promise method for uploading images [todo: change to uploading files]
|
* Promise method for uploading images [todo: change to uploading files]
|
||||||
* @param {string} type - type of upload
|
* @param {string} type - type of upload
|
||||||
* @param {input} files - form input containing files
|
* @param {input} files - form input containing files
|
||||||
*/
|
*/
|
||||||
imageUpload(type, files) {
|
imageUpload(type, files) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let url = "";
|
let url = '';
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "avatar-upload":
|
case 'avatar-upload':
|
||||||
url = API_UPLOAD_AVATAR;
|
url = API_UPLOAD_AVATAR;
|
||||||
break;
|
break;
|
||||||
case "background-upload":
|
case 'background-upload':
|
||||||
url = API_UPLOAD_BACKGROUND;
|
url = API_UPLOAD_BACKGROUND;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
url = API_IMAGE_UPLOAD;
|
url = API_IMAGE_UPLOAD;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var imageData = new FormData();
|
var imageData = new FormData();
|
||||||
|
|
||||||
if (this.baseURL) {
|
if (this.baseURL) {
|
||||||
imageData.append("key", this.key);
|
imageData.append('key', this.key);
|
||||||
imageData.append("remote", true);
|
imageData.append('remote', true);
|
||||||
} else {
|
} else {
|
||||||
imageData.append("remote", false);
|
imageData.append('remote', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < files.length; i++) {
|
for (var i = 0; i < files.length; i++) {
|
||||||
var file = files[i];
|
var file = files[i];
|
||||||
// Check the file type.
|
// Check the file type.
|
||||||
if (!file.type.match("image.*")) {
|
if (!file.type.match('image.*')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (type === "avatar-upload") {
|
if (type === 'avatar-upload') {
|
||||||
imageData.append("avatar_upload", file, file.name);
|
imageData.append('avatar_upload', file, file.name);
|
||||||
} else if (type === "background-upload") {
|
} else if (type === 'background-upload') {
|
||||||
imageData.append("background_upload", file, file.name);
|
imageData.append('background_upload', file, file.name);
|
||||||
} else {
|
} else {
|
||||||
imageData.append("post_image", file, file.name);
|
imageData.append('post_image', file, file.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._request(
|
this._request(
|
||||||
url,
|
url,
|
||||||
TASK_UPLOAD_FILES,
|
TASK_UPLOAD_FILES,
|
||||||
REQUEST_TYPE_POST,
|
REQUEST_TYPE_POST,
|
||||||
CONTENT_TYPE_FORM,
|
CONTENT_TYPE_FORM,
|
||||||
imageData
|
imageData
|
||||||
)
|
)
|
||||||
.then((r) => {
|
.then(r => {
|
||||||
resolve(r);
|
resolve(r);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// private
|
// private
|
||||||
//--------------------------
|
//--------------------------
|
||||||
_request(
|
_request(
|
||||||
requestURL,
|
requestURL,
|
||||||
eventType,
|
eventType,
|
||||||
requestType = REQUEST_TYPE_GET,
|
requestType = REQUEST_TYPE_GET,
|
||||||
contentType = CONTENT_TYPE_JSON,
|
contentType = CONTENT_TYPE_JSON,
|
||||||
requestData = null
|
requestData = null
|
||||||
) {
|
) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
request.upload.addEventListener("progress", (e) =>
|
request.upload.addEventListener('progress', e =>
|
||||||
self.handleLoadProgress(e, self.progressBar)
|
self.handleLoadProgress(e, self.progressBar)
|
||||||
);
|
);
|
||||||
request.open(requestType, requestURL, true);
|
request.open(requestType, requestURL, true);
|
||||||
request.onload = () => {
|
request.onload = () => {
|
||||||
if (request.status == 200) {
|
if (request.status == 200) {
|
||||||
let response = JSON.parse(request["response"]);
|
let response = JSON.parse(request['response']);
|
||||||
resolve(response);
|
resolve(response);
|
||||||
} else {
|
} else {
|
||||||
let error = JSON.parse(request["response"]);
|
let error = JSON.parse(request['response']);
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
||||||
if (eventType === TASK_UPLOAD_FILES)
|
if (eventType === TASK_UPLOAD_FILES)
|
||||||
request.setRequestHeader("fipamo-access-token", self.token);
|
request.setRequestHeader('fipamo-access-token', self.token);
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
case CONTENT_TYPE_JSON:
|
case CONTENT_TYPE_JSON:
|
||||||
request.setRequestHeader(
|
request.setRequestHeader(
|
||||||
"Content-type",
|
'Content-type',
|
||||||
"application/" + contentType
|
'application/' + contentType
|
||||||
);
|
);
|
||||||
request.send(JSON.stringify(requestData));
|
request.send(JSON.stringify(requestData));
|
||||||
break;
|
break;
|
||||||
case CONTENT_TYPE_FORM:
|
case CONTENT_TYPE_FORM:
|
||||||
request.send(requestData);
|
request.send(requestData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
request.send();
|
request.send();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// event handlers
|
// event handlers
|
||||||
//--------------------------
|
//--------------------------
|
||||||
handleLoadProgress(e, progressBar) {
|
handleLoadProgress(e, progressBar) {
|
||||||
let percent = Math.ceil((e.loaded / e.total) * 100);
|
let percent = Math.ceil((e.loaded / e.total) * 100);
|
||||||
//if a progress bar element is present, talk to it
|
//if a progress bar element is present, talk to it
|
||||||
if (progressBar != null) {
|
if (progressBar != null) {
|
||||||
progressBar.style.width = percent + "%";
|
progressBar.style.width = percent + '%';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { MaintenanceManager as default };
|
export { MaintenanceManager as default };
|
||||||
|
|
Loading…
Reference in a new issue