UNPKG

flexbiz-server

Version:

Flexible Server

58 lines (57 loc) 29.7 kB
const fs=require("fs"),appModel=global.getModel("app"),listinfo=global.getModel("listinfo"),Token=global.getModel("token"),express=require("express"),underscore=require("underscore"),passport=require("passport"),path=require("path"),async=require("async"),sharp=require("sharp"),docs=require("./libs/docs"),{isValidObjectId}=require("mongoose"),permission=require("./libs/permission"),{sendMessageZalo}=require("./libs/utils"),{getTmpFile}=require("./libs/excelHelper"),controllerRPT=require("./controllers/controllerRPT"); module.exports=async function($app$$,$cb$$=()=>{},$limiter$jscomp$2_limiter$$=!1){var $createRateLimiterMiddleware_limiter_others$$=global.createRateLimiterMiddleware;const $router$$=global.routerAPI=express.Router();if($limiter$jscomp$2_limiter$$!==!0){configs.trust_proxy&&$app$$.enable("trust proxy",!0);require("./auths/google")($app$$,passport);require("./auths/facebook")($app$$,passport);require("./auths/local")($app$$,passport);require("./auths/bearer")(passport);$router$$.use(async function($req$$, $access_token_listinfo_code_res$$,$next$$){for(let $key$$ in $req$$.query)$req$$.query[$key$$]=="true"&&($req$$.query[$key$$]=!0),$req$$.query[$key$$]=="false"&&($req$$.query[$key$$]=!1);($access_token_listinfo_code_res$$=$req$$.query.access_token)||($access_token_listinfo_code_res$$=$req$$.get("X-Access-Token")||$req$$.get("access-token"));$access_token_listinfo_code_res$$&&($req$$.query.access_token=$access_token_listinfo_code_res$$,Token.updateOne({token:$access_token_listinfo_code_res$$,used:{$ne:!0}}, {used:!0}).catch($err$$=>Logger.error("Token update error:",$err$$)));($access_token_listinfo_code_res$$=$req$$.get("listinfo-code")||$req$$.query["listinfo-code"])&&($req$$.query["listinfo-code"]=$access_token_listinfo_code_res$$);$next$$()});var $authenticate_dynamic_list_limiter_limiter$$=require("./auths/authMiddleware");$router$$.use($authenticate_dynamic_list_limiter_limiter$$);$router$$.param("id_app",async function($req$$,$res$$,$next$$,$id_app$$){let $_clientIp$$=(($req$$.headers["x-forwarded-for"]|| "").split(",").pop()||$req$$.connection.remoteAddress||$req$$.socket.remoteAddress||$req$$.connection.socket.remoteAddress).split(":");if($id_app$$){if($req$$.user.token_id_app&&$req$$.user.token_id_app!=$id_app$$)return $res$$.status(400).send(`Token kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb cho c\u00f4ng ty '${$id_app$$}'`);if(mongoose.Types.ObjectId.isValid($id_app$$))try{const $appInfo$$=await appModel.findById($id_app$$).lean();if($appInfo$$)$req$$.user.current_id_app=$id_app$$,$appInfo$$.options|| ($appInfo$$.options={}),$req$$.user.current_app_info=$appInfo$$,$req$$.query.id_app=$id_app$$,$req$$.user.clientIp=$_clientIp$$[$_clientIp$$.length-1],$req$$.user.appAdmin=await new Promise($rs$$=>{permission.isAdmin($id_app$$,$req$$.user.email,function($e$$,$admin$$){$rs$$($admin$$)})}),$req$$.user.current_app_info.appAdmin=$req$$.user.appAdmin,$next$$();else return $res$$.status(400).send(`ID c\u00f4ng ty '${$id_app$$}' kh\u00f4ng t\u1ed3n t\u1ea1i`)}catch($error$$){$next$$($error$$)}else return $res$$.status(400).send("id_app:'"+ $id_app$$+"' kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb")}else $req$$.user&&($req$$.user.current_id_app=void 0,$req$$.user.appAdmin=void 0,$req$$.user.current_app_info={},$req$$.user.clientIp=$_clientIp$$[$_clientIp$$.length-1],$next$$())});configs.limitRequest.api&&($authenticate_dynamic_list_limiter_limiter$$=await $createRateLimiterMiddleware_limiter_others$$(configs.limitRequest.api,"api"),$router$$.use($authenticate_dynamic_list_limiter_limiter$$));$app$$.use("/api",$router$$)}$router$$.post(["/dispatch-report/:id_app/:code", "/dispatch-report/:code"],($req$$,$queryString_res$$,$next$$)=>{const {id_app:$id_app$$,code:$code$$}=$req$$.params;$queryString_res$$=$req$$.url.includes("?")?$req$$.url.substring($req$$.url.indexOf("?")):"";global.controllers?.[$code$$.trim().toUpperCase()]?$req$$.url=$id_app$$?`/search/${$id_app$$}/${$code$$}${$queryString_res$$}`:`/search/${$code$$}${$queryString_res$$}`:(global.report_controllers?.[$code$$.toUpperCase()]||(Logger.warn("[route] T\u1ea1o customize report...",$code$$),new controllerRPT($router$$, $code$$,async function($req$$,$callback$$){$callback$$(null,[])})),$req$$.url=$id_app$$?`/${$id_app$$}/${$code$$}${$queryString_res$$}`:`/${$code$$}${$queryString_res$$}`);Logger.info(`[dispatch-report] ${$req$$.method} ${$req$$.originalUrl} ---> ${$req$$.url}`);$next$$()});const $funcs_inited$$={};configs.paths.modules&&fs.existsSync(configs.paths.modules+"/lists")&&fs.readdirSync(configs.paths.modules+"/lists").forEach(function($file$$){$file$$.endsWith(".js")&&(require(configs.paths.modules+ "/lists/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});fs.existsSync(__dirname+"/modules/lists")&&fs.readdirSync(__dirname+"/modules/lists").filter($file$$=>!$funcs_inited$$[$file$$]).forEach(function($file$$){$file$$.endsWith(".js")&&(require("./modules/lists/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});configs.paths.modules&&fs.existsSync(configs.paths.modules+"/vouchers")&&fs.readdirSync(configs.paths.modules+"/vouchers").forEach(function($file$$){$file$$.endsWith(".js")&& (require(configs.paths.modules+"/vouchers/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});fs.existsSync(__dirname+"/modules/vouchers")&&fs.readdirSync(__dirname+"/modules/vouchers").filter($file$$=>!$funcs_inited$$[$file$$]).forEach(function($file$$){$file$$.endsWith(".js")&&(require("./modules/vouchers/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});configs.paths.modules&&fs.existsSync(configs.paths.modules+"/reports")&&fs.readdirSync(configs.paths.modules+"/reports").forEach(function($file$$){$file$$.endsWith(".js")&& (require(configs.paths.modules+"/reports/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});fs.existsSync(__dirname+"/modules/reports")&&fs.readdirSync(__dirname+"/modules/reports").filter($file$$=>!$funcs_inited$$[$file$$]).forEach(function($file$$){$file$$.endsWith(".js")&&(require("./modules/reports/"+$file$$)($router$$),$funcs_inited$$[$file$$]=$file$$)});const $sys_router$$=express.Router();$sys_router$$.use(passport.authenticate("bearer",{session:!1}));configs.limitRequest.sys&&($authenticate_dynamic_list_limiter_limiter$$= await $createRateLimiterMiddleware_limiter_others$$(configs.limitRequest.sys,"sys"),$sys_router$$.use($authenticate_dynamic_list_limiter_limiter$$));$app$$.use("/api",$sys_router$$);const $redisCache$$=require("./libs/redis-cache");$sys_router$$.route("/uploadfile").post(function($req$$,$res$$){const $access_token$$=$req$$.query.access_token;let $folder$$=$req$$.query.folder||"";const $id_app$$=$req$$.query.id_app;if(!$req$$.files.fileupload)if($req$$.files.file)$req$$.files.fileupload=$req$$.files.file; else return $res$$.status(400).send({error:"File kh\u00f4ng t\u1ed3n t\u1ea1i"});const $path$$=require("path");if(!$req$$.files.fileupload.path)return $res$$.status(400).send({error:"File kh\u00f4ng t\u1ed3n t\u1ea1i"});let $ext$$=$path$$.extname($req$$.files.fileupload.path);if($ext$$){if($ext$$=$ext$$.toLowerCase(),$ext$$===".exe")return $res$$.status(400).send({error:"Ki\u1ec3u file n\u00e0y kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ea5p nh\u1eadn"})}else return $res$$.status(400).send({error:"Ki\u1ec3u file n\u00e0y kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ea5p nh\u1eadn"}); const $ip$$=$req$$.ip||$req$$.headers["x-forwarded-for"]||$req$$.socket.remoteAddress;global.getModel("user").findByToken($access_token$$,$ip$$,async($error$$,$_user$$)=>{if($error$$)return $res$$.status(400).send({error:$error$$.message||$error$$.error||$error$$});if($_user$$){const $user$$=await global.getModel("user").findOne({email:$_user$$.email});fs.readFile($req$$.files.fileupload.path,async function($err$$,$data$$){if($err$$)return $res$$.status(400).send({error:$err$$.message||$err$$.error|| $err$$});var $originalname_p$$=$req$$.files.fileupload.originalname.split(" ").join("_");$err$$=configs.paths.images||$path$$.dirname($path$$.dirname(__dirname))+"/images";$err$$+="/";fs.existsSync($err$$+$folder$$)||fs.mkdirSync($err$$+$folder$$);$id_app$$&&$folder$$!="avatar"&&($folder$$=$folder$$+"/"+$id_app$$,fs.existsSync($err$$+$folder$$)||fs.mkdirSync($err$$+$folder$$));let $newPath$$=$folder$$+"/"+$user$$._id.toString()+"_"+(new Date).getTime().toString()+"_"+$originalname_p$$,$path_image$$; if($ext$$!==".png"&&$ext$$!==".jpeg"&&$ext$$!==".jpg"&&$ext$$!==".gif"&&$ext$$!==".webp")$path_image$$=$err$$+$newPath$$,fs.writeFile($path_image$$,$data$$,function($err$$){if($err$$)return $res$$.status(400).send({error:$err$$.message||$err$$.error||$err$$});fs.unlink($req$$.files.fileupload.path,$e$$=>{$e$$&&Logger.error($e$$)});$req$$.query.json?$res$$.send({fileUrl:"/getfile/"+$newPath$$}):($res$$.writeHead(200,{"Content-Type":"text/html"}),$res$$.end("<html><head><title>/getfile/"+$newPath$$+ "</title></head><body>success</body></html>"))});else{$ext$$!==".webp"&&($newPath$$+=".webp");$path_image$$=$err$$+$newPath$$;$originalname_p$$=$originalname_p$$.split(".");var $thumbPath$$=$newPath$$+".thumb."+$originalname_p$$[$originalname_p$$.length-1],$path_image_thumb$$=$err$$+$thumbPath$$,$_gm$$=sharp($req$$.files.fileupload.path,{failOnError:!1});($err$$=await $_gm$$.metadata())&&$err$$.format!="webp"&&($thumbPath$$+=".webp",$path_image_thumb$$+=".webp",$_gm$$=$_gm$$.webp({lossless:!1})); async.parallel([function($callback$$){setImmediate(async()=>{let $orgin_file_size$$=await $_gm$$.metadata();$orgin_file_size$$&&$orgin_file_size$$.width>4096?$_gm$$.resize({width:4096}).rotate().toFile($path_image$$,async function($error$$){$error$$?(Logger.error("save resize image:",$error$$.message,". Ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd s\u1eed d\u1ee5ng file g\u1ed1c\n",$req$$.header("user-agent")),fs.writeFile($path_image$$,$data$$,$e$$=>{if($e$$)return $callback$$($error$$);$callback$$()})): (Logger.info("resize image",$path_image$$),$callback$$())}):fs.writeFile($path_image$$,$data$$,function($err$$){$callback$$($err$$)})})}],function($err$$){if($err$$)return $res$$.status(400).send({error:$err$$.message||$err$$.error||$err$$});$_gm$$.resize({width:350}).rotate().toFile($path_image_thumb$$,async function($error$$){if($error$$){Logger.error("Kh\u00f4ng th\u1ec3 t\u1ea1o thumbnail cho h\u00ecnh \u1ea3nh n\u00e0y\n"+$error$$,$req$$.files.fileupload.path,". Ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd s\u1eed d\u1ee5ng file g\u1ed1c l\u00e0m thubnail\n", $req$$.header("user-agent"));try{await new Promise(($res$$,$rej$$)=>{fs.writeFile($path_image_thumb$$,$data$$,$e$$=>{if($e$$)return $rej$$($e$$);$res$$()})})}catch($error$$){return $res$$.status(400).send({error:"Kh\u00f4ng th\u1ec3 t\u1ea1o thumbnail cho h\u00ecnh \u1ea3nh n\u00e0y. L\u1ed7i: "+($error$$.message||$error$$.error||$error$$)})}}fs.unlink($req$$.files.fileupload.path,$e$$=>{$e$$&&Logger.error($e$$)});let $path_avatar$$="/getfile/"+$newPath$$,$path_thumb$$="/getfile/"+$thumbPath$$;$folder$$== "avatars"?($user$$.local.picture=$path_avatar$$,$user$$.picture=$user$$.local.picture,$user$$.save(function($error$$){if($error$$)return $res$$.status(400).send($error$$);$redisCache$$.set("user",$user$$.toObject(),function($e$$){$e$$?Logger.error($e$$):Logger.info("cache user infomation to redis");$res$$.send("\u0110\u00e3 c\u1eadp nh\u1eadt th\u00e0nh c\u00f4ng")});$req$$.query.json?$res$$.send({image:$path_avatar$$,thumb:$path_thumb$$,fileUrl:$path_avatar$$}):($res$$.writeHead(200,{"Content-Type":"text/html"}), $res$$.end("<html><head><title>"+$path_avatar$$+"</title></head><body>success</body></html>"))})):$req$$.query.json?$res$$.send({image:$path_avatar$$,thumb:$path_thumb$$,fileUrl:$path_avatar$$}):($res$$.writeHead(200,{"Content-Type":"text/html"}),$res$$.end("<html><head><title>"+$path_avatar$$+"</title></head><body>success</body></html>"))})})}})}else $res$$.status(404).send({error:"Not found"})})});$sys_router$$.route("/uploadexcel").post(function($req$$,$res$$){let $access_token$$=$req$$.query.access_token; if(!$req$$.files.fileupload)if($req$$.files.file)$req$$.files.fileupload=$req$$.files.file;else{$res$$.status(400).send({error:"File kh\u00f4ng t\u1ed3n t\u1ea1i"});return}var $path$$=require("path"),$ext$jscomp$1_ip$$=$path$$.extname($req$$.files.fileupload.path);if($ext$jscomp$1_ip$$)if($ext$jscomp$1_ip$$=$ext$jscomp$1_ip$$.toLowerCase(),$ext$jscomp$1_ip$$!=".xlsx"&&$ext$jscomp$1_ip$$!=".xml")$res$$.status(400).send("Ch\u1ec9 ch\u1ea5p nh\u1eadn c\u00e1c \u0111\u1ecbnh d\u1ea1ng file: xlsx,xml"); else{var $root_dir_templates$$=configs.paths.templates||$path$$.dirname($path$$.dirname(__dirname))+"/templates";$root_dir_templates$$+="/";$ext$jscomp$1_ip$$=$req$$.ip||$req$$.headers["x-forwarded-for"]||$req$$.connection.remoteAddress;global.getModel("user").findByToken($access_token$$,$ext$jscomp$1_ip$$,function($error$$,$user$$){if($error$$)return $res$$.status(400).send({error:$error$$.message||$error$$.error||$error$$});$user$$?fs.readFile($req$$.files.fileupload.path,function($err$$,$data$$){if($err$$)return $res$$($err$$); fs.existsSync($root_dir_templates$$+"excels")||fs.mkdirSync($root_dir_templates$$+"excels");let $newPath$$="excels/"+$user$$._id.toString()+"_"+$path$$.basename($req$$.files.fileupload.path);fs.unlink($req$$.files.fileupload.path,$e$$=>{$e$$&&Logger.error($e$$)});Logger.info("uploading template file",$root_dir_templates$$+$newPath$$);fs.writeFile($root_dir_templates$$+$newPath$$,$data$$,function($error$$){if($error$$)return Logger.error("Error upload file",$error$$),$res$$.status(404).send($error$$); $error$$="/templates/"+$newPath$$;Logger.info("Updated a file to ",$root_dir_templates$$+$newPath$$);if($req$$.query.json)return $res$$.send({fileUrl:$error$$});$res$$.writeHead(200,{"Content-Type":"text/html"});$res$$.end("<html><head><title>"+$error$$+"</title></head><body>success</body></html>")})}):$res$$.status(404).send({error:"Not found"})})}else $res$$.status(400).send("Ch\u1ec9 ch\u1ea5p nh\u1eadn c\u00e1c \u0111\u1ecbnh d\u1ea1ng file: xlsx,xml")});let $funcs_sys_inited$$={};configs.paths.modules&& fs.existsSync(configs.paths.modules+"/systems")&&fs.readdirSync(configs.paths.modules+"/systems").forEach(function($file$$){$file$$.endsWith(".js")&&(require(configs.paths.modules+"/systems/"+$file$$)($sys_router$$),$funcs_sys_inited$$[$file$$]=$file$$)});fs.readdirSync(__dirname+"/modules/systems").filter($file$$=>!$funcs_sys_inited$$[$file$$]).forEach(function($file$$){$file$$.endsWith(".js")&&(require("./modules/systems/"+$file$$)($sys_router$$),$funcs_sys_inited$$[$file$$]=$file$$)});$authenticate_dynamic_list_limiter_limiter$$= await listinfo.find({status:!0,create_model:!0}).lean();Logger.info("Creating dynamic APIs...",$authenticate_dynamic_list_limiter_limiter$$.length);$authenticate_dynamic_list_limiter_limiter$$.forEach($r$$=>{listinfo.createController(global.routerAPI,$r$$)});if($limiter$jscomp$2_limiter$$!==!0){const {isSupperAdmin:$isSupperAdmin$$}=require("./libs/utils"),{evalute:$evalute$$}=require("./libs/utils"),$usersAdmin$$=configs.admins;$router$$.route("/:id_app/query/:model/:action").get(function($req$$, $res$$,$next$$){let $user$$=$req$$.user,$id_app$$=$req$$.params.id_app,$modelName$$=$req$$.params.model.trim().toLowerCase(),$action$$=$req$$.params.action,$model$$=global.getModel($modelName$$);if(($action$$!=="find"||!underscore.has($model$$.schema.paths,"id_app")||"user token otp tokens app asskey".split(" ").indexOf($modelName$$)>=0)&&!underscore.contains($usersAdmin$$,$user$$.email)&&!$isSupperAdmin$$($user$$.email.toLowerCase()))return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n th\u1ef1c hi\u1ec7n thao t\u00e1c n\u00e0y"); permission.isAdmin($id_app$$,$user$$.email,function($e$$,$admin$$){if($e$$||!$admin$$)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n th\u1ef1c hi\u1ec7n thao t\u00e1c n\u00e0y");$e$$={};if($req$$.query.q)try{$e$$=$evalute$$(`return ${$req$$.query.q}`,{})}catch($e$$){return $res$$.status(400).send($e$$.message)}try{switch(underscore.has($model$$.schema.paths,"id_app")&&($e$$.id_app=$id_app$$),$action$$){case "find":let $limit$$=$req$$.query.limit||1E3,$fields$$;$req$$.query.fields&&($fields$$= {},$req$$.query.fields.split(",").forEach($field$$=>{$fields$$[$field$$]=1}));try{$limit$$=Number($limit$$)}catch($e$$){return $res$$.status(400).send($e$$.message)}$limit$$||=1E3;$model$$.find($e$$,$fields$$).limit($limit$$).lean().then($rs$$=>{$res$$.send($rs$$)}).catch($e$$=>{$res$$.status(400).send($e$$)});break;case "update":let $data$$=$req$$.query.fields;if(!$data$$)return $res$$.status(400).send("Data to update is empty");try{$data$$=JSON.parse($data$$),$model$$.updateMany($e$$,$data$$,($e$$, $rs$$)=>{if($e$$)return $res$$.status(400).send($e$$);$res$$.send($rs$$)})}catch($e$$){$res$$.status(400).send($e$$.message)}break;case "remove":$model$$.deleteMany($e$$,($e$$,$rs$$)=>{if($e$$)return $res$$.status(400).send($e$$);$res$$.send($rs$$)});break;default:$res$$.status(400).send(`Action ${$action$$} is not support`)}}catch($e$$){$res$$.status(400).send($e$$.message)}})});const $bot_router$$=express.Router();configs.limitRequest.bot&&($limiter$jscomp$2_limiter$$=await $createRateLimiterMiddleware_limiter_others$$(configs.limitRequest.bot, "bot"),$router$$.use($limiter$jscomp$2_limiter$$));$app$$.use("/bot",$bot_router$$);fs.existsSync(__dirname+"/modules/bot")&&(Logger.info("init bot..."),fs.readdirSync(__dirname+"/modules/bot").forEach(function($file$$){$file$$.endsWith(".js")&&require("./modules/bot/"+$file$$)($bot_router$$)}));const $public_router$$=express.Router();configs.limitRequest.public&&($limiter$jscomp$2_limiter$$=await $createRateLimiterMiddleware_limiter_others$$(configs.limitRequest.public,"public"),$router$$.use($limiter$jscomp$2_limiter$$)); $public_router$$.use(function($req$$,$res$$,$next$$){for(let $key$$ in $req$$.query)$req$$.query[$key$$]=="true"&&($req$$.query[$key$$]=!0),$req$$.query[$key$$]=="false"&&($req$$.query[$key$$]=!1);if($req$$.query.id_app){let $id_app$$=$req$$.query.id_app;appModel.findOne({_id:$id_app$$},function($error$$,$app$$){if($error$$)return $next$$($error$$);if($app$$)$req$$.user={current_id_app:$id_app$$,email:"public"},$req$$.query.id_app=$id_app$$,$next$$();else return $res$$.status(400).send("Kh\u00f4ng t\u1ed3n t\u1ea1i c\u00f4ng ty n\u00e0y")})}else $next$$()}); $app$$.use("/public",$public_router$$);const $funcs_public_inited$$={};configs.paths.modules&&fs.existsSync(configs.paths.modules+"/public")&&(Logger.info("init custom public..."),fs.readdirSync(configs.paths.modules+"/public").forEach(function($file$$){$file$$.endsWith(".js")&&(require(configs.paths.modules+"/public/"+$file$$)($public_router$$),$funcs_public_inited$$[$file$$]=$file$$)}));fs.existsSync(__dirname+"/modules/public")&&(Logger.info("init default public..."),fs.readdirSync(__dirname+"/modules/public").filter($file$$=> !$funcs_public_inited$$[$file$$]).forEach(function($file$$){$file$$.endsWith(".js")&&(require("./modules/public/"+$file$$)($public_router$$),$funcs_public_inited$$[$file$$]=$file$$)}));configs.useFlow&&require("./flow/FlowEngine")($app$$);$createRateLimiterMiddleware_limiter_others$$=await $createRateLimiterMiddleware_limiter_others$$(configs.limitRequest.other||{},"other");$app$$.get("/downloads/temp/:fileName",$createRateLimiterMiddleware_limiter_others$$,async function($fileName_req$$,$res$$){$fileName_req$$= $fileName_req$$.params.fileName;try{const $fileBuffer$$=await getTmpFile($fileName_req$$);if(!$fileBuffer$$)return Logger.error("[route][download temp file] Kh\u00f4ng t\u00ecm th\u1ea5y file",$fileName_req$$),$res$$.status(404).send('\n <div style="font-family: Arial; text-align: center; margin-top: 50px;">\n <h2 style="color: red;">\u0110\u01b0\u1eddng link \u0111\u00e3 h\u1ebft h\u1ea1n ho\u1eb7c kh\u00f4ng t\u1ed3n t\u1ea1i!</h2>\n <p>V\u00ec l\u00fd do b\u1ea3o m\u1eadt, link t\u1ea3i file ch\u1ec9 c\u00f3 hi\u1ec7u l\u1ef1c trong v\u00f2ng 1 ph\u00fat.</p>\n <p>Vui l\u00f2ng quay l\u1ea1i ph\u1ea7n m\u1ec1m v\u00e0 th\u1ef1c hi\u1ec7n xu\u1ea5t file l\u1ea7n n\u1eefa.</p>\n </div>\n '); $fileName_req$$=$fileName_req$$.replace(/[\r\n]+/g,"").replace(/"/g,'\\"');const $asciiFileName$$=$fileName_req$$.replace(/[^\x20-\x7E]/g,"_"),$encodedFileName$$=encodeURIComponent($fileName_req$$);if($fileName_req$$.endsWith(".json"))return $res$$.setHeader("Content-Type","application/json; charset=utf-8"),$res$$.setHeader("Content-Disposition",`attachment; filename="${$asciiFileName$$}"; filename*=UTF-8''${$encodedFileName$$}`),$res$$.send($fileBuffer$$);$res$$.setHeader("Content-Disposition",`attachment; filename="${$fileName_req$$}"; filename*=UTF-8''${$encodedFileName$$}`); $res$$.type($fileName_req$$);return $res$$.send($fileBuffer$$)}catch($e$$){Logger.error("[download tmp data]",$e$$,$fileName_req$$),$res$$.status(400).send($e$$.message)}});$app$$.get("/getfile/:folder/:filename",$createRateLimiterMiddleware_limiter_others$$,async function($orgin_file_size$jscomp$2_req$$,$res$$){let $root_dir$$=configs.paths.images||path.join(__dirname,"images"),$imgPath$$=path.join($root_dir$$,$orgin_file_size$jscomp$2_req$$.params.folder,$orgin_file_size$jscomp$2_req$$.params.filename); if(fs.existsSync($imgPath$$)){let $size$$,$cache$$;if($orgin_file_size$jscomp$2_req$$.query.size||$orgin_file_size$jscomp$2_req$$.query.resize)switch(String($orgin_file_size$jscomp$2_req$$.query.size||$orgin_file_size$jscomp$2_req$$.query.resize).toUpperCase()){case "64":case "X":$size$$=64;$cache$$=!0;break;case "128":$size$$=128;$cache$$=!0;break;case "320":case "S":$size$$=320;$cache$$=!0;break;case "512":case "M":$size$$=512;$cache$$=!0;break;case "1024":case "L":$size$$=1024;$cache$$=!0;break; default:try{$size$$=Number($orgin_file_size$jscomp$2_req$$.query.size||$orgin_file_size$jscomp$2_req$$.query.resize);if(isNaN($size$$)||!$size$$)$size$$=1024;$cache$$=!0}catch($e$$){Logger.info("size param must be X or S or M or L or a number")}}if($size$$){let $file_name_resize$$=path.join($root_dir$$,$orgin_file_size$jscomp$2_req$$.params.folder,String($size$$)+"."+$orgin_file_size$jscomp$2_req$$.params.filename);if(fs.existsSync($file_name_resize$$))return sharp($file_name_resize$$,{failOnError:!1}).pipe($res$$); let $_gm$$=sharp($imgPath$$,{failOnError:!1});($orgin_file_size$jscomp$2_req$$=await $_gm$$.metadata())&&$size$$&&$size$$<$orgin_file_size$jscomp$2_req$$.width&&$size$$<$orgin_file_size$jscomp$2_req$$.height&&($_gm$$=$_gm$$.resize({width:$size$$}).rotate());$orgin_file_size$jscomp$2_req$$&&$orgin_file_size$jscomp$2_req$$.format!="webp"&&($_gm$$=$_gm$$.webp({lossless:!1}));$cache$$&&setImmediate(()=>{$_gm$$.toFile($file_name_resize$$,$error$$=>{if($error$$)return Logger.error("save resize image:", $error$$.message);Logger.info("cache file",$file_name_resize$$)})});$_gm$$.pipe($res$$)}else $res$$.sendFile($imgPath$$)}else $res$$.sendFile(path.join(__dirname,"images","others","noimage.png"))});const $Labelinfo$$=global.getModel("labelinfo");$app$$.get("/labels/:labelid",$createRateLimiterMiddleware_limiter_others$$,function($labelid_req$$,$res$$){$labelid_req$$=$labelid_req$$.params.labelid.toUpperCase();$Labelinfo$$.find({labelid:$labelid_req$$}).lean().then($labels$$=>{let $_labels$$={};$labels$$.forEach($l$$=> {$_labels$$[$l$$.textid]=$l$$});$res$$.send($_labels$$)}).catch($e$$=>{$res$$.status(400).send($e$$)})});$app$$.get("/aff/:_id",$createRateLimiterMiddleware_limiter_others$$,async function($id_afflink_req$$,$res$$){$id_afflink_req$$=$id_afflink_req$$.params._id;if(!$id_afflink_req$$)return $res$$.status(404).send("Page not found");var $afflink_page_link$$=await global.getModel("afflink").findOne({_id:$id_afflink_req$$});$afflink_page_link$$?(await global.getModel("afflink").updateOne({_id:$id_afflink_req$$}, {$inc:{da_truy_cap:1}}),$afflink_page_link$$=$afflink_page_link$$.page_link,$afflink_page_link$$=$afflink_page_link$$.indexOf("?")<0?`${$afflink_page_link$$}?afflink=${$id_afflink_req$$}`:`${$afflink_page_link$$}&afflink=${$id_afflink_req$$}`,$res$$.redirect($afflink_page_link$$)):$res$$.status(404).send("Page not found")});const $axios$$=require("axios"),$querystring$$=require("querystring");$app$$.post("/oa/sendMessage",$createRateLimiterMiddleware_limiter_others$$,async function($data$jscomp$3_req$$, $res$$){$data$jscomp$3_req$$=$data$jscomp$3_req$$.body||{};if(!($data$jscomp$3_req$$.secret_key&&$data$jscomp$3_req$$.id_app&&$data$jscomp$3_req$$.template_data&&$data$jscomp$3_req$$.template_id&&$data$jscomp$3_req$$.phone))return $res$$.status(400).send({error:"Kh\u00f4ng th\u1ec3 g\u1eedi Zalo OTP"});try{let $rs$$=await sendMessageZalo($data$jscomp$3_req$$.id_app,$data$jscomp$3_req$$.phone,$data$jscomp$3_req$$.template_id,$data$jscomp$3_req$$.template_data,null,$data$jscomp$3_req$$.secret_key); $res$$.send($rs$$)}catch($e$$){$res$$.status(400).send({error:$e$$.error||$e$$.message||$e$$})}});$app$$.get("/oa/:id_app",$createRateLimiterMiddleware_limiter_others$$,async function($data$jscomp$4_req$$,$res$$){var $app$jscomp$2_id_app$$=$data$jscomp$4_req$$.params.id_app,$headers_zalo_oa_id$$=$data$jscomp$4_req$$.query.oa_id;$data$jscomp$4_req$$=$data$jscomp$4_req$$.query.code;if(!$headers_zalo_oa_id$$||!$data$jscomp$4_req$$||!isValidObjectId($app$jscomp$2_id_app$$))return $res$$.status(400).send({error:"Y\u00eau c\u1ea7u kh\u00f4ng h\u1ee3p l\u1ec7"}); $app$jscomp$2_id_app$$=await global.getModel("app").findOne({_id:$app$jscomp$2_id_app$$});if(!$app$jscomp$2_id_app$$)return $res$$.status(400).send({error:"App kh\u00f4ng t\u1ed3n t\u1ea1i"});$app$jscomp$2_id_app$$.zalo_oa_id=$headers_zalo_oa_id$$;$app$jscomp$2_id_app$$.zalo_code=$data$jscomp$4_req$$;if($app$jscomp$2_id_app$$.zalo_secret_key&&$app$jscomp$2_id_app$$.zalo_app_id){$headers_zalo_oa_id$$={"Content-Type":"application/x-www-form-urlencoded",secret_key:$app$jscomp$2_id_app$$.zalo_secret_key}; $data$jscomp$4_req$$={code:$app$jscomp$2_id_app$$.zalo_code,app_id:$app$jscomp$2_id_app$$.zalo_app_id,grant_type:"authorization_code"};try{var $message$$=(await $axios$$.post("https://oauth.zaloapp.com/v4/oa/access_token",$querystring$$.stringify($data$jscomp$4_req$$),{headers:$headers_zalo_oa_id$$})).data;$app$jscomp$2_id_app$$.zalo_access_token=$message$$.access_token;$app$jscomp$2_id_app$$.zalo_refresh_token=$message$$.refresh_token;$app$jscomp$2_id_app$$.zalo_expires_in=$message$$.expires_in; $app$jscomp$2_id_app$$.zalo_updated=new Date}catch($e$$){return $message$$=(($e$$.response||{}).data||{}).message,Logger.error("Can't get access token from zalo",$message$$||$e$$),$res$$.status(400).send({error:$message$$||$e$$.message||$e$$})}}else return $res$$.status(400).send({error:"Ch\u01b0a khai b\u00e1o th\u00f4ng tin zalo app id v\u00e0 secret key cho c\u00f4ng ty n\u00e0y"});await $app$jscomp$2_id_app$$.save();$res$$.send({ok:!0})});$app$$.get("/task-status/:id",$createRateLimiterMiddleware_limiter_others$$, async function($req$$,$res$$){try{let $task$$=await $redisCache$$.getObject($req$$.params.id);return $res$$.send($task$$)}catch($e$$){$res$$.status(400).send($e$$)}});$app$$.get("/api-docs-auth",$createRateLimiterMiddleware_limiter_others$$,async function($document_req$$,$res$$){$document_req$$=await docs.apiAuth();return $res$$.send($document_req$$)});$app$$.get("/api-docs-fields/:code",$createRateLimiterMiddleware_limiter_others$$,async function($document$jscomp$1_req$$,$res$$){$document$jscomp$1_req$$= await docs.apiFields($document$jscomp$1_req$$.params.code,$document$jscomp$1_req$$.query.id_app,$document$jscomp$1_req$$.query.import_yn);return $res$$.send($document$jscomp$1_req$$)});$app$$.get("/api-docs-input/:code",$createRateLimiterMiddleware_limiter_others$$,async function($document$jscomp$2_req$$,$res$$){$document$jscomp$2_req$$=await docs.apiDocs($document$jscomp$2_req$$.params.code,$document$jscomp$2_req$$.query.id_app);return $res$$.send($document$jscomp$2_req$$)});$app$$.get("/api-docs-report/:code", $createRateLimiterMiddleware_limiter_others$$,async function($document$jscomp$3_req$$,$res$$){$document$jscomp$3_req$$=await docs.apiReport($document$jscomp$3_req$$.params.code,$document$jscomp$3_req$$.query.id_app);return $res$$.send($document$jscomp$3_req$$)});if(configs.has_api_barcode){const $qr$$=require("qr-image");$app$$.get("/qrcode",$createRateLimiterMiddleware_limiter_others$$,function($req$$,$res$$){let $report$$=$req$$.query.report,$type$$=$req$$.query.type||"svg",$size$$=5;if($req$$.query.size)try{$size$$= Number($req$$.query.size),isNaN($size$$)&&($size$$=void 0)}catch($e$$){Logger.error("size qrcode is not valid",$e$$)}else $size$$=5;$report$$||($report$$=$req$$.query.data)&&($report$$=Buffer.from($report$$,"base64").toString("utf8"));if(!$report$$)return $res$$.status(400).send("miss parameter 'report'");setImmediate(()=>{const $code$$=$qr$$.image($report$$,{type:$type$$,size:$size$$});$res$$.type($type$$);$code$$.pipe($res$$)})});const $bwipjs$$=require("bwip-js"),$stream$$=require("stream");$app$$.get("/barcode", $createRateLimiterMiddleware_limiter_others$$,function($height_req$$,$res$$){let $code$$=$height_req$$.query.code||"code128",$data$$=$height_req$$.query.data;$height_req$$=Number($height_req$$.query.height||10);if(!$data$$)return $res$$.status(400).send({error:"Thi\u1ebfu c\u00e1c tham s\u1ed1 data"});$bwipjs$$.toBuffer({bcid:$code$$,text:$data$$,scale:3,height:$height_req$$,includetext:!0,textxalign:"center"},function($bufferStream_e$$,$png$$){$bufferStream_e$$?(Logger.error($bufferStream_e$$,$code$$, $data$$),$res$$.status(400).send({error:$bufferStream_e$$.message||$bufferStream_e$$.error||$bufferStream_e$$})):($bufferStream_e$$=new $stream$$.PassThrough,$bufferStream_e$$.end($png$$),$bufferStream_e$$.pipe($res$$))})})}}$cb$$()};