dsql-admin/dsql-app/.local_dist/server/pages/api/importDatabase.js

16 lines
8.0 KiB
JavaScript
Raw Normal View History

2024-12-05 07:03:33 +00:00
"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:()=>g,default:()=>S,routeModule:()=>b});var a={};r.r(a),r.d(a,{default:()=>h});var s=r(89947),l=r(2706),n=r(96762),i=r(72350),o=r.n(i),u=r(77538),c=r.n(u),d=r(6420),p=r.n(d);let m=r(29021),f=r(21820),{execSync:_}=r(79646);async function h(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=f.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()};f.platform().match(/win/i)&&(n.shell="bash.exe"),_(`${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 S=(0,n.M)(a,"default"),g=(0,n.M)(a,"config"),b=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),s=r(41709),{default:l}=r(49827),{default:n}=r(19857),i=r(47670);e.exports=async function({userId:e,database:t}){try{if(!e){console.log("No user Id provided");return}let r=l({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 l=u[r],n=Object.values(l)[0];await i({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:e,db_id:t.id,db_slug:t.db_slug,table_name:s(n),table_slug:n}});let c={tableName:n,tableFullName:s(n)||"",fields:[],indexes:[]},d=await a({database:t.db_full_name,queryString:`SHOW COLUMNS FROM ${n}`});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 ${n}`});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 n({schemaData:r,userId:e}),!0}catch(e){return console.log(e),!1}}},49827:(e,t,r)=>{r.r(t),r.d(t,{default:()=>n});let a=r(77538),s=r(29021),l=r(33873);function n({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:"/functions/backend/grabUserSchemaData",message:e.message}),null}}},77538:(e,t,r)=>{let a=r(290
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",`
2024-11-05 11:12:42 +00:00
2024-12-05 07:03:33 +00:00
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},19857:(e,t,r)=>{r.r(t),r.d(t,{default:()=>n});let a=r(77538),s=r(29021),l=r(33873);function n({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}}},6420:(e,t,r)=>{r(81630);let a=r(51348),s=r(72907),l=r(29021);async function n(e,t,r,n){if(!e.cookies?.datasquirelAuthKey?.match(/./))return null;let i=s(e.cookies.datasquirelAuthKey);if(!i)return null;let o=JSON.parse(i);if(!o.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${o.csrf_k}`)))return null;let u=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!u)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!l.existsSync(`${u}/${o.csrf_k}`))return null;if(0==o.verification_status&&!r){let e=await a(`SELECT verification_status FROM users WHERE id='${o.id}'`);e&&e[0]&&1==e[0].verification_status&&t.setHeader("Set-Cookie",["user_refresh=1"])}return o?.date&&Date.now()-o.date>6048e5?null:o}e.exports=n},72907:(e,t,r)=>{let{scryptSync:a,createDecipheriv:s}=r(55511),{Buffer:l}=r(79428);e.exports=e=>{let t=s("aes-192-cbc",a(process.env.DSQL_ENCRYPTION_PASSWORD||"",process.env.DSQL_ENCRYPTION_SALT||"",24),l.alloc(16,0));try{let r=t.update(e,"hex","utf8");return r+=t.final("utf8")}catch(e){return null}}},30003:e=>{e.exports=/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/},6061:(e,t,r)=>{let a=r(72907),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(o))}r.push(n)}return r}catch(t){return console.log("ERROR in parseDbResults Function =>",t.message),e}}},57638:(e,t,r)=>{r(29021);let a=r(6061),s=r(48225),l=r(51348),n=r(21339);e.exports=async function({queryString:e,queryValuesArray:t,database:r,tableSchema:i}){let o;let u=!!r?.match(/^datasquirel$/),c=u?l:n;try{o=e&&t&&Array.isArray(t)&&t[0]?u?await c(e,t):await c({paradigm:"Full Access",database:r,queryString:e,queryValues:t}):u?await c(e):await c({paradigm:"Full Access",database:r,queryString:e})}catch(e){s({component:"varDatabaseDbHandler/lines-29-32",message:e.message})}if(o&&i)try{let e=o;return await a({unparsedResults:e,tableSchema:i})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),s({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(o)return o;else return null}},41709: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})();