dsql-admin/dsql-app/.local_dist/server/pages/api/importDatabase.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

16 lines
7.7 KiB
JavaScript

"use strict";(()=>{var e={};e.id=8675,e.ids=[8675],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},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:()=>$,default:()=>v,routeModule:()=>A});var a={};r.r(a),r.d(a,{default:()=>S});var s=r(89947),l=r(2706),n=r(96762),i=r(29021),o=r.n(i),u=r(21820),d=r.n(u),c=r(79646),m=r(30944),p=r(4820),f=r(21724),g=r(98e3);function b(e){return e?e.split("-").filter(e=>""!==e).map(e=>e.substr(0,1).toUpperCase()+e.substr(1).toLowerCase()).join(" "):null}async function h({userId:e,database:t}){try{if(!e){console.log("No user Id provided");return}let r=(0,p.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,m.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,g.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:e,db_id:t.id,db_slug:t.db_slug,table_name:b(n),table_slug:n}});let i={tableName:n,tableFullName:b(n)||"",fields:[],indexes:[]},o=await (0,m.A)({database:t.db_full_name,queryString:`SHOW COLUMNS FROM ${t.db_full_name}.${n}`});if(o)for(let e=0;e<o.length;e++){let{Field:t,Type:r,Null:a,Key:s,Default:l,Extra:n}=o[e],u={fieldName:t,dataType:r.toUpperCase()};a?.match(/^no$/i)&&(u.notNullValue=!0),s?.match(/^pri$/i)&&(u.primaryKey=!0),l?.toString()?.match(/./)&&(u.defaultValue=l),l?.toString()?.match(/timestamp/i)&&(delete u.defaultValue,u.defaultValueLiteral=l),n?.toString()?.match(/auto_increment/i)&&(u.autoIncrement=!0),i.fields.push(u)}let u=await (0,m.A)({database:t.db_full_name,queryString:`SHOW INDEXES FROM ${t.db_full_name}.${n}`});if(u)for(let e=0;e<u.length;e++){let{Table:t,Key_name:r,Column_name:a,Null:s,Index_type:l,Index_comment:n}=u[e];if(!n?.match(/^schema_index$/))continue;let d={indexType:l?.match(/fulltext/i)?"fullText":"regular",indexName:r,indexTableFields:[]},c=o?.filter(e=>e.Field===a)[0],m=i.indexes?.filter(e=>e.indexName==r);m&&m[0]?m[0].indexTableFields?.push({value:a,dataType:c.Type.toUpperCase()}):(d.indexTableFields=[{value:a,dataType:c.Type.toUpperCase()}],i.indexes?.push(d))}a.tables.push(i)}return(0,f.A)({schemaData:r,userId:e}),!0}catch(e){return console.log(e),!1}}var y=r(67619),_=r(69842);async function S(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=d().platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql";o().writeFileSync(`./.tmp/${e}.sql`,s,"base64");let n={cwd:process.cwd()};d().platform().match(/win/i)&&(n.shell="bash.exe"),(0,c.execSync)(`${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 h({database:a,userId:r.id});t.json({success:!!i})}catch(e){console.log(e),(0,y.A)({component:"/api/importDatabase/catch-error",message:e.message,user:r}),t.json({success:!1,msg:"Import Failed!"})}}let v=(0,n.M)(a,"default"),$=(0,n.M)(a,"config"),A=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:()=>l});var a=r(39908),s=r.n(a);async function l(e,t,r){let a=s()({req:e,level:r?"deep":void 0});return console.log(a),a.payload}},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.A)({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.d(t,{A:()=>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.A)({component:"/functions/backend/setUserSchemaData",message:e.message}),!1}}},30944:(e,t,r)=>{r.d(t,{A:()=>i});var a=r(43775),s=r(67619),l=r(7447),n=r(64292);async function i({queryString:e,queryValuesArray:t,database:r,tableSchema:i,useLocal:o,debug:u}){let d=(0,l.A)({fa:!0});o&&(d=(0,l.A)({local:!0})),r?.match(/^datasquirel$/)&&(d=(0,l.A)()),u&&(console.log("varDatabaseDbHandler:query:",e),console.log("varDatabaseDbHandler:values:",t));let c=await (0,n.A)(d,e,t);if(u&&console.log("varDatabaseDbHandler:results:",c),c&&i)try{return await (0,a.A)({unparsedResults:c,tableSchema:i})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",r,e),(0,s.A)({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(c)return c;else return null}},95969:(e,t,r)=>{r.d(t,{A:()=>n});var a=r(55511),s=r(79428),l=r(32443);function n({encryptedString:e,encryptionKey:t,encryptionSalt:r}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:n,salt:i,keyLen:o,algorithm:u,bufferAllocSize:d}=(0,l.A)({encryptionKey:t});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 c=(0,a.scryptSync)(n,i,o),m=s.Buffer.alloc(d,0),p=(0,a.createDecipheriv)(u,c,m);try{let t=p.update(e,"hex","utf8");return t+=p.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})();