Skip to content

Commit

Permalink
Fix updatechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueTree242 committed Apr 27, 2024
1 parent c5f3fd0 commit 29937e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,24 @@ public Group mergeGroups(List<Group> groups) {
return new Group(name, tabcomplete);
}

private static final HttpClient client = HttpClient.newBuilder()
.withBaseURL("https://advancedplhide.bluetree242.dev")
.withEntityMapper(new EntityMapper().registerSerializer(HTTPRequestMultipartBody.class, new HTTPRequestMultipartBody.MultiPartSerializer()))
.build();
public UpdateCheckResult updateCheck() throws Throwable {
try {
HttpClient client = HttpClient.newBuilder()
.withBaseURL("https://advancedplhide.bluetree242.dev")
.build();
HTTPRequestMultipartBody multipartBody = new HTTPRequestMultipartBody.Builder()
.addPart("version", PluginInfo.VERSION)
.addPart("buildNumber", PluginInfo.BUILD_NUMBER)
.addPart("buildDate", PluginInfo.BUILD_DATE)
.addPart("commit", PluginInfo.COMMIT)
.addPart("devUpdatechecker", String.valueOf(getConfig().dev_updatechecker()))
.build();
HttpResponse response = client.post("/updatecheck")
.withMapper(EntityMapper.newInstance())
.withHeader("Content-Type", multipartBody.getContentType())
.withInput(() -> new String(multipartBody.getBody(), StandardCharsets.UTF_8))
.execute();
JSONObject json = new JSONObject(new String(Objects.requireNonNull(response).getRawResponse(), StandardCharsets.UTF_8));
return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("message") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl"));
} catch (RuntimeException e) {
throw e.getCause();
}
HTTPRequestMultipartBody multipartBody = new HTTPRequestMultipartBody.Builder()
.addPart("version", PluginInfo.VERSION)
.addPart("buildNumber", PluginInfo.BUILD_NUMBER)
.addPart("buildDate", PluginInfo.BUILD_DATE)
.addPart("commit", PluginInfo.COMMIT)
.addPart("devUpdatechecker", String.valueOf(getConfig().dev_updatechecker()))
.build();
HttpResponse response = client.post("/updatecheck")
.withHeader("Content-Type", multipartBody.getContentType())
.withInput(() -> multipartBody)
.execute();
JSONObject json = new JSONObject(new String(Objects.requireNonNull(response).getRawResponse(), StandardCharsets.UTF_8));
return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("message") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl"));
}

public abstract Type getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

package dev.bluetree242.advancedplhide.utils;

import com.intellectualsites.http.ContentType;
import com.intellectualsites.http.EntityMapper;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -71,7 +72,6 @@ public Builder addPart(String fieldName, String fieldValue) {
part.setFieldName(fieldName);
part.setContent(fieldValue);
String DEFAULT_MIMETYPE = "text/plain";
part.setContentType(DEFAULT_MIMETYPE);
this.parts.add(part);
return this;
}
Expand All @@ -80,17 +80,14 @@ public Builder addPart(String fieldName, String fieldValue, String contentType)
MultiPartRecord part = new MultiPartRecord();
part.setFieldName(fieldName);
part.setContent(fieldValue);
part.setContentType(contentType);
this.parts.add(part);
return this;
}

public Builder addPart(String fieldName, Object fieldValue, String contentType, String fileName) {
public Builder addPart(String fieldName, String fieldValue, String contentType, String fileName) {
MultiPartRecord part = new MultiPartRecord();
part.setFieldName(fieldName);
part.setContent(fieldValue);
part.setContentType(contentType);
part.setFilename(fileName);
this.parts.add(part);
return this;
}
Expand All @@ -99,42 +96,21 @@ public HTTPRequestMultipartBody build() throws IOException {
String boundary = new BigInteger(256, new SecureRandom()).toString();
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (MultiPartRecord record : parts) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("--").append(boundary).append("\r\n").append("Content-Disposition: form-data; name=\"").append(record.getFieldName());
if (record.getFilename() != null) {
stringBuilder.append("\"; filename=\"").append(record.getFilename());
}
out.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
out.write(("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + record.getFieldName()).getBytes(StandardCharsets.UTF_8));
out.write(("\"\r\n").getBytes(StandardCharsets.UTF_8));
Object content = record.getContent();
if (content instanceof String) {
out.write(("\r\n\r\n").getBytes(StandardCharsets.UTF_8));
out.write(((String) content).getBytes(StandardCharsets.UTF_8));
} else if (content instanceof byte[]) {
out.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes(StandardCharsets.UTF_8));
out.write((byte[]) content);
} else if (content instanceof File) {
out.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes(StandardCharsets.UTF_8));
Files.copy(((File) content).toPath(), out);
} else {
out.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes(StandardCharsets.UTF_8));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject(content);
objectOutputStream.flush();
}
out.write(("\r\n").getBytes(StandardCharsets.UTF_8));
out.write(((String) content).getBytes(StandardCharsets.UTF_8));
out.write("\r\n".getBytes(StandardCharsets.UTF_8));
}
out.write(("--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8));

HTTPRequestMultipartBody httpRequestMultipartBody = new HTTPRequestMultipartBody(out.toByteArray(), boundary);
return httpRequestMultipartBody;
return new HTTPRequestMultipartBody(out.toByteArray(), boundary);
}

public static class MultiPartRecord {
private String fieldName;
private String filename;
private String contentType;
private Object content;
private String content;

public String getFieldName() {
return fieldName;
Expand All @@ -144,31 +120,26 @@ public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public Object getContent() {
return content;
}

public void setContent(Object content) {
public void setContent(String content) {
this.content = content;
}
}
}

public static class MultiPartSerializer implements EntityMapper.EntitySerializer<HTTPRequestMultipartBody> {

@Override
public byte @NotNull [] serialize(@NotNull HTTPRequestMultipartBody input) {
return input.getBody();
}

@Override
public ContentType getContentType() {
return ContentType.STRING_UTF8; // No input... Must overwrite.
}
}
}

0 comments on commit 29937e0

Please sign in to comment.