"use strict";(()=>{var e={};e.id=5319,e.ids=[5319],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9288:e=>{e.exports=require("sharp")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},72944:(e,r,t)=>{t.r(r),t.d(r,{config:()=>b,default:()=>S,routeModule:()=>A});var a={};t.r(a),t.d(a,{config:()=>f,default:()=>h});var i=t(89947),n=t(2706),s=t(96762),l=t(29021),o=t.n(l),u=t(33873),d=t.n(u),c=t(98e3),g=t(61932),m=t(96551),p=t(67619),_=t(56081);let f={api:{bodyParser:{sizeLimit:"50mb"}}};async function h(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});try{let t=e.headers.authorization;if(!t)throw Error("No Authorization Found!");let a=(0,_.A)({key:t,user_id:String(e.query.user_id),media:!0});if(!a?.user_id)throw Error(a?.error||"Api Credentials invalid!");let{user_id:i,full_access:n}=a;if(!n)return r.json({success:!1,msg:"Unauthorized"});let{imageData:s,imageName:l,mimeType:u,thumbnailSize:p,folder:f,isPrivate:h}=e.body,S=process.env.DSQL_STATIC_SERVER_DIR;if(!S)throw console.log("Static File ENV not Found!"),Error("No Static Path!");if(f){let e=d().join(S,`images/user-images/user-${i}/${f}`);o().existsSync(e)||o().mkdirSync(e,{recursive:!0})}let b=u?.match(/jpeg/i)?"jpeg":u?.match(/png/i)?"png":u?.match(/webp/i)?"webp":u?.match(/svg/i)?"svg":"jpg",A=await (0,m.A)({imageName:l.replace(/ /g,"-").replace(/\.{1,}|\//g,""),imageSourceBase64:s,userId:i,mimeType:b,thumbnailSize:p,folder:f,isPrivate:h});if(!A)throw Error("Write Image Failed in add-media API route");let{urlPath:y,urlThumbnailPath:E}=A;await (0,g.A)({dbFullName:"datasquirel",tableName:"user_media",identifierColumnName:"media_url",identifierValue:y}),await (0,c.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:i,media_name:l,media_url:y,media_thumbnail_url:E,folder:f||""}}),r.json({success:!0,payload:{urlPath:y,urlThumbnailPath:E}})}catch(t){(0,p.A)({component:"/api/query/add-image/main-catch-error",message:t.message,user:{},req:e}),r.json({success:!1,msg:"Add Image Error!",error:t.message})}}let S=(0,s.M)(a,"default"),b=(0,s.M)(a,"config"),A=new i.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/query/[user_id]/add-image",pathname:"/api/query/[user_id]/add-image",bundlePath:"",filename:""},userland:a})},56081:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(29021),i=t.n(a),n=t(95969);let s=({key:e,database:r,table:t,user_id:a,media:s})=>{if(!e||!a)return null;try{let l=process.env.DSQL_API_KEYS_PATH;if(!l)throw Error("process.env.DSQL_API_KEYS_PATH variable not found");let o=(0,n.A)({encryptedString:e}),u=JSON.parse(o||""),d=i().existsSync(`${l}/${u.sign}`);if(String(u.user_id)!==String(a)||!d)return null;if(!u.target_database||s)return u;if(!r&&u.target_database)return null;let c=u.target_database?.split(",").includes(String(r));if(c&&!u.target_table)return u;if(c&&!t&&u.target_table)return null;if(u.target_table?.split(",").includes(String(t)))return u;return null}catch(e){return console.log(`api-cred ERROR: ${e.message}`),{error:`api-cred ERROR: ${e.message}`}}}},61932:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(52939),i=t(64292);async function n({dbContext:e,dbFullName:r,tableName:t,identifierColumnName:n,identifierValue:s,forceLocal:l}){try{let o=!!l||(0,a.A)({dbContext:e,dbFullName:r}),u=o?global.DSQL_DB_CONN:global.DSQL_FULL_ACCESS_DB_CONN||global.DSQL_DB_CONN;o?global.DSQL_DB_CONN:global.DSQL_READ_ONLY_DB_CONN||global.DSQL_DB_CONN;let d=`DELETE FROM ${o?"":`\`${r}\`.`}\`${t}\` WHERE \`${n}\`=?`;return await (0,i.A)(u,d,[s])}catch(e){return null}}},95969:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(55511),i=t(79428),n=t(31542);function s({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let{key:s,salt:l,keyLen:o,algorithm:u,bufferAllocSize:d}=(0,n.A)({encryptionKey:r});if(!s?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!l?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let c=(0,a.scryptSync)(s,l,o),g=i.Buffer.alloc(d,0),m=(0,a.createDecipheriv)(u,c,g);try{let r=m.update(e,"hex","utf8");return r+=m.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}}};var r=require("../../../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),a=r.X(0,[5194],()=>t(72944));module.exports=a})();