From 66cf7b1bd2b88ad29367d72c6958408b9296ec78 Mon Sep 17 00:00:00 2001 From: xtrafrancyz Date: Mon, 17 Apr 2017 14:26:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=BE=D1=83=D1=82=D1=8B=20=D1=81=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=BF=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D1=8B,=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D1=82=D0=B5=D0=BB=D0=B0=20=D1=82=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=B0=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=B5=D1=82=20=D0=B8=20=D0=BD=D0=B5=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../skinservice/pippo/SkinApplication.java | 130 ++++++++++-------- .../skinservice/processor/Humanizer.java | 11 +- 2 files changed, 73 insertions(+), 68 deletions(-) diff --git a/src/main/java/net/xtrafrancyz/skinservice/pippo/SkinApplication.java b/src/main/java/net/xtrafrancyz/skinservice/pippo/SkinApplication.java index e1f69e2..6a5928a 100644 --- a/src/main/java/net/xtrafrancyz/skinservice/pippo/SkinApplication.java +++ b/src/main/java/net/xtrafrancyz/skinservice/pippo/SkinApplication.java @@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory; import ro.pippo.core.Application; import ro.pippo.core.route.RouteContext; +import ro.pippo.core.route.RouteGroup; import net.xtrafrancyz.skinservice.SkinService; import net.xtrafrancyz.skinservice.processor.Humanizer; @@ -12,6 +13,7 @@ import net.xtrafrancyz.skinservice.util.CloudflareUtil; import java.io.IOException; +import java.util.function.Consumer; /** * @author xtrafrancyz @@ -72,76 +74,76 @@ public SkinApplication(SkinService service) { // ### /game - GET("/game/v1/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, Resizer.getSkin(username, false, 64, 32)); - }); - - GET("/game/v1/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, Resizer.getCape(username, 22, 17)); - }); - - GET("/game/v2/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, service.skinRepository.getSkin(username, false)); - }); - - GET("/game/v2/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, Resizer.getCape(username, 64, 32)); + addRouteGroup("/game", group -> { + group.GET("/v1/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, Resizer.getSkin(username, false, 64, 32)); + }); + group.GET("/v1/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, Resizer.getCape(username, 22, 17)); + }); + group.GET("/v2/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, service.skinRepository.getSkin(username, false)); + }); + group.GET("/v2/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, Resizer.getCape(username, 64, 32)); + }); }); // ### /raw - GET("/raw/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, service.skinRepository.getCape(username)); - }); - GET("/raw/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { - String username = context.getParameter("username").toString(); - writeImage(context, service.skinRepository.getSkin(username, false)); + addRouteGroup("/raw", group -> { + group.GET("/cape/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, service.skinRepository.getCape(username)); + }); + group.GET("/skin/{username: [a-zA-z0-9_-]+}\\.png", context -> { + String username = context.getParameter("username").toString(); + writeImage(context, service.skinRepository.getSkin(username, false)); + }); }); - // ### /private/0 auth - ALL("/private/{token}/.*", context -> { - String token = context.getParameter("token").toString(); - if (service.config.tokens.contains(token)) { - context.next(); - } else { - context.status(403); - context.send("Invalid token"); - } - }); - - // ### /private/0/cache/cape - DELETE("/private/{token}/cache/cape/{username: [a-zA-z0-9_-]+}", context -> { - String username = context.getParameter("username").toString(); - service.skinRepository.invalidateCape(username); - CloudflareUtil.clearCache( - "/game/v1/cape/" + username + ".png", - "/game/v2/cape/" + username + ".png", - "/cape/" + username + ".png" - ); - context.status(200); - context.send("OK"); + // ### /private + addRouteGroup("/private", group -> { + group.ALL("/{token}/.*", context -> { + String token = context.getParameter("token").toString(); + if (service.config.tokens.contains(token)) { + context.next(); + } else { + context.status(403); + context.send("Invalid token"); + } + }); + group.DELETE("/{token}/cache/cape/{username: [a-zA-z0-9_-]+}", context -> { + String username = context.getParameter("username").toString(); + service.skinRepository.invalidateCape(username); + CloudflareUtil.clearCache( + "/game/v1/cape/" + username + ".png", + "/game/v2/cape/" + username + ".png", + "/cape/" + username + ".png" + ); + context.status(200); + context.send("OK"); + }); + group.DELETE("/{token}/cache/skin/{username: [a-zA-z0-9_-]+}", context -> { + String username = context.getParameter("username").toString(); + service.skinRepository.invalidateSkin(username); + CloudflareUtil.clearCache( + "/game/v1/skin/" + username + ".png", + "/game/v2/skin/" + username + ".png", + "/helm/" + username + ".png", + "/head/" + username + ".png", + "/body/" + username + ".png" + ); + context.status(200); + context.send("OK"); + }); }); - // ### /private/0/cache/skin - DELETE("/private/{token}/cache/skin/{username: [a-zA-z0-9_-]+}", context -> { - String username = context.getParameter("username").toString(); - service.skinRepository.invalidateSkin(username); - CloudflareUtil.clearCache( - "/game/v1/skin/" + username + ".png", - "/game/v2/skin/" + username + ".png", - "/helm/" + username + ".png", - "/head/" + username + ".png", - "/body/" + username + ".png" - ); - context.status(200); - context.send("OK"); - }); ALL(".*", context -> { if (detailed) { @@ -175,4 +177,10 @@ private static void writeImage(RouteContext context, Image image) { e.printStackTrace(); } } + + private void addRouteGroup(String pattern, Consumer filler) { + RouteGroup group = new RouteGroup(pattern); + filler.accept(group); + addRouteGroup(group); + } } diff --git a/src/main/java/net/xtrafrancyz/skinservice/processor/Humanizer.java b/src/main/java/net/xtrafrancyz/skinservice/processor/Humanizer.java index 2114a20..6fbbdbd 100644 --- a/src/main/java/net/xtrafrancyz/skinservice/processor/Humanizer.java +++ b/src/main/java/net/xtrafrancyz/skinservice/processor/Humanizer.java @@ -43,19 +43,16 @@ public static Image body(String username, int size) { // Left arm img.copyFrom(skin, 44, 20, 48, 32, 0, 8); // Right arm + img.copyFlippedXFrom(skin, 44, 20, 48, 32, 12, 8); if (skin.getHeight() == 64) - img.copyFrom(skin, 36, 52, 40, 64, 12, 8); - else - img.copyFlippedXFrom(skin, 44, 20, 48, 32, 12, 8); + img.copyWithAlphaFrom(skin, 36, 52, 40, 64, 12, 8); // Left leg img.copyFrom(skin, 4, 20, 8, 32, 4, 20); // Right leg + img.copyFlippedXFrom(skin, 4, 20, 8, 32, 8, 20); if (skin.getHeight() == 64) - img.copyFrom(skin, 20, 52, 24, 64, 8, 20); - else - img.copyFlippedXFrom(skin, 4, 20, 8, 32, 8, 20); - + img.copyWithAlphaFrom(skin, 20, 52, 24, 64, 8, 20); return img.scale(size, size * 2); }