dsql-admin/dsql-app/.local_dist/server/pages/api/createUserDatabase.js
Benjamin Toby e0a030f10d Updates
2025-01-13 09:00:21 +01:00

1 line
5.5 KiB
JavaScript

"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")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},8325:(e,a,s)=>{s.r(a),s.d(a,{config:()=>p,default:()=>S,routeModule:()=>f});var r={};s.r(r),s.d(r,{default:()=>E});var t=s(89947),i=s(2706),n=s(96762),l=s(48786),u=s(67619),d=s(69842),o=s(98e3),c=s(14160),_=s(76856);let b=s(29021);s(33873);let{execSync:m}=s(79646);async function E(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let s=await (0,d.A)(e,a,!0);if(!s)return a.json({success:!1,msg:"Unauthorized"});let r=await (0,l.A)("SELECT * FROM users WHERE id = ?",[s.id]),t=r?.[0],{db_name:i,db_slug:n,db_description:m,db_image:E,duplicate:S,keepUpdated:p,keepDataUpdated:f}=e.body;if(n.match(/[^a-zA-Z0-9-_]/))return a.json({success:!1,msg:"Input Error"});let A=`datasquirel_user_${s.id}_${n}`,D=await (0,c.A)(`CREATE DATABASE IF NOT EXISTS \`${A}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!D?.affectedRows)return a.json({success:!1,msg:"Database Could Not be created!"});let N={user_id:s.id,db_name:i,db_slug:n,db_full_name:A,db_description:m,db_image:E};S&&p&&(N.active_clone="1",N.active_clone_parent_db=S),S&&f&&(N.active_data="1");let O=await (0,o.A)({dbFullName:"datasquirel",tableName:"user_databases",data:N});if(O.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${s.id}/main.json`,r=JSON.parse(b.readFileSync(e,"utf8")),l={dbName:i,dbSlug:n,dbFullName:A,dbDescription:m,dbImage:E,tables:[]};if(S)try{let e=r.filter(e=>e.dbFullName===S)[0];l.tables=e.tables,p&&(l.childDatabase=!0,l.childDatabaseDbFullName=S,f&&(l.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:A}):e.childrenDatabases=[{dbFullName:A}]);for(let e=0;e<l.tables.length;e++){let a=l.tables[e];await (0,o.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:s.id,db_id:O.insertId,db_slug:n,table_name:a.tableFullName,table_slug:a.tableName}})}}catch(e){(0,u.default)({component:"/api/createUserDatabase/lines-117-159",message:e.message,user:s})}r.push(l),b.writeFileSync(`${e}`,JSON.stringify(r),"utf8"),S&&await (0,_.A)({userId:s.id,targetDatabase:A});let d=await (0,c.A)(`GRANT ALL PRIVILEGES ON ${A}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}' WITH GRANT OPTION`);if(d.error)throw Error(d.error);let D=await (0,c.A)(`GRANT SELECT ON ${A}.* 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,c.A)(`GRANT ALL PRIVILEGES ON ${A}.* TO '${t.mariadb_user}'@'${t.mariadb_host}'`),await (0,c.A)("FLUSH PRIVILEGES"),a.json({success:!0})}catch(e){(0,u.default)({component:"/api/createUserDatabase/main-catch-error",message:e.message,user:s}),a.json({success:!1,msg:"Database Files could not be written!"})}else a.json({success:!1,msg:"Database Could Not Be Recorded!"})}let S=(0,n.M)(r,"default"),p=(0,n.M)(r,"config"),f=new t.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/createUserDatabase",pathname:"/api/createUserDatabase",bundlePath:"",filename:""},userland:r})},69842:(e,a,s)=>{s.d(a,{A:()=>u});var r=s(48786),t=s(95969),i=s(29021),n=s.n(i),l=s(89159);async function u(e,a,s){let{keyCookieName:i,csrfCookieName:u}=(0,l.A)();if(!e.cookies?.[i]?.match(/./))return null;let d=(0,t.A)({encryptedString:e.cookies[i]});if(!d)return null;let o=JSON.parse(d);if(!o.csrf_k||s&&!Object.keys(e.headers).find(e=>e.includes(o.csrf_k)))return null;let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(s&&!n().existsSync(`${c}/${o.csrf_k}`))return null;if(0==o.verification_status&&!s){let e=await (0,r.A)(`SELECT verification_status FROM users WHERE id='${o.id}'`);e&&e[0]&&1==e[0].verification_status&&a.setHeader("Set-Cookie",["user_refresh=1"])}return o?.date&&Date.now()-o.date>6048e5?null:o}},89159:(e,a,s)=>{s.d(a,{A:()=>r});function r(e){let a=process.env.DSQL_COOKIES_PREFIX||"dsql_",s=process.env.DSQL_COOKIES_KEY_NAME||"key",r=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",t=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",i=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=a;e?.userId&&(n+=`user_${e.userId}_`),i&&(n+=`${i}_`),n+=s;let l=a;e?.userId&&(l+=`user_${e.userId}_`),i&&(l+=`${i}_`),l+=r;let u=a;return e?.userId&&(u+=`user_${e.userId}_`),i&&(u+=`${i}_`),{keyCookieName:n,csrfCookieName:l,oneTimeCodeName:u+=t}}},14160:(e,a,s)=>{s.d(a,{A:()=>l});var r=s(29021),t=s.n(r),i=s(67619),n=s(47546);async function l(e){let a;"production".match(/dev/)&&t().appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{a=await (0,n.A)(e)}catch(e){(0,i.default)({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return a||null}},47546:(e,a,s)=>{s.d(a,{A:()=>r});function r(...e){let a=global.DSQL_DB_CONN;try{return new Promise((s,r)=>{a.query(...e).then(e=>{a.end(),s(JSON.parse(JSON.stringify(e)))}).catch(e=>{a.end(),s({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var s=e=>a(a.s=e),r=a.X(0,[1251,2282],()=>s(8325));module.exports=r})();