"use strict";(()=>{var e={};e.id=2562,e.ids=[2562],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")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},82453:(e,r,t)=>{t.r(r),t.d(r,{config:()=>x,default:()=>$,routeModule:()=>R});var i={};t.r(i),t.d(i,{config:()=>E,default:()=>v});var s=t(89947),a=t(2706),n=t(96762),l=t(33873),o=t.n(l),u=t(47670),d=t.n(u),c=t(64122),m=t.n(c),p=t(48227),f=t.n(p),g=t(48225),_=t.n(g),S=t(77619),y=t.n(S);let h=t(29021),E={api:{bodyParser:{sizeLimit:"50mb"}}};async function v(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});try{let t=e.headers.authorization;if(!t)return r.json({success:!1,msg:"Unauthorized"});let i=y()({key:t,user_id:String(e.query.user_id)});if(!i?.user_id)throw Error("Api Credentials invalid!");let{user_id:s,full_access:a}=i;if(!a)return r.json({success:!1,msg:"Unauthorized"});let{fileData:n,fileName:l,mimeType:u,folder:c,isPrivate:p}=e.body,g=process.env.DSQL_STATIC_SERVER_DIR;if(!g)throw console.log("Static File ENV not Found!"),Error("No Static Path!");if(c){let e=o().join(g,`images/user-images/user-${s}/${c?.toString().replace(/\.\./g,"")}`);h.existsSync(e)||h.mkdirSync(e,{recursive:!0})}let _=f()({folder:c,isPrivate:p,user:i});if(!_)throw Error("Couldn't Grab Image URLs");let{fileRootPath:S,urlRootPath:E}=_,v=u?.match(/csv/i)?".csv":u?.match(/pdf/i)?".pdf":u?.match(/xlsx/)?".xlsx":u?.match(/json/i)?".json":".txt",$=E+l+v;h.writeFileSync(S+l+v,n,"base64"),await m()({dbFullName:"datasquirel",tableName:"user_media",identifierColumnName:"media_url",identifierValue:$}),await d()({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:s,media_name:l,media_url:$,media_thumbnail_url:$,folder:c||"",media_type:"file",private:p?"1":null}}),r.json({success:!0,payload:{urlPath:$}})}catch(t){console.log("File write error:",t),_()({component:"/api/query/add-file/main-catch-error",message:t.message,req:e}),r.json({success:!1,msg:"Add File Error!",error:t.message})}}let $=(0,n.M)(i,"default"),x=(0,n.M)(i,"config"),R=new s.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/query/[user_id]/add-file",pathname:"/api/query/[user_id]/add-file",bundlePath:"",filename:""},userland:i})},48227:(e,r,t)=>{let i=t(29021),s=t(33873);e.exports=function({isPrivate:e,user:r,folder:t,video:a,pathOnly:n}){"production".match(/production/);let l=r?.id||r?.user_id,o=process.env.DSQL_STATIC_SERVER_DIR;if(!o)return console.log("Static File ENV not Found!"),null;let u=e?`@/${a?"videos":"media"}/${t?t+"/":""}`:a?`/videos/user-videos/user-${l}/${t?t+"/":""}`:`/images/user-images/user-${l}/${t?t+"/":""}`,d=e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${l}/media/${t?t+"/":""}`:s.join(o,u);return i.existsSync(d)||n||i.mkdirSync(d,{recursive:!0}),{fileRootPath:d,urlRootPath:e?`@/media/${t?t+"/":""}`:`${process.env.DSQL_STATIC_HOST}${u}`,relativePath:u}}},77619:(e,r,t)=>{let i=t(29021),s=t(72835);e.exports=({key:e,database:r,table:t,user_id:a})=>{if(!e||!a)return null;try{let n=process.env.DSQL_API_KEYS_PATH;if(!n)throw Error("process.env.DSQL_API_KEYS_PATH variable not found");let l=s({encryptedString:e}),o=JSON.parse(l||""),u=i.existsSync(`${n}/${o.sign}`);if(String(o.user_id)!==String(a)||!u)return null;if(!o.target_database)return o;if(!r&&o.target_database)return null;let d=o.target_database?.split(",").includes(String(r));if(d&&!o.target_table)return o;if(d&&!t&&o.target_table)return null;if(o.target_table?.split(",").includes(String(t)))return o;return null}catch(e){return console.log(`api-cred ERROR: ${e.message}`),null}}},64122:(e,r,t)=>{let i=t(51348),s=t(21339),a=t(44900);async function n({dbContext:e,paradigm:r,dbFullName:t,tableName:n,identifierColumnName:l,identifierValue:o,useLocal:u}){try{let d=!e?.match(/dsql.user/i)&&(!t||!!t.match(/^datasquirel$/)),c=u?a:d?i:s,m=`DELETE FROM ${n} WHERE \`${l}\`=?`;return d?await c(m,[o]):await c({paradigm:r,queryString:m,database:t,queryValues:[o]})}catch(e){return null}}e.exports=n},48225:(e,r,t)=>{let i=t(29021),{IncomingMessage:s}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:s,req:a}){let n=new Date,l=(()=>{if(!a)return null;try{let e=a.headers["x-forwarded-for"],r=a.headers["x-real-ip"],t=a.headers["cf-connecting-ip"],i=Array.isArray(e)?e[0]:e?.split(",")[0],s=t||i||r||a.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}`),a?.url&&(s+=`
URL: ${a.url}`),a?.body&&(s+=`
Request Body: ${JSON.stringify(a.body,null,4)}`),l&&(s+=`
IP: ${l}`),s+=`
Date: ${n.toDateString()}
========================================`,i.existsSync("./.tmp/error.log")||i.writeFileSync("./.tmp/error.log","","utf-8");let o=i.readFileSync("./.tmp/error.log","utf-8");i.writeFileSync("./.tmp/error.log",s),i.appendFileSync("./.tmp/error.log",`




${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},72835:(e,r,t)=>{let{scryptSync:i,createDecipheriv:s}=t(55511),{Buffer:a}=t(79428);e.exports=({encryptedString:e,encryptionKey:r,encryptionSalt:t})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,l=t||process.env.DSQL_ENCRYPTION_SALT,o=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=s("aes-192-cbc",i(n,l,o),a.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),i=r.X(0,[4050],()=>t(82453));module.exports=i})();