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

16 lines
9.4 KiB
JavaScript
Raw Normal View History

2025-01-14 06:19:19 +00:00
"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,r,t)=>{t.r(r),t.d(r,{config:()=>b,default:()=>y,routeModule:()=>E});var a={};t.r(a),t.d(a,{default:()=>g});var s=t(89947),n=t(2706),l=t(96762),i=t(30944),o=t(4820),u=t(21724),d=t(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:r}){try{if(!e){console.log("No user Id provided");return}let t=(0,o.A)({userId:e});if(!t)throw Error("User schema data not found!");let a=t.filter(e=>e.dbFullName===r.db_full_name)[0],s=await (0,i.A)({database:r.db_full_name,queryString:`SHOW TABLES FROM ${r.db_full_name}`});if(!s)throw Error("No Existing Tables");for(let t=0;t<s.length;t++){let n=s[t],l=Object.values(n)[0];await (0,d.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:e,db_id:r.id,db_slug:r.db_slug,table_name:c(l),table_slug:l}});let o={tableName:l,tableFullName:c(l)||"",fields:[],indexes:[]},u=await (0,i.A)({database:r.db_full_name,queryString:`SHOW COLUMNS FROM ${r.db_full_name}.${l}`});if(u)for(let e=0;e<u.length;e++){let{Field:r,Type:t,Null:a,Key:s,Default:n,Extra:l}=u[e],i={fieldName:r,dataType:t.toUpperCase()};a?.match(/^no$/i)&&(i.notNullValue=!0),s?.match(/^pri$/i)&&(i.primaryKey=!0),n?.toString()?.match(/./)&&(i.defaultValue=n),n?.toString()?.match(/timestamp/i)&&(delete i.defaultValue,i.defaultValueLiteral=n),l?.toString()?.match(/auto_increment/i)&&(i.autoIncrement=!0),o.fields.push(i)}let m=await (0,i.A)({database:r.db_full_name,queryString:`SHOW INDEXES FROM ${r.db_full_name}.${l}`});if(m)for(let e=0;e<m.length;e++){let{Table:r,Key_name:t,Column_name:a,Null:s,Index_type:n,Index_comment:l}=m[e];if(!l?.match(/^schema_index$/))continue;let i={indexType:n?.match(/fulltext/i)?"fullText":"regular",indexName:t,indexTableFields:[]},d=u?.filter(e=>e.Field===a)[0],c=o.indexes?.filter(e=>e.indexName==t);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:t,userId:e}),!0}catch(e){return console.log(e),!1}}var p=t(67619),_=t(69842);let f=t(29021),h=t(21820),{execSync:S}=t(79646);async function g(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,_.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{database:a,fileBase64:s}=e.body;a?.db_full_name?.match(/datasquirel_user_.*/)||r.json({success:!1,msg:"Unauthorized"});try{let e=`user-${t.id}-temp-sql`,n=h.platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql";f.writeFileSync(`./.tmp/${e}.sql`,s,"base64");let l={cwd:process.cwd()};h.platform().match(/win/i)&&(l.shell="bash.exe"),S(`${n} -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`,l);let i=await m({database:a,userId:t.id});r.json({success:!!i})}catch(e){console.log(e),(0,p.default)({component:"/api/importDatabase/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Import Failed!"})}}let y=(0,l.M)(a,"default"),b=(0,l.M)(a,"config"),E=new s.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/importDatabase",pathname:"/api/importDatabase",bundlePath:"",filename:""},userland:a})},69842:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(48786),s=t(95969),n=t(29021),l=t.n(n),i=t(89159);async function o(e,r,t){try{let{keyCookieName:n,csrfCookieName:o}=(0,i.A)();if(!e.cookies?.[n]?.match(/./))throw Error("Request Cookies not Found!");let u=(0,s.A)({encryptedString:e.cookies[n]});if(!u)throw Error("Cou
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
2024-12-08 19:56:57 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2025-01-14 06:19:19 +00:00
User Email: ${e?.email}`),n?.url&&(a+=`
URL: ${n.url}`),n?.body&&(a+=`
Request Body: ${JSON.stringify(n.body,null,4)}`),i&&(a+=`
2025-01-13 08:00:21 +00:00
IP: ${i}`),a+=`
2025-01-14 06:19:19 +00:00
Date: ${l.toDateString()}
2025-01-13 08:00:21 +00:00
========================================`,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",`
2024-12-08 19:56:57 +00:00
2025-01-14 06:19:19 +00:00
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},21724:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(67619),s=t(29021),n=t.n(s),l=t(33873),i=t.n(l);function o({userId:e,schemaData:r}){try{let t=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return n().writeFileSync(t,JSON.stringify(r),"utf8"),!0}catch(e){return(0,a.default)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}},30944:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(43775),s=t(67619),n=t(48786),l=t(37275),i=t(7971);async function o({queryString:e,queryValuesArray:r,database:t,tableSchema:o,useLocal:u}){let d;let c=!!u||!!t?.match(/^datasquirel$/),m=u?i.A:c?n.A:l.default;try{d=e&&r&&Array.isArray(r)&&r[0]?c?await m(e,r):await m({paradigm:"Full Access",queryString:e,queryValues:r}):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 =>",t,e),(0,s.default)({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(d)return d;else return null}},95969:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(55511),s=t(79428);function n({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,l=t||process.env.DSQL_ENCRYPTION_SALT,i=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(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let o=(0,a.scryptSync)(n,l,i),u=s.Buffer.alloc(16,0),d=(0,a.createDecipheriv)("aes-192-cbc",o,u);try{let r=d.update(e,"hex","utf8");return r+=d.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[1251],()=>t(54356));module.exports=a})();