dsql-admin/dsql-app/.local_dist/server/pages/api/addUserMedia.js
Benjamin Toby 1564c2811a Updates
2025-01-14 08:24:54 +01:00

16 lines
9.3 KiB
JavaScript

"use strict";(()=>{var e={};e.id=979,e.ids=[979],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")},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")},27973:(e,r,t)=>{t.r(r),t.d(r,{config:()=>g,default:()=>v,routeModule:()=>E});var a={};t.r(a),t.d(a,{config:()=>h,default:()=>S});var i=t(89947),s=t(2706),l=t(96762),o=t(29021),n=t.n(o),u=t(33873),d=t.n(u),c=t(98e3),m=t(96551),_=t(45185),p=t(67619),f=t(69842);let h={api:{responseLimit:"1mb",bodyParser:{sizeLimit:"200mb"}}};async function S(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await (0,f.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let a=e.body,{media:i}=a,s=a.folder,l=a.type;"production".match(/production/);let o=process.env.DSQL_STATIC_SERVER_DIR;if(!o)return console.log("Static File ENV not Found!"),r.json({success:!1,msg:"No Static File Path"});if(s){let e=d().join(o,`images/user-images/user-${t.id}/${s}`);n().existsSync(e)||n().mkdirSync(e,{recursive:!0});let r=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/media/${s}`;n().existsSync(r)||n().mkdirSync(r,{recursive:!0})}try{let e=[];for(let r=0;r<i.length;r++){let a=i[r];if(l?.match(/image/i)){let{imageBase64:r,imageName:i,imageSize:l,mimeType:o}=a,n=await (0,m.A)({imageName:i.replace(/ /g,"-").replace(/\.{1,}|\//g,""),imageSourceBase64:r,userId:t.id,folder:s,mimeType:o,isPrivate:a.private});if(!n){console.log("Image Write Failed!");continue}let{urlPath:u,urlThumbnailPath:d,urlRelativePath:_,urlThumbnailRelativePath:p}=n;await (0,c.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:i.replace(/ /g,"-"),media_url:u,media_type:"image",media_thumbnail_url:d,media_path:_,media_thumbnail_path:p,folder:s,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:u}),e.push({media_name:i.replace(/ /g,"-"),media_url:u,media_thumbnail_url:d})}else if(l?.match(/file/i)){let{fileBase64:r,fileSize:i,fileType:l}=a,o=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=(0,_.A)({isPrivate:a.private,userId:t.id,folder:s});if(!u)continue;let{fileRootPath:d,urlRootPath:m,relativePath:p}=u,f=l?.match(/pdf/)?".pdf":l?.match(/xlsx/)?".xlsx":l?.match(/csv/)?".csv":l?.match(/json/)?".json":null;if(!f)continue;let h=d+o+f,S=m+o+f,v=p+o+f;n().writeFileSync(h,r,"base64"),await (0,c.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:o,media_url:S,media_thumbnail_url:S,media_path:v,media_thumbnail_path:v,media_type:"file/"+f.replace(/\./,""),folder:s,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:S}),e.push({media_name:o,media_url:S,media_thumbnail_url:S})}else if(l?.match(/video/i)){let{fileBase64:r,fileSize:i,fileType:l}=a,o=a.fileName.replace(/ /g,"-").replace(/\./g,""),u=(0,_.A)({isPrivate:a.private,userId:t.id,folder:s,video:!0});if(!u)continue;let{fileRootPath:d,urlRootPath:m,relativePath:p}=u,f=l?.match(/mp4/)?".mp4":null;if(!f)continue;let h=d+o+f,S=m+o+f,v=p+o+f;n().writeFileSync(h,r,"base64"),await (0,c.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:t.id,media_name:o,media_url:S,media_thumbnail_url:S,media_path:v,media_thumbnail_path:v,media_type:"video/"+f.replace(/\./,""),folder:s,private:a.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:S}),e.push({media_name:o,media_url:S,media_thumbnail_url:S})}}r.json({success:!0,mediaData:e})}catch(e){console.log(`Add User Media Error: ${e.message}`),(0,p.A)({component:"/api/addUserMedia/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Database Files could not be written!"})}}let v=(0,l.M)(a,"default"),g=(0,l.M)(a,"config"),E=new i.PagesAPIRouteModule({definition:{kind:s.A.PAGES_API,page:"/api/addUserMedia",pathname:"/api/addUserMedia",bundlePath:"",filename:""},userland:a})},96551:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(9288),i=t.n(a),s=t(67619),l=t(45185);async function o({imageSourceBase64:e,imageName:r,userId:t,mimeType:a,thumbnailSize:o,folder:n,isPrivate:u}){try{let s=Buffer.from(e,"base64"),d=o?parseInt(o.toString()):400,c=i()(s),m=i()(s),_=(0,l.A)({isPrivate:u,userId:t,folder:n});if(!_)return null;let{fileRootPath:p,urlRootPath:f,relativePath:h}=_,{width:S,height:v,format:g}=await c.metadata(),E=a||g;S&&v&&S>1800&&c.resize(1800,Math.round(1800/S*v),{fit:"cover"}),c.toFormat(E,{quality:80}),await c.metadata();let y=`${r}.${E}`;if(await c.toFile(p+y),S&&v&&S>d){let e=d/S;m.resize(d,Math.round(v*e),{fit:"cover"})}else S&&v&&m.resize(150,Math.round(150/S*v),{fit:"cover"});m.toFormat(E,{quality:80});let A=`${r}_thumbnail.${E}`;return await m.toFile(p+A),{urlPath:f+y,urlThumbnailPath:f+A,urlRelativePath:h+y,urlThumbnailRelativePath:h+A}}catch(e){return console.log("Write Image to Disk error =>",e.message),(0,s.A)({component:"functions/backend/fsWriteImageToDiskFromBase64",message:e.message}),null}}},45185:(e,r,t)=>{t.d(r,{A:()=>o});var a=t(29021),i=t.n(a),s=t(33873),l=t.n(s);function o({isPrivate:e,userId:r,folder:t,video:a,pathOnly:s}){"production".match(/production/);let o=process.env.DSQL_STATIC_SERVER_DIR;if(!o)return console.log("Static File ENV not Found!"),null;let n=e?`@/${a?"videos":"media"}/${t?t+"/":""}`:a?`/videos/user-videos/user-${r}/${t?t+"/":""}`:`/images/user-images/user-${r}/${t?t+"/":""}`,u=e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r}/media/${t?t+"/":""}`:l().join(o,n);return i().existsSync(u)||s||i().mkdirSync(u,{recursive:!0}),{fileRootPath:u,urlRootPath:e?`@/media/${t?t+"/":""}`:`${process.env.DSQL_STATIC_HOST}${n}`,relativePath:n}}},69842:(e,r,t)=>{t.d(r,{A:()=>n});var a=t(48786),i=t(95969),s=t(29021),l=t.n(s),o=t(89159);async function n(e,r,t){try{let{keyCookieName:s,csrfCookieName:n}=(0,o.A)();if(!e.cookies?.[s]?.match(/./))throw Error("Request Cookies not Found!");let u=(0,i.A)({encryptedString:e.cookies[s]});if(!u)throw Error("Couldn't decrypt auth cookie!");let d=JSON.parse(u);if(!d.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(d.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!l().existsSync(`${c}/${d.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==d.verification_status&&!t){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(d?.date&&Date.now()-d.date>6048e5)throw Error("Key expired!");return d}catch(r){return console.log(`userAuth Error for ${e.url}: ${r.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>a});function a(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",i=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",s=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),l=r;e?.userId&&(l+=`user_${e.userId}_`),s&&(l+=`${s}_`),l+=t;let o=r;e?.userId&&(o+=`user_${e.userId}_`),s&&(o+=`${s}_`),o+=a;let n=r;return e?.userId&&(n+=`user_${e.userId}_`),s&&(n+=`${s}_`),{keyCookieName:l,csrfCookieName:o,oneTimeCodeName:n+=i}}},67619:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(29021),i=t.n(a);async function s({user:e,message:r,component:t,noMail:a,req:s}){let l=new Date,o=(()=>{if(!s)return null;try{let e=s.headers["x-forwarded-for"],r=s.headers["x-real-ip"],t=s.headers["cf-connecting-ip"],a=Array.isArray(e)?e[0]:e?.split(",")[0],i=t||a||r||s.socket.remoteAddress;if(!i)return null;return String(i)}catch(e){return null}})();try{let a=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(a+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),s?.url&&(a+=`
URL: ${s.url}`),s?.body&&(a+=`
Request Body: ${JSON.stringify(s.body,null,4)}`),o&&(a+=`
IP: ${o}`),a+=`
Date: ${l.toDateString()}
========================================`,i().existsSync("./.tmp/error.log")||i().writeFileSync("./.tmp/error.log","","utf-8");let n=i().readFileSync("./.tmp/error.log","utf-8");i().writeFileSync("./.tmp/error.log",a),i().appendFileSync("./.tmp/error.log",`
${n}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>s});var a=t(55511),i=t(79428);function s({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let s=r||process.env.DSQL_ENCRYPTION_PASSWORD,l=t||process.env.DSQL_ENCRYPTION_SALT,o=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;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 n=(0,a.scryptSync)(s,l,o),u=i.Buffer.alloc(16,0),d=(0,a.createDecipheriv)("aes-192-cbc",n,u);try{let r=d.update(e,"hex","utf8");return r+=d.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,[1251],()=>t(27973));module.exports=a})();