dsql-admin/dsql-app/.local_dist/server/pages/api/importDatabase.js
Benjamin Toby e0a030f10d Updates
2025-01-13 09:00:21 +01:00

16 lines
9.2 KiB
JavaScript

"use strict";(()=>{var e={};e.id=8675,e.ids=[8675],e.modules={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")},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")},54356:(e,t,r)=>{r.r(t),r.d(t,{config:()=>y,default:()=>h,routeModule:()=>E});var a={};r.r(a),r.d(a,{default:()=>b});var s=r(89947),l=r(2706),n=r(96762),i=r(30944),o=r(4820),u=r(21724),d=r(98e3);function c(e){return e?e.split("-").filter(e=>""!==e).map(e=>e.substr(0,1).toUpperCase()+e.substr(1).toLowerCase()).join(" "):null}async function m({userId:e,database:t}){try{if(!e){console.log("No user Id provided");return}let r=(0,o.A)({userId:e});if(!r)throw Error("User schema data not found!");let a=r.filter(e=>e.dbFullName===t.db_full_name)[0],s=await (0,i.A)({database:t.db_full_name,queryString:`SHOW TABLES FROM ${t.db_full_name}`});if(!s)throw Error("No Existing Tables");for(let r=0;r<s.length;r++){let l=s[r],n=Object.values(l)[0];await (0,d.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:e,db_id:t.id,db_slug:t.db_slug,table_name:c(n),table_slug:n}});let o={tableName:n,tableFullName:c(n)||"",fields:[],indexes:[]},u=await (0,i.A)({database:t.db_full_name,queryString:`SHOW COLUMNS FROM ${t.db_full_name}.${n}`});if(u)for(let e=0;e<u.length;e++){let{Field:t,Type:r,Null:a,Key:s,Default:l,Extra:n}=u[e],i={fieldName:t,dataType:r.toUpperCase()};a?.match(/^no$/i)&&(i.notNullValue=!0),s?.match(/^pri$/i)&&(i.primaryKey=!0),l?.toString()?.match(/./)&&(i.defaultValue=l),l?.toString()?.match(/timestamp/i)&&(delete i.defaultValue,i.defaultValueLiteral=l),n?.toString()?.match(/auto_increment/i)&&(i.autoIncrement=!0),o.fields.push(i)}let m=await (0,i.A)({database:t.db_full_name,queryString:`SHOW INDEXES FROM ${t.db_full_name}.${n}`});if(m)for(let e=0;e<m.length;e++){let{Table:t,Key_name:r,Column_name:a,Null:s,Index_type:l,Index_comment:n}=m[e];if(!n?.match(/^schema_index$/))continue;let i={indexType:l?.match(/fulltext/i)?"fullText":"regular",indexName:r,indexTableFields:[]},d=u?.filter(e=>e.Field===a)[0],c=o.indexes?.filter(e=>e.indexName==r);c&&c[0]?c[0].indexTableFields?.push({value:a,dataType:d.Type.toUpperCase()}):(i.indexTableFields=[{value:a,dataType:d.Type.toUpperCase()}],o.indexes?.push(i))}a.tables.push(o)}return(0,u.A)({schemaData:r,userId:e}),!0}catch(e){return console.log(e),!1}}var p=r(67619),_=r(69842);let f=r(29021),S=r(21820),{execSync:g}=r(79646);async function b(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await (0,_.A)(e,t,!0);if(!r)return t.json({success:!1,msg:"Unauthorized"});let{database:a,fileBase64:s}=e.body;a?.db_full_name?.match(/datasquirel_user_.*/)||t.json({success:!1,msg:"Unauthorized"});try{let e=`user-${r.id}-temp-sql`,l=S.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql";f.writeFileSync(`./.tmp/${e}.sql`,s,"base64");let n={cwd:process.cwd()};S.platform().match(/win/i)&&(n.shell="bash.exe"),g(`${l} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${a.db_full_name} < .tmp/${e}.sql`,n);let i=await m({database:a,userId:r.id});t.json({success:!!i})}catch(e){console.log(e),(0,p.default)({component:"/api/importDatabase/catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Import Failed!"})}}let h=(0,n.M)(a,"default"),y=(0,n.M)(a,"config"),E=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/importDatabase",pathname:"/api/importDatabase",bundlePath:"",filename:""},userland:a})},69842:(e,t,r)=>{r.d(t,{A:()=>o});var a=r(48786),s=r(95969),l=r(29021),n=r.n(l),i=r(89159);async function o(e,t,r){let{keyCookieName:l,csrfCookieName:o}=(0,i.A)();if(!e.cookies?.[l]?.match(/./))return null;let u=(0,s.A)({encryptedString:e.cookies[l]});if(!u)return null;let d=JSON.parse(u);if(!d.csrf_k||r&&!Object.keys(e.headers).find(e=>e.includes(d.csrf_k)))return null;let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!n().existsSync(`${c}/${d.csrf_k}`))return null;if(0==d.verification_status&&!r){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}},89159:(e,t,r)=>{r.d(t,{A:()=>a});function a(e){let t=process.env.DSQL_COOKIES_PREFIX||"dsql_",r=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",s=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",l=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=t;e?.userId&&(n+=`user_${e.userId}_`),l&&(n+=`${l}_`),n+=r;let i=t;e?.userId&&(i+=`user_${e.userId}_`),l&&(i+=`${l}_`),i+=a;let o=t;return e?.userId&&(o+=`user_${e.userId}_`),l&&(o+=`${l}_`),{keyCookieName:n,csrfCookieName:i,oneTimeCodeName:o+=s}}},4820:(e,t,r)=>{r.d(t,{A:()=>o});var a=r(67619),s=r(29021),l=r.n(s),n=r(33873),i=r.n(n);function o({userId:e}){try{let t=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(l().readFileSync(t,"utf-8"))}catch(e){return(0,a.default)({component:"grabUserSchemaData",message:e.message}),null}}},43775:(e,t,r)=>{r.d(t,{A:()=>l});var a=r(95969);let s=/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;async function l({unparsedResults:e,tableSchema:t}){let r=[];try{for(let l=0;l<e.length;l++){let n=e[l],i=Object.keys(n);for(let e=0;e<i.length;e++){let r=i[e],l=t?.fields[e];if(r?.match(s))continue;let o=n[r];("number"==typeof o||o)&&l?.encrypted&&o?.match(/./)&&(n[r]=(0,a.A)({encryptedString:o}))}r.push(n)}return r}catch(t){return console.log("ERROR in parseDbResults Function =>",t.message),e}}},67619:(e,t,r)=>{r.r(t),r.d(t,{default:()=>l});var a=r(29021),s=r.n(a);async function l({user:e,message:t,component:r,noMail:a,req:l}){let n=new Date,i=(()=>{if(!l)return null;try{let e=l.headers["x-forwarded-for"],t=l.headers["x-real-ip"],r=l.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],s=r||a||t||l.socket.remoteAddress;if(!s)return null;return String(s)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
Error Message: ${t}
Component: ${r}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),l?.url&&(a+=`
URL: ${l.url}`),l?.body&&(a+=`
Request Body: ${JSON.stringify(l.body,null,4)}`),i&&(a+=`
IP: ${i}`),a+=`
Date: ${n.toDateString()}
========================================`,s().existsSync("./.tmp/error.log")||s().writeFileSync("./.tmp/error.log","","utf-8");let o=s().readFileSync("./.tmp/error.log","utf-8");s().writeFileSync("./.tmp/error.log",a),s().appendFileSync("./.tmp/error.log",`
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},21724:(e,t,r)=>{r.d(t,{A:()=>o});var a=r(67619),s=r(29021),l=r.n(s),n=r(33873),i=r.n(n);function o({userId:e,schemaData:t}){try{let r=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return l().writeFileSync(r,JSON.stringify(t),"utf8"),!0}catch(e){return(0,a.default)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}},30944:(e,t,r)=>{r.d(t,{A:()=>o});var a=r(43775),s=r(67619),l=r(48786),n=r(37275),i=r(7971);async function o({queryString:e,queryValuesArray:t,database:r,tableSchema:o,useLocal:u}){let d;let c=!!u||!!r?.match(/^datasquirel$/),m=u?i.A:c?l.A:n.default;try{d=e&&t&&Array.isArray(t)&&t[0]?c?await m(e,t):await m({paradigm:"Full Access",queryString:e,queryValues:t}):c?await m(e):await m({paradigm:"Full Access",queryString:e})}catch(e){console.log(`varDatabaseDbHandler Error: ${e.message}`),(0,s.default)({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(d&&o)try{let e=d;return await (0,a.A)({unparsedResults:e,tableSchema:o})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),(0,s.default)({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(d)return d;else return null}},95969:(e,t,r)=>{r.d(t,{A:()=>l});var a=r(55511),s=r(79428);function l({encryptedString:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let l=t||process.env.DSQL_ENCRYPTION_PASSWORD,n=r||process.env.DSQL_ENCRYPTION_SALT,i=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 o=(0,a.scryptSync)(l,n,i),u=s.Buffer.alloc(16,0),d=(0,a.createDecipheriv)("aes-192-cbc",o,u);try{let t=d.update(e,"hex","utf8");return t+=d.final("utf8")}catch(t){return console.log("Error in decrypting =>",t.message),e}}}};var t=require("../../webpack-api-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[1251],()=>r(54356));module.exports=a})();