@@ -5,14 +5,14 @@ import { GlobalIcon, icons } from "../../../types/GlobalIcon";
55import { stripUUID } from "../../../libs/game-profiles" ;
66import { ElysiaApp } from "../../.." ;
77import { sendCustomIconUploadMessage } from "../../../libs/discord-notifier" ;
8- import sharp from "sharp" ;
98import Logger from "../../../libs/Logger" ;
109import { generateSecureCode } from "../../../libs/crypto" ;
1110import { Player } from "../../../libs/database/schemas/Player" ;
1211import { tResponseBody , tHeaders , tParams , tRequestBody } from "../../../libs/models" ;
1312import { DocumentationCategory } from "../../../types/DocumentationCategory" ;
1413import { customIconFile , customIconPath } from "../../../libs/data-accessor" ;
1514import { readdirSync } from "fs" ;
15+ import { imageErrorTranslation , processUploadedImage } from "../../../libs/image-processor" ;
1616
1717export function getCustomIconUrl ( uuid : string , hash : string ) {
1818 return `${ config . baseUrl } /players/${ uuid } /icon/${ hash } ` ;
@@ -37,7 +37,7 @@ export default (app: ElysiaApp) => app.get('/', async ({ session, params: { uuid
3737 params : tParams . uuid
3838} ) . get ( '/:hash' , async ( { params : { uuid, hash } , i18n, status } ) => { // Get custom icon
3939 const player = await Player . findOne ( { uuid : stripUUID ( uuid ) } ) ;
40- if ( ! player ) return status ( 404 , { error : i18n ( '$.error.noTag ' ) } ) ;
40+ if ( ! player ) return status ( 404 , { error : i18n ( '$.error.playerNotFound ' ) } ) ;
4141 if ( player . isBanned ( ) ) return status ( 403 , { error : i18n ( '$.error.playerBanned' ) } ) ;
4242
4343 const file = customIconFile ( player . uuid , hash ) ;
@@ -55,36 +55,35 @@ export default (app: ElysiaApp) => app.get('/', async ({ session, params: { uuid
5555 404 : tResponseBody . Error
5656 } ,
5757 params : tParams . uuidAndIconHash
58- } ) . post ( '/' , async ( { session, body : { image } , params, i18n, status } ) => { // Upload custom icon
58+ } ) . post ( '/' , async ( { session, body, params, i18n, status } ) => { // Upload custom icon
5959 if ( ! session || ! session . self ) return status ( 403 , { error : i18n ( '$.error.notAllowed' ) } ) ;
6060
6161 const player = await Player . findOne ( { uuid : stripUUID ( params . uuid ) } ) ;
6262 if ( ! player ) return status ( 404 , { error : i18n ( '$.error.noTag' ) } ) ;
6363 if ( player . isBanned ( ) ) return status ( 403 , { error : i18n ( '$.error.banned' ) } ) ;
6464 if ( ! player . hasPermission ( Permission . CustomIcon ) ) return status ( 403 , { error : i18n ( '$.icon.upload.notAllowed' ) } ) ;
6565
66- const metadata = await sharp ( await image . arrayBuffer ( ) ) . metadata ( ) . catch ( ( err : Error ) => {
67- Logger . error ( 'Failed to read image metadata:' , err . message ) ;
68- return null ;
69- } ) ;
70-
71- if ( ! metadata ) return status ( 422 , { error : i18n ( '$.icon.upload.invalidMetadata' ) } ) ;
72- if ( metadata . format != 'png' ) return status ( 422 , { error : i18n ( '$.icon.upload.wrongFormat' ) } ) ;
73- if ( ! metadata . height || metadata . height != metadata . width ) return status ( 422 , { error : i18n ( '$.icon.upload.wrongResolution' ) } ) ;
74- if ( metadata . height > config . validation . icon . maxResolution ) return status ( 422 , { error : i18n ( '$.icon.upload.exceedsMaxResolution' ) . replaceAll ( '<max>' , config . validation . icon . maxResolution . toString ( ) ) } ) ;
75-
7666 player . icon . type = GlobalIcon . Custom ;
7767 player . icon . hash = generateSecureCode ( 32 ) ;
7868 player . markModified ( 'icon' ) ;
79- await player . save ( ) ;
80- await Bun . write ( customIconFile ( player . uuid , player . icon . hash ) , await image . arrayBuffer ( ) , { createPath : true } ) ;
8169
82- if ( ! player . hasPermission ( Permission . BypassValidation ) ) sendCustomIconUploadMessage (
83- await player . getGameProfile ( ) ,
84- player . icon . hash
85- ) ;
70+ try {
71+ await processUploadedImage (
72+ new Bun . Image ( await body . image . arrayBuffer ( ) ) ,
73+ config . validation . icon . maxResolution ,
74+ customIconFile ( player . uuid , player . icon . hash )
75+ ) ;
76+ await player . save ( ) ;
77+
78+ if ( ! player . hasPermission ( Permission . BypassValidation ) ) sendCustomIconUploadMessage (
79+ await player . getGameProfile ( ) ,
80+ player . icon . hash
81+ ) ;
8682
87- return { message : i18n ( '$.icon.upload.success' ) , hash : player . icon . hash } ;
83+ return { message : i18n ( '$.icon.upload.success' ) , hash : player . icon . hash } ;
84+ } catch ( err ) {
85+ return status ( 422 , { error : i18n ( imageErrorTranslation ( err ) ) } ) ;
86+ }
8887} , {
8988 detail : {
9089 tags : [ DocumentationCategory . Tags ] ,
0 commit comments