Skip to content

Commit

Permalink
Роуты сгруппированы, генератор тела теперь обрабатывает и неправильны…
Browse files Browse the repository at this point in the history
…е скины
  • Loading branch information
xtrafrancyz committed Apr 17, 2017
1 parent a55efc9 commit 66cf7b1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
130 changes: 69 additions & 61 deletions src/main/java/net/xtrafrancyz/skinservice/pippo/SkinApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@
import net.xtrafrancyz.skinservice.util.CloudflareUtil;

import java.io.IOException;
import java.util.function.Consumer;

/**
* @author xtrafrancyz
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -175,4 +177,10 @@ private static void writeImage(RouteContext context, Image image) {
e.printStackTrace();
}
}

private void addRouteGroup(String pattern, Consumer<RouteGroup> filler) {
RouteGroup group = new RouteGroup(pattern);
filler.accept(group);
addRouteGroup(group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 66cf7b1

Please sign in to comment.