dsql-admin/dsql-app/.local_dist/server/pages/api/createUserDatabase.js
Benjamin Toby d9d32a4643 Updates
2024-12-09 13:27:08 +01:00

1 line
5.5 KiB
JavaScript

"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,a)=>{a.r(s),a.d(s,{config:()=>R,default:()=>A,routeModule:()=>g});var r={};a.r(r),a.d(r,{default:()=>h});var t=a(89947),i=a(2706),n=a(96762),l=a(51348),o=a.n(l),u=a(48225),c=a.n(u),d=a(6420),_=a.n(d),p=a(47670),b=a.n(p),S=a(51926),m=a.n(S),E=a(19261),f=a.n(E);let D=a(29021);a(33873);let{execSync:N}=a(79646);async function h(e,s){if("POST"!==e.method)return s.json({msg:"Failed!"});let a=await _()(e,s,!0);if(!a)return s.json({success:!1,msg:"Unauthorized"});let r=await o()("SELECT * FROM users WHERE id = ?",[a.id]),t=r?.[0],{db_name:i,db_slug:n,db_description:l,db_image:u,duplicate:d,keepUpdated:p,keepDataUpdated:S}=e.body;if(n.match(/[^a-zA-Z0-9-_]/))return s.json({success:!1,msg:"Input Error"});let E=`datasquirel_user_${a.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:a.id,db_name:i,db_slug:n,db_full_name:E,db_description:l,db_image:u};d&&p&&(h.active_clone="1",h.active_clone_parent_db=d),d&&S&&(h.active_data="1");let A=await b()({dbFullName:"datasquirel",tableName:"user_databases",data:h});if(A.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${a.id}/main.json`,r=JSON.parse(D.readFileSync(e,"utf8")),o={dbName:i,dbSlug:n,dbFullName:E,dbDescription:l,dbImage:u,tables:[]};if(d)try{let e=r.filter(e=>e.dbFullName===d)[0];o.tables=e.tables,p&&(o.childDatabase=!0,o.childDatabaseDbFullName=d,S&&(o.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:E}):e.childrenDatabases=[{dbFullName:E}]);for(let e=0;e<o.tables.length;e++){let s=o.tables[e];await b()({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:a.id,db_id:A.insertId,db_slug:n,table_name:s.tableFullName,table_slug:s.tableName}})}}catch(e){c()({component:"/api/createUserDatabase/lines-117-159",message:e.message,user:a})}r.push(o),D.writeFileSync(`${e}`,JSON.stringify(r),"utf8"),d&&await f()({userId:a.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){c()({component:"/api/createUserDatabase/main-catch-error",message:e.message,user:a}),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)(r,"default"),R=(0,n.M)(r,"config"),g=new t.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/createUserDatabase",pathname:"/api/createUserDatabase",bundlePath:"",filename:""},userland:r})},6420:(e,s,a)=>{a(81630);let r=a(51348),t=a(72835),i=a(29021),n=a(31341);async function l(e,s,a,l){let{keyCookieName:o,csrfCookieName:u}=n();if(!e.cookies?.[o]?.match(/./))return null;let c=t({encryptedString:e.cookies[o]});if(!c)return null;let d=JSON.parse(c);if(!d.csrf_k||a&&!e.headers["x-csrf-auth"]?.match(RegExp(`${d.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(a&&!i.existsSync(`${_}/${d.csrf_k}`))return null;if(0==d.verification_status&&!a){let e=await r(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&s.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=l},31341:e=>{e.exports=function(e){let s=process.env.DSQL_COOKIES_PREFIX||"dsql_",a=process.env.DSQL_COOKIES_KEY_NAME||"key",r=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",t=e?.database||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),i=s;e?.userId&&(i+=`user_${e.userId}_`),t&&(i+=`${t}_`),i+=a;let n=s;return e?.userId&&(n+=`user_${e.userId}_`),t&&(n+=`${t}_`),{keyCookieName:i,csrfCookieName:n+=r}}},51926:(e,s,a)=>{let r=a(29021),t=a(48225),i=a(45356);e.exports=async function(e){let s;"production".match(/dev/)&&r.appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{s=await i(e)}catch(e){t({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return s||null}},45356:(e,s,a)=>{let r=a(9973),t=a(21580),i=r({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,charset:"utf8mb4",ssl:t()}});e.exports=function(...e){try{return new Promise((s,a)=>{i.query(...e).then(e=>{i.end(),s(JSON.parse(JSON.stringify(e)))}).catch(e=>{i.end(),s({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}}};var s=require("../../webpack-api-runtime.js");s.C(e);var a=e=>s(s.s=e),r=s.X(0,[4050,2451],()=>a(34932));module.exports=r})();