dsql-admin/dsql-app/.local_dist/server/pages/api/addUserMedia.js
Benjamin Toby bc037e839f Updates
2024-12-15 12:27:16 +01:00

16 lines
9.0 KiB
JavaScript

"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:()=>b,default:()=>$,routeModule:()=>N});var a={};t.r(a),t.d(a,{config:()=>E,default:()=>y});var i=t(89947),s=t(2706),l=t(96762),n=t(47670),o=t.n(n),u=t(10701),d=t.n(u),c=t(48227),m=t.n(c),_=t(48225),p=t.n(_),f=t(6420),h=t.n(f);let S=t(29021),g=t(33873),{execSync:v}=t(79646),E={api:{responseLimit:"1mb",bodyParser:{sizeLimit:"200mb"}}};async function y(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,s=a.folder,l=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(s){let e=g.join(n,`images/user-images/user-${t.id}/${s}`);S.existsSync(e)||S.mkdirSync(e,{recursive:!0});let r=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/media/${s}`;S.existsSync(r)||S.mkdirSync(r,{recursive:!0})}try{let e=[];for(let r=0;r<i.length;r++){let a=i[r];if(l?.match(/image/i)){let{imageBase64:r,imageName:i,imageSize:l,mimeType:n}=a,u=await d()({imageName:i.replace(/ /g,"-").replace(/\.{1,}|\//g,""),imageSourceBase64:r,userId:t.id,folder:s,mimeType:n,isPrivate:a.private});if(!u){console.log("Image Write Failed!");continue}let{urlPath:c,urlThumbnailPath:m,urlRelativePath:_,urlThumbnailRelativePath:p}=u;await o()({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:i.replace(/ /g,"-"),media_url:c,media_type:"image",media_thumbnail_url:m,media_path:_,media_thumbnail_path:p,folder:s,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:c}),e.push({media_name:i.replace(/ /g,"-"),media_url:c,media_thumbnail_url:m})}else if(l?.match(/file/i)){let{fileBase64:r,fileSize:i,fileType:l}=a,n=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=m()({isPrivate:a.private,userId:t.id,folder:s});if(!u)continue;let{fileRootPath:d,urlRootPath:c,relativePath:_}=u,p=l?.match(/pdf/)?".pdf":l?.match(/xlsx/)?".xlsx":l?.match(/csv/)?".csv":l?.match(/json/)?".json":null;if(!p)continue;let f=d+n+p,h=c+n+p,g=_+n+p;S.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:g,media_thumbnail_path:g,media_type:"file/"+p.replace(/\./,""),folder:s,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:h}),e.push({media_name:n,media_url:h,media_thumbnail_url:h})}else if(l?.match(/video/i)){let{fileBase64:r,fileSize:i,fileType:l}=a,n=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=m()({isPrivate:a.private,userId:t.id,folder:s,video:!0});if(!u)continue;let{fileRootPath:d,urlRootPath:c,relativePath:_}=u,p=l?.match(/mp4/)?".mp4":null;if(!p)continue;let f=d+n+p,h=c+n+p,g=_+n+p;S.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:g,media_thumbnail_path:g,media_type:"video/"+p.replace(/\./,""),folder:s,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}`),p()({component:"/api/addUserMedia/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let $=(0,l.M)(a,"default"),b=(0,l.M)(a,"config"),N=new i.PagesAPIRouteModule({definition:{kind:s.A.PAGES_API,page:"/api/addUserMedia",pathname:"/api/addUserMedia",bundlePath:"",filename:""},userland:a})},10701:(e,r,t)=>{let a=t(9288),i=t(48225),s=t(48227);e.exports=async function({imageSourceBase64:e,imageName:r,userId:t,mimeType:l,thumbnailSize:n,folder:o,isPrivate:u}){try{let i=Buffer.from(e,"base64"),d=n?parseInt(n.toString()):400,c=a(i),m=a(i),_=s({isPrivate:u,userId:t,folder:o});if(!_)return null;let{fileRootPath:p,urlRootPath:f,relativePath:h}=_,{width:S,height:g,format:v}=await c.metadata(),E=l||v;S&&g&&S>1800&&c.resize(1800,Math.round(1800/S*g),{fit:"cover"}),c.toFormat(E,{quality:80}),await c.metadata();let y=`${r}.${E}`;if(await c.toFile(p+y),S&&g&&S>d){let e=d/S;m.resize(d,Math.round(g*e),{fit:"cover"})}else S&&g&&m.resize(150,Math.round(150/S*g),{fit:"cover"});m.toFormat(E,{quality:80});let $=`${r}_thumbnail.${E}`;return await m.toFile(p+$),{urlPath:f+y,urlThumbnailPath:f+$,urlRelativePath:h+y,urlThumbnailRelativePath:h+$}}catch(e){return console.log("Write Image to Disk error =>",e.message),i({component:"functions/backend/fsWriteImageToDiskFromBase64",message:e.message}),null}}},48227:(e,r,t)=>{let a=t(29021),i=t(33873);e.exports=function({isPrivate:e,userId:r,folder:t,video:s,pathOnly:l}){"production".match(/production/);let n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),null;let o=e?`@/${s?"videos":"media"}/${t?t+"/":""}`:s?`/videos/user-videos/user-${r}/${t?t+"/":""}`:`/images/user-images/user-${r}/${t?t+"/":""}`,u=e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r}/media/${t?t+"/":""}`:i.join(n,o);return a.existsSync(u)||l||a.mkdirSync(u,{recursive:!0}),{fileRootPath:u,urlRootPath:e?`@/media/${t?t+"/":""}`:`${process.env.DSQL_STATIC_HOST}${o}`,relativePath:o}}},6420:(e,r,t)=>{t(81630);let a=t(51348),i=t(72835),s=t(29021),l=t(31341);async function n(e,r,t,n){let{keyCookieName:o,csrfCookieName:u}=l();if(!e.cookies?.[o]?.match(/./))return null;let d=i({encryptedString:e.cookies[o]});if(!d)return null;let c=JSON.parse(d);if(!c.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${c.csrf_k}`)))return null;let m=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!m)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!s.existsSync(`${m}/${c.csrf_k}`))return null;if(0==c.verification_status&&!t){let e=await a(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}e.exports=n},31341:e=>{e.exports=function(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",i=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",s=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),l=r;e?.userId&&(l+=`user_${e.userId}_`),s&&(l+=`${s}_`),l+=t;let n=r;e?.userId&&(n+=`user_${e.userId}_`),s&&(n+=`${s}_`),n+=a;let o=r;return e?.userId&&(o+=`user_${e.userId}_`),s&&(o+=`${s}_`),{keyCookieName:l,csrfCookieName:n,oneTimeCodeName:o+=i}}},48225:(e,r,t)=>{let a=t(29021),{IncomingMessage:i}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:i,req:s}){let l=new Date,n=(()=>{if(!s)return null;try{let e=s.headers["x-forwarded-for"],r=s.headers["x-real-ip"],t=s.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],i=t||a||r||s.socket.remoteAddress;if(!i)return null;return String(i)}catch(e){return null}})();try{let i=`🚀 SERVER ERROR ===========================
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}`),s?.url&&(i+=`
URL: ${s.url}`),s?.body&&(i+=`
Request Body: ${JSON.stringify(s.body,null,4)}`),n&&(i+=`
IP: ${n}`),i+=`
Date: ${l.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",`
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},72835:(e,r,t)=>{let{scryptSync:a,createDecipheriv:i}=t(55511),{Buffer:s}=t(79428);e.exports=({encryptedString:e,encryptionKey:r,encryptionSalt:t})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let l=r||process.env.DSQL_ENCRYPTION_PASSWORD,n=t||process.env.DSQL_ENCRYPTION_SALT,o=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!l?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!n?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=i("aes-192-cbc",a(l,n,o),s.alloc(16,0));try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};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})();