dsql-admin/dsql-app/.local_dist/server/pages/api/importDatabase.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=8675,e.ids=[8675],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")},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")},21820:e=>{e.exports=require("os")},33873:e=>{e.exports=require("path")},75588:(e,t,r)=>{r.r(t),r.d(t,{config:()=>h,default:()=>g,routeModule:()=>y});var a={};r.r(a),r.d(a,{default:()=>S});var s=r(89947),l=r(2706),n=r(96762),i=r(72350),o=r.n(i),u=r(48225),c=r.n(u),d=r(6420),p=r.n(d);let m=r(29021),_=r(21820),{execSync:f}=r(79646);async function S(e,t){if("POST"!==e.method)return t.json({msg:"Failed!"});let r=await p()(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=_.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql";m.writeFileSync(`./.tmp/${e}.sql`,s,"base64");let n={cwd:process.cwd()};_.platform().match(/win/i)&&(n.shell="bash.exe"),f(`${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 o()({database:a,userId:r.id});t.json({success:!!i})}catch(e){console.log(e),c()({component:"/api/importDatabase/catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Import Failed!"})}}let g=(0,n.M)(a,"default"),h=(0,n.M)(a,"config"),y=new s.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/importDatabase",pathname:"/api/importDatabase",bundlePath:"",filename:""},userland:a})},72350:(e,t,r)=>{let a=r(57638),{default:s}=r(2258),{default:l}=r(78634),n=r(47670),i=r(24256);e.exports=async function({userId:e,database:t}){try{if(!e){console.log("No user Id provided");return}let r=s({userId:e});if(!r)throw Error("User schema data not found!");let o=r.filter(e=>e.dbFullName===t.db_full_name)[0],u=await a({database:t.db_full_name,queryString:"SHOW TABLES"});if(!u)throw Error("No Existing Tables");for(let r=0;r<u.length;r++){let s=u[r],l=Object.values(s)[0];await n({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:e,db_id:t.id,db_slug:t.db_slug,table_name:i(l),table_slug:l}});let c={tableName:l,tableFullName:i(l)||"",fields:[],indexes:[]},d=await a({database:t.db_full_name,queryString:`SHOW COLUMNS FROM ${l}`});if(d)for(let e=0;e<d.length;e++){let{Field:t,Type:r,Null:a,Key:s,Default:l,Extra:n}=d[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),c.fields.push(i)}let p=await a({database:t.db_full_name,queryString:`SHOW INDEXES FROM ${l}`});if(p)for(let e=0;e<p.length;e++){let{Table:t,Key_name:r,Column_name:a,Null:s,Index_type:l,Index_comment:n}=p[e];if(!n?.match(/^schema_index$/))continue;let i={indexType:l?.match(/fulltext/i)?"fullText":"regular",indexName:r,indexTableFields:[]},o=d?.filter(e=>e.Field===a)[0],u=c.indexes?.filter(e=>e.indexName==r);u&&u[0]?u[0].indexTableFields?.push({value:a,dataType:o.Type.toUpperCase()}):(i.indexTableFields=[{value:a,dataType:o.Type.toUpperCase()}],c.indexes?.push(i))}o.tables.push(c)}return l({schemaData:r,userId:e}),!0}catch(e){return console.log(e),!1}}},6420:(e,t,r)=>{r(81630);let a=r(51348),s=r(72835),l=r(29021),n=r(31341);async function i(e,t,r,i){let{keyCookieName:o,csrfCookieName:u}=n();if(!e.cookies?.[o]?.match(/./))return null;let c=s({encryptedString:e.cookies[o]});if(!c)return null;let d=JSON.parse(c);if(!d.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.csrf_k}`)))return null;let p=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!p)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!l.existsSync(`${p}/${d.csrf_k}`))return null;if(0==d.verification_status&&!r){let e=await 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}e.exports=i},31341:e=>{e.exports=function(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}}},30003:e=>{e.exports=/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/},2258:(e,t,r)=>{let a=r(48225),s=r(29021),l=r(33873);e.exports=function({userId:e}){try{let t=l.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(s.readFileSync(t,"utf-8"))}catch(e){return a({component:"grabUserSchemaData",message:e.message}),null}}},6061:(e,t,r)=>{let a=r(72835),s=r(30003);e.exports=async function({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]=a({encryptedString:o}))}r.push(n)}return r}catch(t){return console.log("ERROR in parseDbResults Function =>",t.message),e}}},48225:(e,t,r)=>{let a=r(29021),{IncomingMessage:s}=r(81630);e.exports=async function({user:e,message:t,component:r,noMail:s,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 s=`🚀 SERVER ERROR ===========================
Error Message: ${t}
Component: ${r}`;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}`),l?.url&&(s+=`
URL: ${l.url}`),l?.body&&(s+=`
Request Body: ${JSON.stringify(l.body,null,4)}`),i&&(s+=`
IP: ${i}`),s+=`
Date: ${n.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",s),a.appendFileSync("./.tmp/error.log",`
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},78634:(e,t,r)=>{let a=r(48225),s=r(29021),l=r(33873);e.exports=function({userId:e,schemaData:t}){try{let r=l.resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return s.writeFileSync(r,JSON.stringify(t),"utf8"),!0}catch(e){return a({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}},57638:(e,t,r)=>{r(29021);let a=r(6061),s=r(48225),l=r(51348),n=r(21339),i=r(44900);e.exports=async function({queryString:e,queryValuesArray:t,database:r,tableSchema:o,useLocal:u}){let c;let d=!!u||!!r?.match(/^datasquirel$/),p=u?i:d?l:n;try{c=e&&t&&Array.isArray(t)&&t[0]?d?await p(e,t):await p({paradigm:"Full Access",database:r,queryString:e,queryValues:t}):d?await p(e):await p({paradigm:"Full Access",database:r,queryString:e})}catch(e){s({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(c&&o)try{let e=c;return await a({unparsedResults:e,tableSchema:o})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),s({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(c)return c;else return null}},72835:(e,t,r)=>{let{scryptSync:a,createDecipheriv:s}=r(55511),{Buffer:l}=r(79428);e.exports=({encryptedString:e,encryptionKey:t,encryptionSalt:r})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=t||process.env.DSQL_ENCRYPTION_PASSWORD,i=r||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(!i?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=s("aes-192-cbc",a(n,i,o),l.alloc(16,0));try{let t=u.update(e,"hex","utf8");return t+=u.final("utf8")}catch(t){return console.log("Error in decrypting =>",t.message),e}}},24256:e=>{e.exports=function(e){return e?e.split("-").filter(e=>""!==e).map(e=>e.substr(0,1).toUpperCase()+e.substr(1).toLowerCase()).join(" "):null}}};var t=require("../../webpack-api-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[4050],()=>r(75588));module.exports=a})();