"use strict";(()=>{var e={};e.id=2678,e.ids=[2678],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},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")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},10304:(e,r,t)=>{t.r(r),t.d(r,{config:()=>f,default:()=>S,routeModule:()=>E});var s={};t.r(s),t.d(s,{default:()=>m});var i=t(89947),a=t(2706),n=t(96762),o=t(51348),c=t.n(o),u=t(6420),l=t.n(u),d=t(33873),_=t.n(d);let p=t(29021);async function m(e,r){if("POST"!==e.method)return r.json({success:!1,msg:"Failed!"});let t=await l()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let s=e.body;if(s.media_url?.match(/\.\./)||s.folder?.match(/\.\./)||s.id?.toString()?.match(/\.\./))return r.json({success:!1,msg:"Error!",error:"Invalid url"});try{let e=s?.private==1,i=e?s?.media_path?.replace(/\@\/media/i,"")||s?.media_url.replace(/\@\/media/i,""):s?.media_path?.match(/\/user-images\/user-.*/)||s?.media_url?.match(/\/user-images\/user-.*/),a=s.folder?`/${s.folder}`:"",n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)throw console.log("Static File ENV not Found!"),Error("No Static Path!");let o=_().join(n,"images"),u=`${process.env.DSQL_STATIC_HOST}/images/user-images/user-${t.id}`,l=`/images/user-images/user-${t.id}`,d=(e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/media`:o)+i,m=e?`${o}/user-images/user-${t.id}`:`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/media`;if(!p.existsSync(m))try{p.mkdirSync(m,{recursive:!0})}catch(e){console.log("LINE 88 error:",e.message)}a&&a.split("/").reduce((e,r)=>{let t=e?.match(/./)?`${e}/${r}`:m;return p.existsSync(t)||p.mkdirSync(t,{recursive:!0}),t},"");let S=d?.match(/[^\/]+$/)?.[0];if(!S)throw Error("No File Name Detected!");let f=S.split(".")[0],E=S.split(".")[1],v=e?`${u}${a?a+"/":"/"}${S}`:`@/media${a?a+"/":"/"}${S}`,h=e?`${l}${a?a+"/":"/"}${S}`:`@/media${a?a+"/":"/"}${S}`,$=m+a+"/"+S;p.copyFileSync(d,$),p.unlinkSync(d);let P=f+"_thumbnail."+E,g=d.replace(S,P),D=e?`${u}${a?a+"/":"/"}${P}`:`@/media${a?a+"/":"/"}${P}`,y=e?`${l}${a?a+"/":"/"}${P}`:`@/media${a?a+"/":"/"}${P}`;try{p.copyFileSync(g,m+a+"/"+P),p.unlinkSync(g)}catch(e){}await c()("UPDATE user_media SET private = ?, media_url= ?, media_thumbnail_url= ?, media_path = ?, media_thumbnail_path = ? WHERE id = ?",[e?0:1,v,D,h,y,s.id]),r.json({success:!0,msg:"Success!"})}catch(e){console.log(e.message),r.json({success:!1,msg:"Error!",error:e})}}let S=(0,n.M)(s,"default"),f=(0,n.M)(s,"config"),E=new i.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/media/addPrivateMedia",pathname:"/api/media/addPrivateMedia",bundlePath:"",filename:""},userland:s})},6420:(e,r,t)=>{t(81630);let s=t(51348),i=t(72835),a=t(29021),n=t(31341);async function o(e,r,t,o){let{keyCookieName:c,csrfCookieName:u}=n();if(!e.cookies?.[c]?.match(/./))return null;let l=i({encryptedString:e.cookies[c]});if(!l)return null;let d=JSON.parse(l);if(!d.csrf_k||t&&!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(t&&!a.existsSync(`${_}/${d.csrf_k}`))return null;if(0==d.verification_status&&!t){let e=await s(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return d?.date&&Date.now()-d.date>6048e5?null:d}e.exports=o},31341:e=>{e.exports=function(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",i=r;e?.userId&&(i+=`user_${e.userId}_`),e?.database&&(i+=`${e.database}_`),i+=t;let a=r;return e?.userId&&(a+=`user_${e.userId}_`),e?.database&&(a+=`${e.database}_`),{keyCookieName:i,csrfCookieName:a+=s}}},72835:(e,r,t)=>{let{scryptSync:s,createDecipheriv:i}=t(55511),{Buffer:a}=t(79428);e.exports=({encryptedString:e,encryptionKey:r,encryptionSalt:t})=>{if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,o=t||process.env.DSQL_ENCRYPTION_SALT,c=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!o?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=i("aes-192-cbc",s(n,o,c),a.alloc(16,0));try{let r=u.update(e,"hex","utf8");return r+=u.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},51348:(e,r,t)=>{let s=t(9973),i=t(21580),a=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:i()}});async function n(...e){try{let r=await a.query(...e);return await a.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=n},21580:(e,r,t)=>{let s=t(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!s.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:s.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var t;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(t||(t={}))},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../../webpack-api-runtime.js");r.C(e);var t=r(r.s=10304);module.exports=t})();