import { Application , Request , Response } from "express" ; import { Requests } from "../generator/Requests" ; import * as Sentry from "@sentry/node" ; import { ISkinDocument } from "../typings" ; import { corsMiddleware , Maybe } from "../util" ; import { Caching } from "../generator/Caching" ; export const register = ( app ) => { app . use ( "/render" , corsMiddleware ) ; app . get ( "/render/:type(head|skin)" , ( req , res ) => { const url = req . query [ "url" ] as string ; if ( ! url ) { res . status ( 400 ) . json ( { error : "Missing URL" } ) ; return ; } const options = req . query [ "options" ] as string || "&aa=true" ; doRender ( req , res , url , req . params [ "type" ] , options ) ; } ) ; app . get ( "/render/:id/:type(head|skin)" , ( req , res ) => { let id = req . params [ "id" ] ; let promise ; if ( id . length > 10 ) { promise = Caching . getSkinByUuid ( id ) ; } else { promise = Caching . getSkinById ( parseInt ( id ) ) ; } promise . then ( ( skin ) => { if ( ! skin ) { res . status ( 404 ) . end ( ) ; } else { const options = req . query [ "options" ] as string || "&aa=true" ; doRender ( req , res , skin . url , req . params [ "type" ] , options ) ; } } ) . catch ( ( err ) => { Sentry . captureException ( err ) ; } ) ; } ) ; app . get ( "/render/texture/:id" , ( req , res ) => { let id = req . params [ "id" ] ; let promise ; if ( id . length > 10 ) { promise = Caching . getSkinByUuid ( id ) ; } else { promise = Caching . getSkinById ( parseInt ( id ) ) ; } promise . then ( ( skin ) => { if ( ! skin ) { res . status ( 404 ) . end ( ) ; } else { Requests . genericRequest ( { url : skin . url , responseType : "stream" } ) . then ( response => { res . header ( "Content-Type" , "image/png" ) ; response . data . pipe ( res ) ; } ) . catch ( ( err ) => { Sentry . captureException ( err ) ; res . status ( 500 ) . end ( ) ; } ) ; } } ) . catch ( ( err ) => { Sentry . captureException ( err ) ; } ) ; } ) ; function doRender ( req , res , url , type , options ) { Requests . axiosInstance . request ( { url : "https://tools.inventivetalent.org/skinrender/3d.php?headOnly=" + ( type === "head" ) + "&user=" + url + options , responseType : "stream" } ) . then ( response => { res . header ( "Content-Type" , "image/png" ) ; response . data . pipe ( res ) ; } ) . catch ( ( err ) => { Sentry . captureException ( err ) ; res . status ( 500 ) . end ( ) ; } ) ; } } ;