dsql-admin/dsql-app/.local_dist/server/pages/api/updateUser.js

329 lines
14 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
"use strict";
(() => {
var exports = {};
exports.id = 4196;
exports.ids = [4196];
exports.modules = {
/***/ 4809:
/***/ ((module) => {
module.exports = require("node-fetch");
/***/ }),
/***/ 5184:
/***/ ((module) => {
module.exports = require("nodemailer");
/***/ }),
/***/ 6109:
/***/ ((module) => {
module.exports = require("sanitize-html");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 7441:
/***/ ((module) => {
module.exports = require("sharp");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 2081:
/***/ ((module) => {
module.exports = require("child_process");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 4142:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
const nodeFetch = __webpack_require__(4809);
const sharp = __webpack_require__(7441);
const serverError = __webpack_require__(2163);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
*
* @param {object} params
* @param {string} params.imageSourceUrl
* @param {string} params.imageWritePath
* @param {number} [params.maxSize]
* @param {string} [params.sourceType]
*/ module.exports = async function fsWriteImageToDisk({ imageSourceUrl , imageWritePath , maxSize , sourceType , }) {
try {
async function generateBuffer() {
let imageBuffer;
if (sourceType?.match("url")) {
if (imageSourceUrl.match(/^http/i)) {
let fetchedImage = await nodeFetch(imageSourceUrl, {});
imageBuffer = await fetchedImage.buffer();
} else {
let readPath = `./public${imageSourceUrl}`;
imageBuffer = readPath;
}
} else if (sourceType?.match("base64")) {
imageBuffer = Buffer.from(imageSourceUrl, "base64");
}
return imageBuffer;
}
const buffer = await generateBuffer();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
let imagePath = imageWritePath;
const MAX_SIZE = maxSize ? maxSize : 650;
// const sharpImage = sharp(imagePath);
const sharpImageRaw = sharp(buffer);
// let imageMetadata = await sharpImage.metadata();
let imageMetadataRaw = await sharpImageRaw.metadata();
let { width , height , format } = imageMetadataRaw;
if (width && height && width > MAX_SIZE) {
let resizeRatio = MAX_SIZE / width;
sharpImageRaw.resize(MAX_SIZE, Math.round(height * resizeRatio), {
fit: "cover"
});
}
if (format) {
sharpImageRaw.toFormat(format, {
quality: 80
});
}
await sharpImageRaw.toFile(imagePath);
// console.log(image);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
// console.log("====================================");
// console.log("Complete!!!");
// console.log("====================================");
} catch (/** @type {any} */ error) {
serverError({
component: "functions/backend/fsWriteImageToDisk",
message: error.message
});
}
}; /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */
/***/ }),
/***/ 9662:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2224);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(7547);
/* harmony import */ var _package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_encrypt__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_fsWriteImageToDisk__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4142);
/* harmony import */ var _functions_backend_fsWriteImageToDisk__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_fsWriteImageToDisk__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _functions_backend_handleNodemailer__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6926);
/* harmony import */ var _functions_backend_handleNodemailer__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_handleNodemailer__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_6__);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(2081);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_7___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_7__);
/* harmony import */ var _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(5886);
/* harmony import */ var _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_8___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_8__);
// @ts-check
const fs = __webpack_require__(7147);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* API handler
* ==============================================================================
* @type {import("next").NextApiHandler}
*/ async function handler(req, res) {
/**
* Check method
*
* @description Check request method and return if invalid
*/ if (req.method !== "POST") return res.json({
user: null,
msg: "Registration Failed!"
});
/**
* User auth
*
* @description Authenticate user
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_5___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* Validate Form
*
* @description Check if request body is valid
*/ const sanitizedReqBody = req.body;
const { first_name , last_name , image } = sanitizedReqBody;
/**
* Validate Form
*
* @description Check if request body is valid
*/ try {
////////////////////////////////////////
const existingImage = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default()(`SELECT image FROM users WHERE id='${user.id}'`);
let isImageExisting = existingImage[0].image === image;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
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 imagePath = path__WEBPACK_IMPORTED_MODULE_6___default().join(STATIC_ROOT, `images/user-images/user-${user.id}/user-${user.id}-profile.jpg`);
let imageThumbnailPath = path__WEBPACK_IMPORTED_MODULE_6___default().join(STATIC_ROOT, `images/user-images/user-${user.id}/user-${user.id}-profile-thumbnail.jpg`);
if (!isImageExisting) {
let writeImage = await _functions_backend_fsWriteImageToDisk__WEBPACK_IMPORTED_MODULE_2___default()({
imageSourceUrl: typeof image === "string" ? image : image.imageBase64,
imageWritePath: imagePath,
sourceType: typeof image === "string" ? "url" : "base64"
});
let writeImageThumbnail = await _functions_backend_fsWriteImageToDisk__WEBPACK_IMPORTED_MODULE_2___default()({
imageSourceUrl: typeof image === "string" ? image : image.imageBase64,
imageWritePath: imageThumbnailPath,
maxSize: 250,
sourceType: typeof image === "string" ? "url" : "base64"
});
(0,child_process__WEBPACK_IMPORTED_MODULE_7__.execSync)(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
let imageUrl = imagePath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
let imageThumbnailUrl = imageThumbnailPath.replace(STATIC_ROOT, process.env.DSQL_STATIC_HOST || "");
const updatedUser = await _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_8___default()({
dbFullName: "datasquirel",
tableName: "users",
identifierColumnName: "id",
identifierValue: user.id,
data: {
first_name,
last_name,
image: imageUrl,
image_thumbnail: imageThumbnailUrl
}
});
const updatedUserUser = await _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_8___default()({
dbFullName: "datasquirel",
tableName: "user_users",
identifierColumnName: "invited_user_id",
identifierValue: user.id,
data: {
first_name,
last_name,
image: imageUrl,
image_thumbnail: imageThumbnailUrl
}
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
res.json({
user: true
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4___default()({
component: "/api/updateUser/main-catch-error",
message: error.message,
user: user
});
res.json({
user: null
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [5425,2224,2163,6825,3017,3403,7547,5886,6926], () => (__webpack_exec__(9662)));
module.exports = __webpack_exports__;
})();