Direct upload

This commit is contained in:
samczsun 2017-02-27 21:42:30 -05:00 committed by cnr
parent a0e79d87a2
commit d072fc3c21
2 changed files with 14 additions and 46 deletions

View File

@ -24,15 +24,5 @@
<artifactId>zt-zip</artifactId> <artifactId>zt-zip</artifactId>
<version>1.9</version> <version>1.9</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,27 +1,21 @@
package nautilus.game.arcade.uhc; package nautilus.game.arcade.uhc;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import net.minecraft.server.v1_8_R3.BiomeBase; import net.minecraft.server.v1_8_R3.BiomeBase;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
@ -42,14 +36,10 @@ import org.zeroturnaround.zip.ByteSource;
import org.zeroturnaround.zip.FileSource; import org.zeroturnaround.zip.FileSource;
import org.zeroturnaround.zip.ZipEntrySource; import org.zeroturnaround.zip.ZipEntrySource;
import org.zeroturnaround.zip.ZipUtil; import org.zeroturnaround.zip.ZipUtil;
import org.zeroturnaround.zip.commons.IOUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class WorldGen extends JavaPlugin implements Listener public class WorldGen extends JavaPlugin implements Listener
{ {
private static final int TIMEOUT = (int) TimeUnit.SECONDS.toMillis(10);
// The world will be -MAP_SIZE to MAP_SIZE large // The world will be -MAP_SIZE to MAP_SIZE large
private static final int MAP_SIZE = 1000; private static final int MAP_SIZE = 1000;
private static final int VIEW_DISTANCE = 5; private static final int VIEW_DISTANCE = 5;
@ -295,32 +285,20 @@ public class WorldGen extends JavaPlugin implements Listener
FileUtils.deleteQuietly(worldFolder); FileUtils.deleteQuietly(worldFolder);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
HttpPost request = new HttpPost("http://" + API_HOST_MAP.get("ENDERCHEST") + "/map/uhc/upload");
request.addHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", outputFile.getName());
jsonObject.addProperty("location", outputFile.toURI().toString());
request.setEntity(new StringEntity(new Gson().toJson(jsonObject), StandardCharsets.UTF_8));
try try
{ {
getLogger().info("Uploading " + seed + "!"); getLogger().info("Uploading " + seed + "!");
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() != 200) URL url = new URL("http://" + API_HOST_MAP.get("ENDERCHEST") + "/map/uhc/upload?name=" + outputFile.getName());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
IOUtils.copy(new FileInputStream(outputFile), connection.getOutputStream());
connection.connect();
if (connection.getResponseCode() != 200)
{ {
if (response.getStatusLine().getStatusCode() == 409) if (connection.getResponseCode() == 409)
{ {
getLogger().warning("Oops - Server rejected " + seed + " because it was already generated"); getLogger().warning("Oops - Server rejected " + seed + " because it was already generated");
@ -331,7 +309,7 @@ public class WorldGen extends JavaPlugin implements Listener
} }
else else
{ {
getLogger().severe("Failed to upload " + seed + ": " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); getLogger().severe("Failed to upload " + seed + ": " + connection.getResponseCode() + " " + connection.getResponseMessage());
} }
} }
else else