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

16 lines
13 KiB
JavaScript
Raw Normal View History

2025-02-17 05:55:49 +00:00
"use strict";(()=>{var e={};e.id=7214,e.ids=[7214],e.modules={76102:e=>{e.exports=require("@moduletrace/datasquirel/dist/client")},39383:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/actions/users/user-auth")},65967:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/ejson")},93726:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/numberfy")},30889:e=>{e.exports=require("@moduletrace/datasquirel/dist/package-shared/utils/slugify")},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")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},53382:(e,r,t)=>{t.r(r),t.d(r,{config:()=>g,default:()=>f,routeModule:()=>L});var s={};t.r(s),t.d(s,{default:()=>m});var n=t(89947),a=t(2706),o=t(96762),l=t(29021),i=t.n(l),c=t(21820),u=t.n(c),d=t(79646),_=t(30802),p=t(67619),S=t(69842),D=t(30944);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let{user:t}=await (0,S.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{id:s,db_name:n,db_slug:a,db_description:o,db_image:l,db_full_name:c,active_clone:m,active_clone_parent_db:f}=e.body;c?.match(/datasquirel_user_.*/)||r.json({success:!1,msg:"Unauthorized"});let g=`datasquirel_user_${t.id}_${a}`;if(await (0,_.A)({dbFullName:"datasquirel",tableName:"user_databases",identifierColumnName:"id",identifierValue:s,data:{db_name:n,db_slug:a,db_description:o,db_image:l,db_full_name:g}}),c&&c!==g){console.log("Renaming Database");let e=u().platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe'":"mysql",r=u().platform().match(/win/i)?"'C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe'":"mysqldump";try{await (0,D.A)({queryString:`CREATE DATABASE \`${g}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`});let t={cwd:process.cwd()};u().platform().match(/win/i)&&(t.shell="bash.exe");let s=`${r} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${c} > tmpDbFile.sql`;console.log(`updateUserDatabase.ts exe => ${s}`),(0,d.execSync)(s,t),(0,d.execSync)(`${e} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${g} < tmpDbFile.sql`,t),await (0,D.A)({queryString:`DROP DATABASE \`${c}\``})}catch(e){(0,p.A)({component:"/api/updateUserDatabase/lines-88-111",message:e.message,user:t})}}try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/main.json`,s=JSON.parse(i().readFileSync(e,"utf8")),u=s.filter(e=>e.dbFullName===c);if(u[0].dbName=n,u[0].dbFullName=g,u[0].dbDescription=o,u[0].dbImage=l,u[0].dbSlug=a,m){let e=s.filter(e=>e.dbFullName===u[0].childDatabaseDbFullName)[0],r=e.childrenDatabases?.find(e=>e.dbFullName===c);r&&(r.dbFullName=g)}i().writeFileSync(`${e}`,JSON.stringify(s),"utf8"),r.json({success:!0})}catch(e){(0,p.A)({component:"/api/updateUserDatabase/main-catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let f=(0,o.M)(s,"default"),g=(0,o.M)(s,"config"),L=new n.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/updateUserDatabase",pathname:"/api/updateUserDatabase",bundlePath:"",filename:""},userland:s})},55153:(e,r,t)=>{t.d(r,{On:()=>n.a,_n:()=>o}),t(76102);var s=t(65967),n=t.n(s);t(30889);var a=t(93726);let o=t.n(a)()},69842:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(39383),n=t.n(s),a=t(55153);async function o(e,r,t,s){let o=n()({req:e,level:t?"deep":void 0}),l={DSQL_REMOTE_SQL_HOST:process.env.DSQL_REMOTE_SQL_HOST,DSQL_SU_USER_ID:process.env.DSQL_SU_USER_ID,DSQL_HOST_ENV:process.env.DSQL_HOST_ENV,DSQL_HOST:process
2025-01-14 06:19:19 +00:00
Error Message: ${r}
2025-02-17 05:55:49 +00:00
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=`
2024-12-08 19:56:57 +00:00
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
2025-02-17 05:55:49 +00:00
User Email: ${e?.email}`),a?.url&&(s+=`
URL: ${a.url}`),a?.body&&(s+=`
Request Body: ${JSON.stringify(a.body,null,4)}`),l&&(s+=`
IP: ${l}`),s+=`
2025-01-14 06:19:19 +00:00
Date: ${o.toDateString()}
2025-02-17 05:55:49 +00:00
========================================`,n().existsSync("./.tmp/error.log")||n().writeFileSync("./.tmp/error.log","","utf-8");let i=n().readFileSync("./.tmp/error.log","utf-8");n().writeFileSync("./.tmp/error.log",s),n().appendFileSync("./.tmp/error.log",`
2024-12-08 19:56:57 +00:00
2025-02-17 05:55:49 +00:00
${i}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},30944:(e,r,t)=>{t.d(r,{A:()=>l});var s=t(43775),n=t(67619),a=t(7447),o=t(64292);async function l({queryString:e,queryValuesArray:r,database:t,tableSchema:l,debug:i}){let c=(0,a.A)({fa:!0});global.DSQL_USE_LOCAL&&(c=(0,a.A)({local:!0})),t?.match(/^datasquirel$/)&&(c=(0,a.A)()),i&&(console.log("varDatabaseDbHandler:query:",e),console.log("varDatabaseDbHandler:values:",r),console.log("varDatabaseDbHandler:CONNECTION:",c.getConfig()));let u=await (0,o.A)(c,e,r);if(i&&console.log("varDatabaseDbHandler:results:",u),u&&l)try{return await (0,s.A)({unparsedResults:u,tableSchema:l})}catch(e){return console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",t,e),(0,n.A)({component:"varDatabaseDbHandler/lines-52-53",message:e.message}),null}else if(u)return u;else return null}},95969:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(55511),n=t(79428),a=t(31542);function o({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:o,salt:l,keyLen:i,algorithm:c,bufferAllocSize:u}=(0,a.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let d=(0,s.scryptSync)(o,l,i),_=n.Buffer.alloc(u,0),p=(0,s.createDecipheriv)(c,d,_);try{let r=p.update(e,"hex","utf8");return r+=p.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},68829:(e,r,t)=>{t.d(r,{A:()=>o});var s=t(55511),n=t(79428),a=t(31542);function o({data:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encryption string is invalid"),e;let{key:o,salt:l,keyLen:i,algorithm:c,bufferAllocSize:u}=(0,a.A)({encryptionKey:r});if(!o?.match(/.{8,}/))return console.log("Encryption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Encryption salt is invalid"),e;let d=(0,s.scryptSync)(o,l,i),_=n.Buffer.alloc(u,0),p=(0,s.createCipheriv)(c,d,_);try{let r=p.update(e,"utf8","hex");return r+=p.final("hex")}catch(r){return console.log("Error in encrypting =>",r.message),e}}},52939:(e,r,t)=>{t.d(r,{A:()=>s});function s({dbContext:e,dbFullName:r}){return!e?.match(/dsql.user/i)&&(!!global.DSQL_USE_LOCAL||!r||!!r.match(/^datasquirel$/))}},64292:(e,r,t)=>{async function s(e,r,t){try{if(!e)throw Error("No Connection Found!");if(!r)throw Error("Query String Required!");if("string"==typeof r){let s=await e.query(n(r),t);return JSON.parse(JSON.stringify(s))}if("object"!=typeof r)return null;{let t=[];for(let s=0;s<r.length;s++)try{let a=r[s],o=await e.query(n(a.query),a.values);t.push(JSON.parse(JSON.stringify(o)))}catch(e){console.log(`connDbHandler Query Error: ${e.message}`),t.push(null)}return t}}catch(r){return console.log(`connDbHandler Error: ${r.message}`),console.log(e?.config()),null}finally{e?.end()}}function n(e){return e.replace(/\n/gm,"").replace(/ {2,}/g,"").trim()}t.d(r,{A:()=>s})},7447:(e,r,t)=>{t.d(r,{A:()=>a});var s=t(9973),n=t.n(s);function a(e){return global.DSQL_USE_LOCAL||e?.local?global.DSQL_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.ro?global.DSQL_READ_ONLY_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_READ_ONLY_USERNAME,password:process.env.DSQL_DB_READ_ONLY_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):e?.fa?global.DSQL_FULL_ACCESS_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_FULL_ACCESS_USERNAME,password:process.env.DSQL_DB_FULL_ACCESS_PASSWORD,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4"}}):global.DSQL_DB_CONN||n()({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:e?.noDb?void 0:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Nu