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

1 line
5.8 KiB
JavaScript
Raw Normal View History

2025-01-14 07:24:54 +00:00
"use strict";(()=>{var e={};e.id=8893,e.ids=[8893],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")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},8325:(e,r,a)=>{a.r(r),a.d(r,{config:()=>f,default:()=>A,routeModule:()=>p});var s={};a.r(s),a.d(s,{default:()=>m});var t=a(89947),i=a(2706),o=a(96762),n=a(29021),u=a.n(n),d=a(48786),l=a(67619),c=a(69842),_=a(98e3),E=a(14160),b=a(76856);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let a=await (0,c.A)(e,r,!0);if(!a)return r.json({success:!1,msg:"Unauthorized"});let s=await (0,d.A)("SELECT * FROM users WHERE id = ?",[a.id]),t=s?.[0],{db_name:i,db_slug:o,db_description:n,db_image:m,duplicate:A,keepUpdated:f,keepDataUpdated:p}=e.body;if(o.match(/[^a-zA-Z0-9-_]/))return r.json({success:!1,msg:"Input Error"});let S=`datasquirel_user_${a.id}_${o}`,D=await (0,E.A)(`CREATE DATABASE IF NOT EXISTS \`${S}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!D?.affectedRows)return r.json({success:!1,msg:"Database Could Not be created!"});let h={user_id:a.id,db_name:i,db_slug:o,db_full_name:S,db_description:n,db_image:m};A&&f&&(h.active_clone="1",h.active_clone_parent_db=A),A&&p&&(h.active_data="1");let N=await (0,_.A)({dbFullName:"datasquirel",tableName:"user_databases",data:h});if(N.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${a.id}/main.json`,s=JSON.parse(u().readFileSync(e,"utf8")),d={dbName:i,dbSlug:o,dbFullName:S,dbDescription:n,dbImage:m,tables:[]};if(A)try{let e=s.filter(e=>e.dbFullName===A)[0];d.tables=e.tables,f&&(d.childDatabase=!0,d.childDatabaseDbFullName=A,p&&(d.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:S}):e.childrenDatabases=[{dbFullName:S}]);for(let e=0;e<d.tables.length;e++){let r=d.tables[e];await (0,_.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:a.id,db_id:N.insertId,db_slug:o,table_name:r.tableFullName,table_slug:r.tableName}})}}catch(e){(0,l.A)({component:"/api/createUserDatabase/lines-117-159",message:e.message,user:a})}s.push(d),u().writeFileSync(`${e}`,JSON.stringify(s),"utf8"),A&&await (0,b.A)({userId:a.id,targetDatabase:S});let c=await (0,E.A)(`GRANT ALL PRIVILEGES ON ${S}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}' WITH GRANT OPTION`);if(c.error)throw Error(c.error);let D=await (0,E.A)(`GRANT SELECT ON ${S}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}'`);if(D.error)throw Error(D.error);t?.mariadb_user&&t?.mariadb_pass&&await (0,E.A)(`GRANT ALL PRIVILEGES ON ${S}.* TO '${t.mariadb_user}'@'${t.mariadb_host}'`),await (0,E.A)("FLUSH PRIVILEGES"),r.json({success:!0})}catch(e){(0,l.A)({component:"/api/createUserDatabase/main-catch-error",message:e.message,user:a}),r.json({success:!1,msg:"Database Files could not be written!"})}else r.json({success:!1,msg:"Database Could Not Be Recorded!"})}let A=(0,o.M)(s,"default"),f=(0,o.M)(s,"config"),p=new t.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/createUserDatabase",pathname:"/api/createUserDatabase",bundlePath:"",filename:""},userland:s})},69842:(e,r,a)=>{a.d(r,{A:()=>u});var s=a(48786),t=a(95969),i=a(29021),o=a.n(i),n=a(89159);async function u(e,r,a){try{let{keyCookieName:i,csrfCookieName:u}=(0,n.A)();if(!e.cookies?.[i]?.match(/./))throw Error("Request Cookies not Found!");let d=(0,t.A)({encryptedString:e.cookies[i]});if(!d)throw Error("Couldn't decrypt auth cookie!");let l=JSON.parse(d);if(!l.csrf_k)throw Error("No `csrf_k` present in user Object!");if(a&&!Object.keys(e.headers).find(e=>e.includes(l.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(a&&!o().existsSync(`${c}/${l.