dsql-admin/dsql-app/.local_dist/server/pages/api/addUserMedia.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

16 lines
7.1 KiB
JavaScript

"use strict";(()=>{var e={};e.id=979,e.ids=[979],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},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,a,r)=>{r.r(a),r.d(a,{config:()=>S,default:()=>v,routeModule:()=>y});var i={};r.r(i),r.d(i,{config:()=>h,default:()=>g});var t=r(89947),l=r(2706),s=r(96762),n=r(29021),o=r.n(n),u=r(33873),d=r.n(u),m=r(98e3),c=r(96551),p=r(45185),_=r(67619),f=r(69842);let h={api:{responseLimit:"1mb",bodyParser:{sizeLimit:"200mb"}}};async function g(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let r=await (0,f.A)(e,a,!0);if(!r)return a.json({success:!1,msg:"Unauthorized"});let i=e.body,{media:t}=i,l=i.folder,s=i.type;"production".match(/production/);let n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),a.json({success:!1,msg:"No Static File Path"});if(l){let e=d().join(n,`images/user-images/user-${r.id}/${l}`);o().existsSync(e)||o().mkdirSync(e,{recursive:!0});let a=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${r.id}/media/${l}`;o().existsSync(a)||o().mkdirSync(a,{recursive:!0})}try{let e=[];for(let a=0;a<t.length;a++){let i=t[a];if(s?.match(/image/i)){let{imageBase64:a,imageName:t,imageSize:s,mimeType:n}=i,o=await (0,c.A)({imageName:t.replace(/ /g,"-").replace(/\.{1,}|\//g,""),imageSourceBase64:a,userId:r.id,folder:l,mimeType:n,isPrivate:i.private});if(!o){console.log("Image Write Failed!");continue}let{urlPath:u,urlThumbnailPath:d,urlRelativePath:p,urlThumbnailRelativePath:_}=o;await (0,m.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:r.id,media_name:t.replace(/ /g,"-"),media_url:u,media_type:"image",media_thumbnail_url:d,media_path:p,media_thumbnail_path:_,folder:l,private:i.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:u}),e.push({media_name:t.replace(/ /g,"-"),media_url:u,media_thumbnail_url:d})}else if(s?.match(/file/i)){let{fileBase64:a,fileSize:t,fileType:s}=i,n=i.fileName.replace(/ /g,"-").replace(/\./g,""),u=(0,p.A)({isPrivate:i.private,userId:r.id,folder:l});if(!u)continue;let{fileRootPath:d,urlRootPath:c,relativePath:_}=u,f=s?.match(/pdf/)?".pdf":s?.match(/xlsx/)?".xlsx":s?.match(/csv/)?".csv":s?.match(/json/)?".json":null;if(!f)continue;let h=d+n+f,g=c+n+f,v=_+n+f;o().writeFileSync(h,a,"base64"),await (0,m.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:r.id,media_name:n,media_url:g,media_thumbnail_url:g,media_path:v,media_thumbnail_path:v,media_type:"file/"+f.replace(/\./,""),folder:l,private:i.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:g}),e.push({media_name:n,media_url:g,media_thumbnail_url:g})}else if(s?.match(/video/i)){let{fileBase64:a,fileSize:t,fileType:s}=i,n=i.fileName.replace(/ /g,"-").replace(/\./g,""),u=(0,p.A)({isPrivate:i.private,userId:r.id,folder:l,video:!0});if(!u)continue;let{fileRootPath:d,urlRootPath:c,relativePath:_}=u,f=s?.match(/mp4/)?".mp4":null;if(!f)continue;let h=d+n+f,g=c+n+f,v=_+n+f;o().writeFileSync(h,a,"base64"),await (0,m.A)({dbFullName:"datasquirel",tableName:"user_media",data:{user_id:r.id,media_name:n,media_url:g,media_thumbnail_url:g,media_path:v,media_thumbnail_path:v,media_type:"video/"+f.replace(/\./,""),folder:l,private:i.private?1:0},duplicateColumnName:"media_url",duplicateColumnValue:g}),e.push({media_name:n,media_url:g,media_thumbnail_url:g})}}a.json({success:!0,mediaData:e})}catch(e){console.log(`Add User Media Error: ${e.message}`),(0,_.A)({component:"/api/addUserMedia/catch-error",message:e.message,user:r}),a.json({success:!1,msg:"Database Files could not be written!"})}}let v=(0,s.M)(i,"default"),S=(0,s.M)(i,"config"),y=new t.PagesAPIRouteModule({definition:{kind:l.A.PAGES_API,page:"/api/addUserMedia",pathname:"/api/addUserMedia",bundlePath:"",filename:""},userland:i})},96551:(e,a,r)=>{r.d(a,{A:()=>n});var i=r(9288),t=r.n(i),l=r(67619),s=r(45185);async function n({imageSourceBase64:e,imageName:a,userId:r,mimeType:i,thumbnailSize:n,folder:o,isPrivate:u}){try{let l=Buffer.from(e,"base64"),d=n?parseInt(n.toString()):400,m=t()(l),c=t()(l),p=(0,s.A)({isPrivate:u,userId:r,folder:o});if(!p)return null;let{fileRootPath:_,urlRootPath:f,relativePath:h}=p,{width:g,height:v,format:S}=await m.metadata(),y=i||S;g&&v&&g>1800&&m.resize(1800,Math.round(1800/g*v),{fit:"cover"}),m.toFormat(y,{quality:80}),await m.metadata();let b=`${a}.${y}`;if(await m.toFile(_+b),g&&v&&g>d){let e=d/g;c.resize(d,Math.round(v*e),{fit:"cover"})}else g&&v&&c.resize(150,Math.round(150/g*v),{fit:"cover"});c.toFormat(y,{quality:80});let $=`${a}_thumbnail.${y}`;return await c.toFile(_+$),{urlPath:f+b,urlThumbnailPath:f+$,urlRelativePath:h+b,urlThumbnailRelativePath:h+$}}catch(e){return console.log("Write Image to Disk error =>",e.message),(0,l.A)({component:"functions/backend/fsWriteImageToDiskFromBase64",message:e.message}),null}}},45185:(e,a,r)=>{r.d(a,{A:()=>n});var i=r(29021),t=r.n(i),l=r(33873),s=r.n(l);function n({isPrivate:e,userId:a,folder:r,video:i,pathOnly:l}){"production".match(/production/);let n=process.env.DSQL_STATIC_SERVER_DIR;if(!n)return console.log("Static File ENV not Found!"),null;let o=e?`@/${i?"videos":"media"}/${r?r+"/":""}`:i?`/videos/user-videos/user-${a}/${r?r+"/":""}`:`/images/user-images/user-${a}/${r?r+"/":""}`,u=e?`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${a}/media/${r?r+"/":""}`:s().join(n,o);return t().existsSync(u)||l||t().mkdirSync(u,{recursive:!0}),{fileRootPath:u,urlRootPath:e?`@/media/${r?r+"/":""}`:`${process.env.DSQL_STATIC_HOST}${o}`,relativePath:o}}},69842:(e,a,r)=>{r.d(a,{A:()=>l});var i=r(39908),t=r.n(i);async function l(e,a,r){let i=t()({req:e,level:r?"deep":void 0});return console.log(i),i.payload}},67619:(e,a,r)=>{r.d(a,{A:()=>l});var i=r(29021),t=r.n(i);async function l({user:e,message:a,component:r,noMail:i,req:l}){let s=new Date,n=(()=>{if(!l)return null;try{let e=l.headers["x-forwarded-for"],a=l.headers["x-real-ip"],r=l.headers["cf-connecting-ip"],i=Array.isArray(e)?e[0]:e?.split(",")[0],t=r||i||a||l.socket.remoteAddress;if(!t)return null;return String(t)}catch(e){return null}})();try{let i=`🚀 SERVER ERROR ===========================
Error Message: ${a}
Component: ${r}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(i+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),l?.url&&(i+=`
URL: ${l.url}`),l?.body&&(i+=`
Request Body: ${JSON.stringify(l.body,null,4)}`),n&&(i+=`
IP: ${n}`),i+=`
Date: ${s.toDateString()}
========================================`,t().existsSync("./.tmp/error.log")||t().writeFileSync("./.tmp/error.log","","utf-8");let o=t().readFileSync("./.tmp/error.log","utf-8");t().writeFileSync("./.tmp/error.log",i),t().appendFileSync("./.tmp/error.log",`
${o}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var r=e=>a(a.s=e),i=a.X(0,[1251],()=>r(27973));module.exports=i})();