dsql-admin/dsql-app/.local_dist/server/pages/api/media/addPrivateMedia.js

263 lines
10 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
"use strict";
(() => {
var exports = {};
exports.id = 8373;
exports.ids = [8373];
exports.modules = {
/***/ 9538:
/***/ ((module) => {
module.exports = require("datasquirel");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 9643:
/***/ ((__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 _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_2__);
// @ts-check
/**
* Imports
* ==============================================================================
*/ const fs = __webpack_require__(7147);
const datasquirel = __webpack_require__(9538);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* addPrivateMedia API handler
* ==============================================================================
* @type {import("next").NextApiHandler} - Next API handler
*/ async function handler(req, res) {
/**
* Check method
* =========================================
* @description Check if the request method is correct
*/ if (req.method !== "POST") {
return res.json({
success: false,
msg: "Failed!"
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* User auth
* =========================================
* @description Authenticate user
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Grab request data
* =========================================
* @description Grab data from the request
* @type {any}
*/ const requestBody = datasquirel.sanitizeSql(req.body, null);
if (requestBody.media_url?.match(/\.\./) || requestBody.folder?.match(/\.\./) || requestBody.id?.toString()?.match(/\.\./)) {
return res.json({
success: false,
msg: "Error!",
error: "Invalid url"
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Send Response
* =========================================
* @description Send a response to the client
*/ try {
/**
* Handle request logic here
*/ const isPrivate = requestBody?.private == 1 ? true : false;
const mediaMatchedPath = isPrivate ? requestBody?.media_path?.replace(/\@\/media/i, "") || requestBody?.media_url.replace(/\@\/media/i, "") : requestBody?.media_path?.match(/\/user-images\/user-.*/) || requestBody?.media_url?.match(/\/user-images\/user-.*/);
/** @type {string} */ const folder = requestBody.folder ? `/${requestBody.folder}` : "";
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!");
}
const publicRootPath = path__WEBPACK_IMPORTED_MODULE_2___default().join(STATIC_ROOT, `images`);
const publicUrlPathRoot = `${process.env.DSQL_STATIC_HOST}/images/user-images/user-${user.id}`;
const publicRelativePathRoot = `/images/user-images/user-${user.id}`;
2024-11-05 14:18:40 +00:00
const mediaRelPathRoot = isPrivate ? `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/media` : publicRootPath;
2024-11-05 11:12:42 +00:00
const mediaRelPath = mediaRelPathRoot + mediaMatchedPath;
2024-11-05 14:18:40 +00:00
const destinationPath = isPrivate ? `${publicRootPath}/user-images/user-${user.id}` : `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/media`;
2024-11-05 11:12:42 +00:00
if (!fs.existsSync(destinationPath)) {
try {
fs.mkdirSync(destinationPath, {
recursive: true
});
} catch (/** @type {any} */ error) {
console.log("LINE 88 error:", error.message);
}
}
if (folder) {
folder.split("/").reduce((prev, curr)=>{
const currFolder = prev?.match(/./) ? `${prev}/${curr}` : destinationPath;
7;
if (!fs.existsSync(currFolder)) {
fs.mkdirSync(currFolder, {
recursive: true
});
}
return currFolder;
}, "");
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* construct new file name
*/ const fileNameWithExtension = mediaRelPath?.match(/[^\/]+$/)?.[0];
if (!fileNameWithExtension) {
throw new Error("No File Name Detected!");
}
const fileName = fileNameWithExtension.split(".")[0];
const fileExtension = fileNameWithExtension.split(".")[1];
const newFileNameInDb = isPrivate ? `${publicUrlPathRoot}${folder ? folder + "/" : "/"}${fileNameWithExtension}` : `@/media${folder ? folder + "/" : "/"}${fileNameWithExtension}`;
const newRelativeFileNameInDb = isPrivate ? `${publicRelativePathRoot}${folder ? folder + "/" : "/"}${fileNameWithExtension}` : `@/media${folder ? folder + "/" : "/"}${fileNameWithExtension}`;
// if (isPrivate) {
// const dstPathFinal = destinationPath + folder + "/" + fileNameWithExtension;
// fs.copyFileSync(dstPathFinal, mediaRelPath);
// fs.unlinkSync(dstPathFinal);
// } else {
// }
fs.copyFileSync(mediaRelPath, destinationPath + folder + "/" + fileNameWithExtension);
fs.unlinkSync(mediaRelPath);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Construct new File Thumbnail name
*/ const thumbnailNameWithExtension = fileName + "_thumbnail." + fileExtension;
const thumbnailSourcePath = mediaRelPath.replace(fileNameWithExtension, thumbnailNameWithExtension);
const newFileThumbnailNameInDb = isPrivate ? `${publicUrlPathRoot}${folder ? folder + "/" : "/"}${thumbnailNameWithExtension}` : `@/media${folder ? folder + "/" : "/"}${thumbnailNameWithExtension}`;
const newRelativeThumbnailNameInDb = isPrivate ? `${publicRelativePathRoot}${folder ? folder + "/" : "/"}${thumbnailNameWithExtension}` : `@/media${folder ? folder + "/" : "/"}${thumbnailNameWithExtension}`;
// if (isPrivate) {
// const dstPathFinal = destinationPath + folder + "/" + thumbnailNameWithExtension;
// fs.copyFileSync(dstPathFinal, thumbnailSourcePath);
// fs.unlinkSync(dstPathFinal);
// } else {
// }
try {
fs.copyFileSync(thumbnailSourcePath, destinationPath + folder + "/" + thumbnailNameWithExtension);
fs.unlinkSync(thumbnailSourcePath);
} catch (error1) {}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const updateDb = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default()(`UPDATE user_media SET private = ?, media_url= ?, media_thumbnail_url= ?, media_path = ?, media_thumbnail_path = ? WHERE id = ?`, [
isPrivate ? 0 : 1,
newFileNameInDb,
newFileThumbnailNameInDb,
newRelativeFileNameInDb,
newRelativeThumbnailNameInDb,
requestBody.id,
]);
/**
* Response data
*/ res.json({
success: true,
msg: "Success!"
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error2) {
/**
* Handle error
* =========================================
* @description Handle errors that occur during
* the request
*/ console.log(error2.message);
res.json({
success: false,
msg: "Error!",
error: error2
});
}
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
2024-11-05 14:18:40 +00:00
var __webpack_exports__ = __webpack_require__.X(0, [2224,6825], () => (__webpack_exec__(9643)));
2024-11-05 11:12:42 +00:00
module.exports = __webpack_exports__;
})();