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

16 lines
7.9 KiB
JavaScript
Raw Normal View History

2024-12-05 07:03:33 +00:00
"use strict";(()=>{var e={};e.id=979,e.ids=[979],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},9288:e=>{e.exports=require("sharp")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},93851:(e,r,t)=>{t.r(r),t.d(r,{config:()=>x,default:()=>b,routeModule:()=>E});var a={};t.r(a),t.d(a,{config:()=>y,default:()=>$});var i=t(89947),l=t(2706),s=t(96762),n=t(47670),o=t.n(n),u=t(10701),d=t.n(u),m=t(48227),c=t.n(m),p=t(77538),_=t.n(p),f=t(6420),h=t.n(f);let g=t(29021),S=t(33873),{execSync:v}=t(79646),y={api:{responseLimit:"1mb",bodyParser:{sizeLimit:"200mb"}}};async function $(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await h()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let a=e.body,{media:i}=a;console.log(a.type);let l=a.folder,s=a.type;"production".match(/production/);let n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),r.json({success:!1,msg:"No Static File Path"});if(l){let e=S.join(n,`images/user-images/user-${t.id}/${l}`);g.existsSync(e)||g.mkdirSync(e,{recursive:!0});let r=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/media/${l}`;g.existsSync(r)||g.mkdirSync(r,{recursive:!0})}try{let e=[];for(let r=0;r<i.length;r++){let a=i[r];if(s?.match(/image/i)){let{imageBase64:r,imageName:i,imageSize:s,mimeType:n}=a,u=await d()({imageName:i.replace(/ /g,"-"),imageSourceBase64:r,user:t,folder:l,mimeType:n,isPrivate:a.private});if(!u){console.log("Image Write Failed!");continue}let{urlPath:m,urlThumbnailPath:c,urlRelativePath:p,urlThumbnailRelativePath:_}=u;await o()({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:i.replace(/ /g,"-"),media_url:m,media_type:"image",media_thumbnail_url:c,media_path:p,media_thumbnail_path:_,folder:l,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:m}),e.push({media_name:i.replace(/ /g,"-"),media_url:m,media_thumbnail_url:c})}else if(s?.match(/file/i)){let{fileBase64:r,fileSize:i,fileType:s}=a,n=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=c()({isPrivate:a.private,user:t,folder:l});if(!u)continue;let{fileRootPath:d,urlRootPath:m,relativePath:p}=u,_=s?.match(/pdf/)?".pdf":s?.match(/xlsx/)?".xlsx":s?.match(/csv/)?".csv":s?.match(/json/)?".json":null;if(!_)continue;let f=d+n+_,h=m+n+_,S=p+n+_;g.writeFileSync(f,r,"base64"),await o()({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:n,media_url:h,media_thumbnail_url:h,media_path:S,media_thumbnail_path:S,media_type:"file/"+_.replace(/\./,""),folder:l,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:h}),e.push({media_name:n,media_url:h,media_thumbnail_url:h})}else if(s?.match(/video/i)){let{fileBase64:r,fileSize:i,fileType:s}=a,n=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=c()({isPrivate:a.private,user:t,folder:l,video:!0});if(!u)continue;let{fileRootPath:d,urlRootPath:m,relativePath:p}=u,_=s?.match(/mp4/)?".mp4":null;if(!_)continue;let f=d+n+_,h=m+n+_,S=p+n+_;g.writeFileSync(f,r,"base64"),await o()({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:n,media_url:h,media_thumbnail_url:h,media_path:S,media_thumbnail_path:S,media_type:"video/"+_.replace(/\./,""),folder:l,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:h}),e.push({media_name:n,media_url:h,media_thumbnail_url:h})}}r.json({success:!0,mediaData:e})}catch(e){console.log(`Add User Media Error: ${e.message}`),_()({component:"/api/addUserMedia/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let b=(0,s.M)(a,"default"),x=(0,s.M)(a,"config"),E=new i.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/addUserMedia",pathna
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(i+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),l?.url&&(i+=`
URL: ${l.url}`),l?.body&&(i+=`
Request Body: ${JSON.stringify(l.body,null,4)}`),n&&(i+=`
IP: ${n}`),i+=`
Date: ${s.toDateString()}
========================================`,a.existsSync("./.tmp/error.log")||a.writeFileSync("./.tmp/error.log","","utf-8");let o=a.readFileSync("./.tmp/error.log","utf-8");a.writeFileSync("./.tmp/error.log",i),a.appendFileSync("./.tmp/error.log",`
2024-11-05 11:12:42 +00:00
2024-12-05 07:03:33 +00:00
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,r,t)=>{t(81630);let a=t(51348),i=t(72907),l=t(29021);async function s(e,r,t,s){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let n=i(e.cookies.datasquirelAuthKey);if(!n)return null;let o=JSON.parse(n);if(!o.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${o.csrf_k}`)))return null;let u=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!u)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!l.existsSync(`${u}/${o.csrf_k}`))return null;if(0==o.verification_status&&!t){let e=await a(`SELECT verification_status FROM users WHERE id='${o.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return o?.date&&Date.now()-o.date>6048e5?null:o}e.exports=s},72907:(e,r,t)=>{let{scryptSync:a,createDecipheriv:i}=t(55511),{Buffer:l}=t(79428);e.exports=e=>{let r=i("aes-192-cbc",a(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),l.alloc(16,0));try{let t=r.update(e,"hex","utf8");return t+=r.final("utf8")}catch(e){return null}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[4050],()=>t(93851));module.exports=a})();