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

16 lines
8.6 KiB
JavaScript
Raw Normal View History

2024-12-06 13:24:26 +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:()=>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),f=r(21820),{execSync:_}=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=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 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
2024-12-05 07:03:33 +00:00
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-06 13:24:26 +00:00
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},78634:(e,t,r)=>{r.r(t),r.d(t,{default:()=>n});let a=r(48225),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}}},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}},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})();