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

1 line
5.7 KiB
JavaScript
Raw Permalink Normal View History

2024-12-15 11:27:16 +00:00
"use strict";(()=>{var e={};e.id=8893,e.ids=[8893],e.modules={60818:e=>{e.exports=require("dotenv")},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")},33873:e=>{e.exports=require("path")},34932:(e,s,r)=>{r.r(s),r.d(s,{config:()=>R,default:()=>A,routeModule:()=>O});var a={};r.r(a),r.d(a,{default:()=>h});var t=r(89947),i=r(2706),n=r(96762),l=r(51348),u=r.n(l),o=r(48225),d=r.n(o),c=r(6420),_=r.n(c),p=r(47670),S=r.n(p),b=r(51926),m=r.n(b),E=r(19261),D=r.n(E);let f=r(29021);r(33873);let{execSync:N}=r(79646);async function h(e,s){if("POST"!==e.method)return s.json({msg:"Failed!"});let r=await _()(e,s,!0);if(!r)return s.json({success:!1,msg:"Unauthorized"});let a=await u()("SELECT * FROM users WHERE id = ?",[r.id]),t=a?.[0],{db_name:i,db_slug:n,db_description:l,db_image:o,duplicate:c,keepUpdated:p,keepDataUpdated:b}=e.body;if(n.match(/[^a-zA-Z0-9-_]/))return s.json({success:!1,msg:"Input Error"});let E=`datasquirel_user_${r.id}_${n}`,N=await m()(`CREATE DATABASE IF NOT EXISTS \`${E}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!N?.affectedRows)return s.json({success:!1,msg:"Database Could Not be created!"});let h={user_id:r.id,db_name:i,db_slug:n,db_full_name:E,db_description:l,db_image:o};c&&p&&(h.active_clone="1",h.active_clone_parent_db=c),c&&b&&(h.active_data="1");let A=await S()({dbFullName:"datasquirel",tableName:"user_databases",data:h});if(A.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r.id}/main.json`,a=JSON.parse(f.readFileSync(e,"utf8")),u={dbName:i,dbSlug:n,dbFullName:E,dbDescription:l,dbImage:o,tables:[]};if(c)try{let e=a.filter(e=>e.dbFullName===c)[0];u.tables=e.tables,p&&(u.childDatabase=!0,u.childDatabaseDbFullName=c,b&&(u.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:E}):e.childrenDatabases=[{dbFullName:E}]);for(let e=0;e<u.tables.length;e++){let s=u.tables[e];await S()({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:r.id,db_id:A.insertId,db_slug:n,table_name:s.tableFullName,table_slug:s.tableName}})}}catch(e){d()({component:"/api/createUserDatabase/lines-117-159",message:e.message,user:r})}a.push(u),f.writeFileSync(`${e}`,JSON.stringify(a),"utf8"),c&&await D()({userId:r.id,targetDatabase:E});let _=await m()(`GRANT ALL PRIVILEGES ON ${E}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}' WITH GRANT OPTION`);if(_.error)throw Error(_.error);let N=await m()(`GRANT SELECT ON ${E}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}'`);if(N.error)throw Error(N.error);t?.mariadb_user&&t?.mariadb_pass&&await m()(`GRANT ALL PRIVILEGES ON ${E}.* TO '${t.mariadb_user}'@'${t.mariadb_host}'`),await m()("FLUSH PRIVILEGES"),s.json({success:!0})}catch(e){d()({component:"/api/createUserDatabase/main-catch-error",message:e.message,user:r}),s.json({success:!1,msg:"Database Files could not be written!"})}else s.json({success:!1,msg:"Database Could Not Be Recorded!"})}let A=(0,n.M)(a,"default"),R=(0,n.M)(a,"config"),O=new t.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/createUserDatabase",pathname:"/api/createUserDatabase",bundlePath:"",filename:""},userland:a})},6420:(e,s,r)=>{r(81630);let a=r(51348),t=r(72835),i=r(29021),n=r(31341);async function l(e,s,r,l){let{keyCookieName:u,csrfCookieName:o}=n();if(!e.cookies?.[u]?.match(/./))return null;let d=t({encryptedString:e.cookies[u]});if(!d)return null;let c=JSON.parse(d);if(!c.csrf_k||r&&!e.headers["x-csrf-auth"]?.match(RegExp(`${c.csrf_k}`)))return null;let _=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!_)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(r&&!i.existsSync(`${_}/${c.csrf_