"use strict";(()=>{var e={};e.id=538,e.ids=[538],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")},41848:(e,r,t)=>{t.r(r),t.d(r,{config:()=>v,default:()=>y,routeModule:()=>R});var a={};t.r(a),t.d(a,{default:()=>h});var s=t(89947),o=t(2706),l=t(96762);t(82703),t(10701);var i=t(77538),n=t.n(i),d=t(6420),u=t.n(d),c=t(27825),m=t.n(c),p=t(51348),f=t.n(p),b=t(27444);t(39831),t(81630),t(77538);let _=t(51348);async function S({tbls:e,user:r,query:t}){let a;if(!t?.delegated)return{isPriviledged:!0,rootUserId:r.id};try{for(let t=0;te.dbFullName==n),c=i.find(e=>e.dbFullName==d),p=u?.tables.find(e=>e.tableName==o.table),_=c?.tables.find(e=>e.tableName==s.table);if(!p||!_)throw Error("Operation Failed! Tables Schemas not present.");if(!m().isEqual(_.fields,p.fields))throw Error("Tables incompatible");switch(a){case"Push":let E=await f()(`SELECT * FROM ${d}.${s.table}`),h=await (0,b.A)({dbName:o.db,paradigm:"import",payload:E,tableName:o.table,userId:l.rootUserId,payloadType:"object"});return await $({dbName:o.db,tableName:o.table,data:E,userId:l.rootUserId}),r.json({success:!0,payload:h});case"Pull":let y=await f()(`SELECT * FROM ${n}.${o.table}`),v=await (0,b.A)({dbName:s.db,paradigm:"import",payload:y,tableName:s.table,userId:l.rootUserId,payloadType:"object"});return await $({dbName:s.db,tableName:s.table,data:y,userId:l.rootUserId}),r.json({success:!0,payload:v})}r.json({success:!1})}catch(e){console.log(e.message),n()({component:"/api/sync-table-data",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}async function $({dbName:e,tableName:r,data:t,userId:a}){let s=`${process.env.DSQL_USER_DB_PREFIX}${a}_${e.replace(/ /g,"")}`,o=t.map(e=>e.id),l=`DELETE FROM ${s}.${r}`;o?.[0]&&(l+=` WHERE id NOT IN (${o.join(",")})`),await f()(l)}let y=(0,l.M)(a,"default"),v=(0,l.M)(a,"config"),R=new s.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/sync-table-data",pathname:"/api/sync-table-data",bundlePath:"",filename:""},userland:a})},10701:(e,r,t)=>{let a=t(9288),s=t(77538),o=t(48227);e.exports=async function({imageSourceBase64:e,imageName:r,user:t,mimeType:l,thumbnailSize:i,folder:n,isPrivate:d}){try{let s=Buffer.from(e,"base64"),u=i?parseInt(i.toString()):400,c=a(s),m=a(s),p=o({isPrivate:d,user:t,folder:n});if(!p)return null;let{fileRootPath:f,urlRootPath:b,relativePath:_}=p,{width:S,height:g,format:E}=await c.metadata(),h=l||E;S&&g&&S>1800&&c.resize(1800,Math.round(1800/S*g),{fit:"cover"}),c.toFormat(h,{quality:80}),await c.metadata();let $=`${r}.${h}`;if(await c.toFile(f+$),S&&g&&S>u){let e=u/S;m.resize(u,Math.round(g*e),{fit:"cover"})}else S&&g&&m.resize(150,Math.round(150/S*g),{fit:"cover"});m.toFormat(h,{quality:80});let y=`${r}_thumbnail.${h}`;return await m.toFile(f+y),{urlPath:b+$,urlThumbnailPath:b+y,urlRelativePath:_+$,urlThumbnailRelativePath:_+y}}catch(e){return console.log("Write Image to Disk error =>",e.message),s({component:"functions/backend/fsWriteImageToDiskFromBase64",message:e.message}),null}}},39831:(e,r,t)=>{t(81630);let a=t(72907);e.exports=async function({request:e,databaseSlug:r,user:t,query:s}){try{let o=t.id,l=null;if(!s?.delegated)return{dbUserId:o};let i=s.dbUserId,n=`${process.env.DSQL_USER_DB_PREFIX}${i}_${r}`,d=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${n}`;try{if(!e.cookies?.[d])throw Error("Cookie not present");let t=a(e.cookies[d]);if(!t)throw Error("Invalid Token");if((l=JSON.parse(t)).databaseSlug===r)return{dbUserId:o=l.rootUserId,rootUserId:l.rootUserId,rootUserName:l.rootUserName,rootUserEmail:l.rootUserEmail,rootUserImage:l.rootUserImage,databaseFullName:l.databaseFullName,databaseSlug:l.databaseSlug,allowedTables:l.allowedTables,priviledges:l.priviledges,database:l.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:o}}catch(e){return null}}},48227:(e,r,t)=>{let a=t(29021),s=t(33873);e.exports=function({isPrivate:e,user:r,folder:t,video:o,pathOnly:l}){"production".match(/production/);let i=r?.id||r?.user_id,n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),null;let d=e?`@/${o?"videos":"media"}/${t?t+"/":""}`:o?`/videos/user-videos/user-${i}/${t?t+"/":""}`:`/images/user-images/user-${i}/${t?t+"/":""}`,u=e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${i}/media/${t?t+"/":""}`:s.join(n,d);return a.existsSync(u)||l||a.mkdirSync(u,{recursive:!0}),{fileRootPath:u,urlRootPath:e?`@/media/${t?t+"/":""}`:`${process.env.DSQL_STATIC_HOST}${d}`,relativePath:d}}},49827:(e,r,t)=>{t.r(r),t.d(r,{default:()=>l});let a=t(77538),s=t(29021),o=t(33873);function l({userId:e}){try{let r=o.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(s.readFileSync(r,"utf-8"))}catch(e){return a({component:"/functions/backend/grabUserSchemaData",message:e.message}),null}}},27444:(e,r,t)=>{t.d(r,{A:()=>i});var a=t(47670),s=t.n(a);let o=t(77538),l=t(51348);async function i({paradigm:e,userId:r,dbName:t,tableName:a,payload:i,payloadType:n}){let d={};try{let o=`${process.env.DSQL_USER_DB_PREFIX}${r}_${t.replace(/ /g,"")}`;switch(e){case"export":let u=await l(`SELECT * FROM \`${o}\`.\`${a}\``);d.tableData=u;break;case"import":let c="base64"==n?Buffer.from(i,"base64").toString():i,m="object"===n?i:JSON.parse(c);for(let e=0;e{let a=t(29021),{IncomingMessage:s}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:s,req:o}){let l=new Date,i=(()=>{if(!o)return null;try{let e=o.headers["x-forwarded-for"],r=o.headers["x-real-ip"],t=o.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||a||r||o.socket.remoteAddress;if(!s)return null;return String(s)}catch(e){return null}})();try{let s=`🚀 SERVER ERROR =========================== Error Message: ${r} Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=` User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email}`),o?.url&&(s+=` URL: ${o.url}`),o?.body&&(s+=` Request Body: ${JSON.stringify(o.body,null,4)}`),i&&(s+=` IP: ${i}`),s+=` Date: ${l.toDateString()} ========================================`,a.existsSync("./.tmp/error.log")||a.writeFileSync("./.tmp/error.log","","utf-8");let n=a.readFileSync("./.tmp/error.log","utf-8");a.writeFileSync("./.tmp/error.log",s),a.appendFileSync("./.tmp/error.log",` ${n}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},6420:(e,r,t)=>{t(81630);let a=t(51348),s=t(72907),o=t(29021);async function l(e,r,t,l){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let i=s(e.cookies.datasquirelAuthKey);if(!i)return null;let n=JSON.parse(i);if(!n.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${n.csrf_k}`)))return null;let d=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!d)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!o.existsSync(`${d}/${n.csrf_k}`))return null;if(0==n.verification_status&&!t){let e=await a(`SELECT verification_status FROM users WHERE id='${n.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return n?.date&&Date.now()-n.date>6048e5?null:n}e.exports=l},72907:(e,r,t)=>{let{scryptSync:a,createDecipheriv:s}=t(55511),{Buffer:o}=t(79428);e.exports=e=>{let r=s("aes-192-cbc",a(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),o.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(41848));module.exports=a})();