dsql-admin/dsql-app/.local_dist/server/pages/api/createUserDatabase.js
Benjamin Toby 7a06e83a46 Updates
2024-12-08 20:56:57 +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:()=>A,default:()=>R,routeModule:()=>g});var r={};a.r(r),a.d(r,{default:()=>N});var t=a(89947),i=a(2706),n=a(96762),l=a(51348),o=a.n(l),u=a(48225),d=a.n(u),c=a(6420),_=a.n(c),b=a(47670),p=a.n(b),S=a(51926),m=a.n(S),E=a(19261),f=a.n(E);let D=a(29021);a(33873);let{execSync:h}=a(79646);async function N(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:c,keepUpdated:b,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}`,h=await m()(`CREATE DATABASE IF NOT EXISTS \`${E}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!h?.affectedRows)return s.json({success:!1,msg:"Database Could Not be created!"});let N={user_id:a.id,db_name:i,db_slug:n,db_full_name:E,db_description:l,db_image:u};c&&b&&(N.active_clone="1",N.active_clone_parent_db=c),c&&S&&(N.active_data="1");let R=await p()({dbFullName:"datasquirel",tableName:"user_databases",data:N});if(R.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(c)try{let e=r.filter(e=>e.dbFullName===c)[0];o.tables=e.tables,b&&(o.childDatabase=!0,o.childDatabaseDbFullName=c,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 p()({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:a.id,db_id:R.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:a})}r.push(o),D.writeFileSync(`${e}`,JSON.stringify(r),"utf8"),c&&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 h=await m()(`GRANT SELECT ON ${E}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}'`);if(h.error)throw Error(h.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:a}),s.json({success:!1,msg:"Database Files could not be written!"})}else s.json({success:!1,msg:"Database Could Not Be Recorded!"})}let R=(0,n.M)(r,"default"),A=(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 d=t({encryptedString:e.cookies[o]});if(!d)return null;let c=JSON.parse(d);if(!c.csrf_k||a&&!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(a&&!i.existsSync(`${_}/${c.csrf_k}`))return null;if(0==c.verification_status&&!a){let e=await r(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&s.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}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=s;e?.userId&&(t+=`user_${e.userId}_`),e?.database&&(t+=`${e.database}_`),t+=a;let i=s;return e?.userId&&(i+=`user_${e.userId}_`),e?.database&&(i+=`${e.database}_`),{keyCookieName:t,csrfCookieName:i+=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})();