-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
233 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageHost.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageUpload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cn.zbx1425.worldcomment.data.network; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ImageUpload { | ||
|
||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); | ||
|
||
private static String apiUrlBase = "https://imgse.com"; | ||
private static String loginSubject = "[email protected]"; | ||
private static String loginCredential = "Sg5KRGj6h9n5HKKC"; | ||
|
||
private static String sessionCookie; | ||
|
||
public static ThumbImage uploadImage(Path imagePath) throws IOException, InterruptedException { | ||
HttpRequest reqGetToken = HttpRequest.newBuilder(URI.create(apiUrlBase + "/login")) | ||
.header("Cookie", sessionCookie == null ? "" : sessionCookie) | ||
.GET() | ||
.build(); | ||
HttpResponse<String> respGetToken = HTTP_CLIENT.send(reqGetToken, HttpResponse.BodyHandlers.ofString()); | ||
if (respGetToken.statusCode() >= 400) | ||
throw new IOException("Auth Token HTTP " + respGetToken.statusCode() + "\n" + respGetToken.body()); | ||
Pattern tokenPattern = Pattern.compile("auth_token = \"([a-z0-9]*)\""); | ||
Matcher tokenMatcher = tokenPattern.matcher(respGetToken.body()); | ||
if (!tokenMatcher.find()) throw new IOException("Cannot locate Auth Token"); | ||
String authToken = tokenMatcher.group(1); | ||
respGetToken.headers().firstValue("Set-Cookie").ifPresent(setCookie -> { | ||
sessionCookie = setCookie.split(";")[0]; | ||
}); | ||
if (sessionCookie == null) throw new IOException("Cannot obtain session cookie"); | ||
|
||
HttpRequest reqLogin = HttpRequest.newBuilder(URI.create(apiUrlBase + "/login")) | ||
.header("Content-Type", "application/x-www-form-urlencoded") | ||
.header("Cookie", sessionCookie) | ||
.POST(HttpRequest.BodyPublishers.ofString(encodeForm(Map.of( | ||
"login-subject", loginSubject, | ||
"password", loginCredential, | ||
"auth_token", authToken | ||
)))) | ||
.build(); | ||
HttpResponse<String> respLogin = HTTP_CLIENT.send(reqLogin, HttpResponse.BodyHandlers.ofString()); | ||
if (respLogin.statusCode() >= 400) | ||
throw new IOException("Login HTTP " + respGetToken.statusCode() + "\n" + respLogin.body()); | ||
|
||
MimeMultipartData body = MimeMultipartData.newBuilder() | ||
.withCharset(StandardCharsets.UTF_8) | ||
.addFile("source", imagePath, "application/octet-stream") | ||
.addText("type", "file") | ||
.addText("action", "upload") | ||
.addText("timestamp", Long.toString(System.currentTimeMillis())) | ||
.addText("auth_token", authToken) | ||
.addText("nsfw", "0") | ||
.build(); | ||
HttpRequest reqUpload = HttpRequest.newBuilder(URI.create(apiUrlBase + "/json")) | ||
.header("Content-Type", body.getContentType()) | ||
.header("Cookie", sessionCookie) | ||
.POST(body.getBodyPublisher()) | ||
.build(); | ||
HttpResponse<String> respUpload = HTTP_CLIENT.send(reqUpload, HttpResponse.BodyHandlers.ofString()); | ||
if (respUpload.statusCode() >= 400) | ||
throw new IOException("Upload HTTP " + respUpload.statusCode() + "\n" + respUpload.body()); | ||
JsonObject respObj = JsonParser.parseString(respUpload.body()).getAsJsonObject(); | ||
if (!respObj.has("success")) throw new IOException("Upload Fail " + respUpload.body()); | ||
return new ThumbImage( | ||
respObj.get("image").getAsJsonObject().get("image").getAsJsonObject().get("url").getAsString(), | ||
respObj.get("image").getAsJsonObject().get("thumb").getAsJsonObject().get("url").getAsString() | ||
); | ||
} | ||
|
||
private static String encodeForm(Map<String, String> params) { | ||
StringBuilder result = new StringBuilder(); | ||
boolean first = true; | ||
for(Map.Entry<String, String> entry : params.entrySet()){ | ||
if (first) | ||
first = false; | ||
else | ||
result.append("&"); | ||
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)); | ||
result.append("="); | ||
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)); | ||
} | ||
return result.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
common/src/main/java/cn/zbx1425/worldcomment/network/PacketCommentDataUIS2C.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cn.zbx1425.worldcomment.network; | ||
|
||
public class PacketCommentDataUIS2C { | ||
} |
45 changes: 45 additions & 0 deletions
45
common/src/main/java/cn/zbx1425/worldcomment/network/PacketRequestCommentUIC2S.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cn.zbx1425.worldcomment.network; | ||
|
||
import cn.zbx1425.worldcomment.ClientPlatform; | ||
import cn.zbx1425.worldcomment.Main; | ||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import io.netty.buffer.Unpooled; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.level.ChunkPos; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PacketRequestCommentUIC2S { | ||
|
||
public static final ResourceLocation IDENTIFIER = new ResourceLocation(Main.MOD_ID, "request_region"); | ||
|
||
public static class ClientLogics { | ||
|
||
public static void send(ResourceLocation level, List<ChunkPos> requests) { | ||
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); | ||
buffer.writeResourceLocation(level); | ||
buffer.writeInt(requests.size()); | ||
for (ChunkPos request : requests) { | ||
buffer.writeChunkPos(request); | ||
} | ||
ClientPlatform.sendPacketToServer(IDENTIFIER, buffer); | ||
} | ||
} | ||
|
||
public static void handle(MinecraftServer server, ServerPlayer initiator, FriendlyByteBuf buffer) { | ||
ResourceLocation level = buffer.readResourceLocation(); | ||
Map<ChunkPos, List<CommentEntry>> results = new Object2ObjectArrayMap<>(); | ||
int size = buffer.readInt(); | ||
for (int i = 0; i < size; i++) { | ||
ChunkPos request = buffer.readChunkPos(); | ||
List<CommentEntry> regionResults = Main.DATABASE.comments.queryRegion(level, request); | ||
results.put(request, regionResults); | ||
} | ||
PacketRegionDataS2C.send(initiator, level, results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.