Roll Back compile version
This commit is contained in:
+122
-106
@@ -1,121 +1,137 @@
|
||||
import path from "path";
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const path_1 = __importDefault(require("path"));
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
import fs from "fs";
|
||||
import { execSync } from "child_process";
|
||||
import EJSON from "../../../utils/ejson";
|
||||
import DB_HANDLER from "../../../utils/backend/global-db/DB_HANDLER";
|
||||
import addDbEntry from "../../../functions/backend/db/addDbEntry";
|
||||
import addMariadbUser from "../../../functions/backend/addMariadbUser";
|
||||
import updateDbEntry from "../../../functions/backend/db/updateDbEntry";
|
||||
import hashPassword from "../../../functions/dsql/hashPassword";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const child_process_1 = require("child_process");
|
||||
const ejson_1 = __importDefault(require("../../../utils/ejson"));
|
||||
const DB_HANDLER_1 = __importDefault(require("../../../utils/backend/global-db/DB_HANDLER"));
|
||||
const addDbEntry_1 = __importDefault(require("../../../functions/backend/db/addDbEntry"));
|
||||
const addMariadbUser_1 = __importDefault(require("../../../functions/backend/addMariadbUser"));
|
||||
const updateDbEntry_1 = __importDefault(require("../../../functions/backend/db/updateDbEntry"));
|
||||
const hashPassword_1 = __importDefault(require("../../../functions/dsql/hashPassword"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../../utils/backend/names/grab-dir-names"));
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
var _a, _b;
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir === null || tmpDir === void 0 ? void 0 : tmpDir.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./new-user.json");
|
||||
const userObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
if (typeof userObj !== "object" || Array.isArray(userObj))
|
||||
throw new Error("User Object Invalid!");
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../../");
|
||||
function createUser() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a, _b;
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
const first_name = userObj.first_name;
|
||||
const last_name = userObj.last_name;
|
||||
const email = userObj.email;
|
||||
const password = userObj.password;
|
||||
const username = userObj.username;
|
||||
if (!(email === null || email === void 0 ? void 0 : email.match(/.*@.*\..*/)))
|
||||
return false;
|
||||
if (!(first_name === null || first_name === void 0 ? void 0 : first_name.match(/^[a-zA-Z]+$/)) ||
|
||||
!(last_name === null || last_name === void 0 ? void 0 : last_name.match(/^[a-zA-Z]+$/)))
|
||||
return false;
|
||||
if (password === null || password === void 0 ? void 0 : password.match(/ /))
|
||||
return false;
|
||||
if (username === null || username === void 0 ? void 0 : username.match(/ /))
|
||||
return false;
|
||||
let hashedPassword = hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: password,
|
||||
});
|
||||
let existingUser = await DB_HANDLER(`SELECT * FROM users WHERE email='${email}'`);
|
||||
if (existingUser === null || existingUser === void 0 ? void 0 : existingUser[0]) {
|
||||
console.log("User Exists");
|
||||
return false;
|
||||
}
|
||||
const newUser = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: Object.assign(Object.assign({}, userObj), { password: hashedPassword }),
|
||||
});
|
||||
if (!((_a = newUser === null || newUser === void 0 ? void 0 : newUser.payload) === null || _a === void 0 ? void 0 : _a.insertId))
|
||||
return false;
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
throw new Error("No Static Path");
|
||||
}
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}`);
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
fs.mkdirSync(newUserMediaFolderPath, { recursive: true });
|
||||
fs.writeFileSync(`${newUserSchemaFolderPath}/main.json`, JSON.stringify([]), "utf8");
|
||||
const imageBasePath = path.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}`);
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
fs.mkdirSync(imageBasePath, { recursive: true });
|
||||
}
|
||||
let imagePath = path.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile.jpg`);
|
||||
let imageThumbnailPath = path.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile-thumbnail.jpg`);
|
||||
let prodImageUrl = imagePath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
|
||||
let prodImageThumbnailUrl = imageThumbnailPath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
|
||||
fs.copyFileSync(path.join(ROOT_DIR, "/public/images/user-preset.png"), imagePath);
|
||||
fs.copyFileSync(path.join(ROOT_DIR, "/public/images/user-preset-thumbnail.png"), imageThumbnailPath);
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
const updateImages = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.payload.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
},
|
||||
});
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir === null || tmpDir === void 0 ? void 0 : tmpDir.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path_1.default.resolve(process.cwd(), tmpDir)
|
||||
: path_1.default.resolve(__dirname, "./new-user.json");
|
||||
const userObj = ejson_1.default.parse(fs_1.default.readFileSync(targetPath, "utf-8"));
|
||||
if (typeof userObj !== "object" || Array.isArray(userObj))
|
||||
throw new Error("User Object Invalid!");
|
||||
const ROOT_DIR = path_1.default.resolve(__dirname, "../../../");
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
const first_name = userObj.first_name;
|
||||
const last_name = userObj.last_name;
|
||||
const email = userObj.email;
|
||||
const password = userObj.password;
|
||||
const username = userObj.username;
|
||||
if (!(email === null || email === void 0 ? void 0 : email.match(/.*@.*\..*/)))
|
||||
return false;
|
||||
if (!(first_name === null || first_name === void 0 ? void 0 : first_name.match(/^[a-zA-Z]+$/)) ||
|
||||
!(last_name === null || last_name === void 0 ? void 0 : last_name.match(/^[a-zA-Z]+$/)))
|
||||
return false;
|
||||
if (password === null || password === void 0 ? void 0 : password.match(/ /))
|
||||
return false;
|
||||
if (username === null || username === void 0 ? void 0 : username.match(/ /))
|
||||
return false;
|
||||
let hashedPassword = (0, hashPassword_1.default)({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: password,
|
||||
});
|
||||
let existingUser = yield (0, DB_HANDLER_1.default)(`SELECT * FROM users WHERE email='${email}'`);
|
||||
if (existingUser === null || existingUser === void 0 ? void 0 : existingUser[0]) {
|
||||
console.log("User Exists");
|
||||
return false;
|
||||
}
|
||||
catch (error) { }
|
||||
const newUser = yield (0, addDbEntry_1.default)({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: Object.assign(Object.assign({}, userObj), { password: hashedPassword }),
|
||||
});
|
||||
if (!((_a = newUser === null || newUser === void 0 ? void 0 : newUser.payload) === null || _a === void 0 ? void 0 : _a.insertId))
|
||||
return false;
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
yield (0, addMariadbUser_1.default)({ userId: newUser.payload.insertId });
|
||||
const { STATIC_ROOT } = (0, grab_dir_names_1.default)();
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
throw new Error("No Static Path");
|
||||
}
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
let newUserMediaFolderPath = path_1.default.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}`);
|
||||
fs_1.default.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
fs_1.default.mkdirSync(newUserMediaFolderPath, { recursive: true });
|
||||
fs_1.default.writeFileSync(`${newUserSchemaFolderPath}/main.json`, JSON.stringify([]), "utf8");
|
||||
const imageBasePath = path_1.default.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}`);
|
||||
if (!fs_1.default.existsSync(imageBasePath)) {
|
||||
fs_1.default.mkdirSync(imageBasePath, { recursive: true });
|
||||
}
|
||||
let imagePath = path_1.default.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile.jpg`);
|
||||
let imageThumbnailPath = path_1.default.join(STATIC_ROOT, `images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile-thumbnail.jpg`);
|
||||
let prodImageUrl = imagePath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
|
||||
let prodImageThumbnailUrl = imageThumbnailPath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
|
||||
fs_1.default.copyFileSync(path_1.default.join(ROOT_DIR, "/public/images/user-preset.png"), imagePath);
|
||||
fs_1.default.copyFileSync(path_1.default.join(ROOT_DIR, "/public/images/user-preset-thumbnail.png"), imageThumbnailPath);
|
||||
(0, child_process_1.execSync)(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
const updateImages = yield (0, updateDbEntry_1.default)({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.payload.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
},
|
||||
});
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs_1.default.unlinkSync(path_1.default.resolve(process.cwd(), tmpDir));
|
||||
}
|
||||
catch (error) { }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `Error Creating User`, error);
|
||||
return false;
|
||||
}
|
||||
catch (error) {
|
||||
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `Error Creating User`, error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
|
||||
+65
-49
@@ -1,59 +1,75 @@
|
||||
import path from "path";
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const path_1 = __importDefault(require("path"));
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
import fs from "fs";
|
||||
import EJSON from "../../../utils/ejson";
|
||||
import hashPassword from "../../../functions/dsql/hashPassword";
|
||||
import updateDbEntry from "../../../functions/backend/db/updateDbEntry";
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const ejson_1 = __importDefault(require("../../../utils/ejson"));
|
||||
const hashPassword_1 = __importDefault(require("../../../functions/dsql/hashPassword"));
|
||||
const updateDbEntry_1 = __importDefault(require("../../../functions/backend/db/updateDbEntry"));
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
var _a;
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir === null || tmpDir === void 0 ? void 0 : tmpDir.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./update-user.json");
|
||||
const updateUserObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
if (typeof updateUserObj !== "object" || Array.isArray(updateUserObj))
|
||||
throw new Error("Update User Object Invalid!");
|
||||
let hashedPassword = updateUserObj.password
|
||||
? hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: updateUserObj.password,
|
||||
})
|
||||
: undefined;
|
||||
let updatePayload = Object.assign({}, updateUserObj);
|
||||
if (hashedPassword) {
|
||||
updatePayload["password"] = hashedPassword;
|
||||
}
|
||||
const newUser = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: Object.assign(Object.assign({}, updatePayload), { id: undefined }),
|
||||
identifierColumnName: "id",
|
||||
identifierValue: updatePayload.id,
|
||||
});
|
||||
if (!(newUser === null || newUser === void 0 ? void 0 : newUser.affectedRows))
|
||||
return false;
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
function createUser() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir === null || tmpDir === void 0 ? void 0 : tmpDir.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path_1.default.resolve(process.cwd(), tmpDir)
|
||||
: path_1.default.resolve(__dirname, "./update-user.json");
|
||||
const updateUserObj = ejson_1.default.parse(fs_1.default.readFileSync(targetPath, "utf-8"));
|
||||
if (typeof updateUserObj !== "object" || Array.isArray(updateUserObj))
|
||||
throw new Error("Update User Object Invalid!");
|
||||
let hashedPassword = updateUserObj.password
|
||||
? (0, hashPassword_1.default)({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: updateUserObj.password,
|
||||
})
|
||||
: undefined;
|
||||
let updatePayload = Object.assign({}, updateUserObj);
|
||||
if (hashedPassword) {
|
||||
updatePayload["password"] = hashedPassword;
|
||||
}
|
||||
catch (error) { }
|
||||
const newUser = yield (0, updateDbEntry_1.default)({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: Object.assign(Object.assign({}, updatePayload), { id: undefined }),
|
||||
identifierColumnName: "id",
|
||||
identifierValue: updatePayload.id,
|
||||
});
|
||||
if (!(newUser === null || newUser === void 0 ? void 0 : newUser.affectedRows))
|
||||
return false;
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs_1.default.unlinkSync(path_1.default.resolve(process.cwd(), tmpDir));
|
||||
}
|
||||
catch (error) { }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Updating User`, error);
|
||||
return false;
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Updating User`, error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
|
||||
Reference in New Issue
Block a user