"use strict";(()=>{var e={};e.id=3382,e.ids=[3382],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},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")},21820:e=>{e.exports=require("os")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},84612:(e,r,t)=>{t.r(r),t.d(r,{config:()=>E,default:()=>p,routeModule:()=>S});var s={};t.r(s),t.d(s,{default:()=>f});var o=t(89947),n=t(2706),a=t(96762),i=t(67619),u=t(69842),c=t(76856);let d=t(29021),l=t(33873);t(21820);let{execSync:_}=t(79646);async function f(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,u.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{database:s,fileBase64:o}=e.body;s?.db_full_name?.match(/datasquirel_user_.*/)||r.json({success:!1,msg:"Unauthorized"});try{let e=Buffer.from(o,"base64").toString("utf-8"),n=JSON.parse(e),a=l.resolve(String(process.env.DSQL_USER_DB_SCHEMA_PATH),`user-${t.id}/main.json`);if(!d.existsSync(a))return r.end();let i=JSON.parse(d.readFileSync(a,"utf-8")),u=i.find(e=>e.dbFullName==s.db_full_name);if(!u)throw Error("Database not found!");u.tables=n.tables,d.writeFileSync(a,JSON.stringify(i),"utf-8"),await (0,c.A)({userId:t.id,targetDatabase:s.db_full_name}),r.json({success:!0})}catch(e){console.log("JSON import ERROR =>",e.message),(0,i.default)({component:"/api/importDatabase/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Import Failed!"})}}let p=(0,a.M)(s,"default"),E=(0,a.M)(s,"config"),S=new o.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/importDatabaseSchema",pathname:"/api/importDatabaseSchema",bundlePath:"",filename:""},userland:s})},69842:(e,r,t)=>{t.d(r,{A:()=>u});var s=t(48786),o=t(95969),n=t(29021),a=t.n(n),i=t(89159);async function u(e,r,t){try{let{keyCookieName:n,csrfCookieName:u}=(0,i.A)();if(!e.cookies?.[n]?.match(/./))throw Error("Request Cookies not Found!");let c=(0,o.A)({encryptedString:e.cookies[n]});if(!c)throw Error("Couldn't decrypt auth cookie!");let d=JSON.parse(c);if(!d.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(d.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let l=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!l)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!a().existsSync(`${l}/${d.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==d.verification_status&&!t){let e=await (0,s.A)(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(d?.date&&Date.now()-d.date>6048e5)throw Error("Key expired!");return d}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>s});function s(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",o=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",n=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),a=r;e?.userId&&(a+=`user_${e.userId}_`),n&&(a+=`${n}_`),a+=t;let i=r;e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),i+=s;let u=r;return e?.userId&&(u+=`user_${e.userId}_`),n&&(u+=`${n}_`),{keyCookieName:a,csrfCookieName:i,oneTimeCodeName:u+=o}}},48786:(e,r,t)=>{t.d(r,{A:()=>s});async function s(...e){let r=global.DSQL_DB_CONN;try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),s=r.X(0,[2282],()=>t(84612));module.exports=s})();