dsql-admin/dsql-app/.local_dist/server/pages/api/addUserMedia.js
Benjamin Toby 748ff55092 Bug Fixes
2024-11-05 15:18:40 +01:00

355 lines
14 KiB
JavaScript

"use strict";
(() => {
var exports = {};
exports.id = 3909;
exports.ids = [3909];
exports.modules = {
/***/ 6517:
/***/ ((module) => {
module.exports = require("lodash");
/***/ }),
/***/ 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");
/***/ }),
/***/ 9146:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "config": () => (/* binding */ config),
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5338);
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_fsWriteImageToDiskFromBase64__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5910);
/* harmony import */ var _functions_backend_fsWriteImageToDiskFromBase64__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_fsWriteImageToDiskFromBase64__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_grabPaths__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6715);
/* harmony import */ var _functions_backend_grabPaths__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_grabPaths__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_4__);
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/ const fs = __webpack_require__(7147);
const path = __webpack_require__(1017);
const { execSync } = __webpack_require__(2081);
const config = {
api: {
responseLimit: "1mb",
bodyParser: {
sizeLimit: "200mb"
}
}
};
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* 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({
msg: "Failed!"
});
/**
* User auth
*
* @description Authenticate user
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_4___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* User auth
*
* @description Authenticate user
*/ const sanitizedReqBody = req.body;
const { media } = sanitizedReqBody;
const folder = sanitizedReqBody.folder;
const type = sanitizedReqBody.type;
const newMediaArray = media;
const isProduction = "production".match(/production/);
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
if (!STATIC_ROOT) {
console.log("Static File ENV not Found!");
return res.json({
success: false,
msg: "No Static File Path"
});
}
if (folder) {
const folderPath = path.join(STATIC_ROOT, `images/user-images/user-${user.id}/${folder}`);
const folderExists = fs.existsSync(folderPath);
if (!folderExists) {
fs.mkdirSync(folderPath, {
recursive: true
});
}
const privateFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/media/${folder}`;
const privateFolderExists = fs.existsSync(privateFolderPath);
if (!privateFolderExists) {
fs.mkdirSync(privateFolderPath, {
recursive: true
});
}
}
/**
* Send Response
*
* @description Send a boolean response
*/ try {
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/ const mediaData = [];
for(let i = 0; i < newMediaArray.length; i++){
const newMedia = newMediaArray[i];
if (type?.match(/image/i)) {
const { imageBase64 , imageName , imageSize , mimeType } = newMedia;
const imageWrite = await _functions_backend_fsWriteImageToDiskFromBase64__WEBPACK_IMPORTED_MODULE_1___default()({
imageName: imageName.replace(/ /g, "-"),
imageSourceBase64: imageBase64,
user: user,
folder,
mimeType,
isPrivate: newMedia.private
});
if (!imageWrite) {
console.log("Image Write Failed!");
continue;
}
const { urlPath , urlThumbnailPath , urlRelativePath , urlThumbnailRelativePath , } = imageWrite;
let newMediaEntry = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0___default()({
dbFullName: "datasquirel",
tableName: "user_media",
data: {
user_id: user.id,
media_name: imageName.replace(/ /g, "-"),
media_url: urlPath,
media_type: "image",
media_thumbnail_url: urlThumbnailPath,
media_path: urlRelativePath,
media_thumbnail_path: urlThumbnailRelativePath,
folder: folder,
private: newMedia.private ? 1 : 0
},
duplicateColumnName: "media_url",
duplicateColumnValue: urlPath
});
// if (newMediaEntry?.insertId) {
mediaData.push({
media_name: imageName.replace(/ /g, "-"),
media_url: urlPath,
media_thumbnail_url: urlThumbnailPath
});
// }
} else if (type?.match(/file/i)) {
const { fileBase64 , fileSize , fileType } = newMedia;
const fileName = newMedia.fileName.replace(/ /g, "-").replace(/\./g, "");
/**
* Grab URL Paths
*/ const grabedPaths = _functions_backend_grabPaths__WEBPACK_IMPORTED_MODULE_2___default()({
isPrivate: newMedia.private,
user: user,
folder: folder
});
if (!grabedPaths) {
continue;
}
const { fileRootPath , urlRootPath , relativePath } = grabedPaths;
const fileExt = (()=>{
if (fileType?.match(/pdf/)) return ".pdf";
if (fileType?.match(/xlsx/)) return ".xlsx";
if (fileType?.match(/csv/)) return ".csv";
if (fileType?.match(/json/)) return ".json";
return null;
})();
if (!fileExt) continue;
const writePath = fileRootPath + fileName + fileExt;
const urlPath1 = urlRootPath + fileName + fileExt;
const urlRelativePath1 = relativePath + fileName + fileExt;
fs.writeFileSync(writePath, fileBase64, "base64");
let newMediaEntry1 = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0___default()({
dbFullName: "datasquirel",
tableName: "user_media",
data: {
user_id: user.id,
media_name: fileName,
media_url: urlPath1,
media_thumbnail_url: urlPath1,
media_path: urlRelativePath1,
media_thumbnail_path: urlRelativePath1,
media_type: "file" + "/" + fileExt.replace(/\./, ""),
folder: folder,
private: newMedia.private ? 1 : 0
},
duplicateColumnName: "media_url",
duplicateColumnValue: urlPath1
});
mediaData.push({
media_name: fileName,
media_url: urlPath1,
media_thumbnail_url: urlPath1
});
} else if (type?.match(/video/i)) {
const { fileBase64: fileBase641 , fileSize: fileSize1 , fileType: fileType1 } = newMedia;
const fileName1 = newMedia.fileName.replace(/ /g, "-").replace(/\./g, "");
/**
* Grab URL Paths
*/ const grabedPaths1 = _functions_backend_grabPaths__WEBPACK_IMPORTED_MODULE_2___default()({
isPrivate: newMedia.private,
user: user,
folder: folder,
video: true
});
if (!grabedPaths1) {
continue;
}
const { fileRootPath: fileRootPath1 , urlRootPath: urlRootPath1 , relativePath: relativePath1 } = grabedPaths1;
const fileExt1 = (()=>{
if (fileType1?.match(/mp4/)) return ".mp4";
return null;
})();
if (!fileExt1) continue;
const writePath1 = fileRootPath1 + fileName1 + fileExt1;
const urlPath2 = urlRootPath1 + fileName1 + fileExt1;
const urlRelativePath2 = relativePath1 + fileName1 + fileExt1;
fs.writeFileSync(writePath1, fileBase641, "base64");
let newMediaEntry2 = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_0___default()({
dbFullName: "datasquirel",
tableName: "user_media",
data: {
user_id: user.id,
media_name: fileName1,
media_url: urlPath2,
media_thumbnail_url: urlPath2,
media_path: urlRelativePath2,
media_thumbnail_path: urlRelativePath2,
media_type: "video" + "/" + fileExt1.replace(/\./, ""),
folder: folder,
private: newMedia.private ? 1 : 0
},
duplicateColumnName: "media_url",
duplicateColumnValue: urlPath2
});
mediaData.push({
media_name: fileName1,
media_url: urlPath2,
media_thumbnail_url: urlPath2
});
}
}
////////////////////////////////////////
res.json({
success: true,
mediaData: mediaData
});
////////////////////////////////////////
} catch (/** @type {any} */ error) {
console.log(`Add User Media Error: ${error.message}`);
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3___default()({
component: "/api/addUserMedia/catch-error",
message: error.message,
user: user
});
res.json({
success: false,
msg: "Database Files could not be written!"
});
////////////////////////////////////////
}
}
/***/ })
};
;
// 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, [2224,2163,6825,3017,3403,7547,5886,5338,6715,5910], () => (__webpack_exec__(9146)));
module.exports = __webpack_exports__;
})();