Refactored and reformatted code to use less static variables
This commit is contained in:
@@ -1,160 +1,149 @@
|
||||
package eu.crushedpixel.replaymod.api.client;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import com.google.gson.Gson;
|
||||
import eu.crushedpixel.replaymod.api.client.holders.ApiError;
|
||||
import eu.crushedpixel.replaymod.api.client.holders.Category;
|
||||
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import eu.crushedpixel.replaymod.api.client.holders.ApiError;
|
||||
import eu.crushedpixel.replaymod.api.client.holders.Category;
|
||||
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
|
||||
|
||||
public class FileUploader {
|
||||
private static Gson gson = new Gson();
|
||||
private static JsonParser jsonParser = new JsonParser();
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private boolean uploading = false;
|
||||
private long filesize;
|
||||
private long current;
|
||||
private boolean uploading = false;
|
||||
private long filesize;
|
||||
private long current;
|
||||
|
||||
private String attachmentName = "file";
|
||||
private String attachmentFileName = "file.mcpr";
|
||||
private String crlf = "\r\n";
|
||||
private String twoHyphens = "--";
|
||||
private String attachmentName = "file";
|
||||
private String attachmentFileName = "file.mcpr";
|
||||
private String crlf = "\r\n";
|
||||
private String twoHyphens = "--";
|
||||
|
||||
private boolean cancel = false;
|
||||
private boolean cancel = false;
|
||||
|
||||
private String boundary = "*****";
|
||||
private GuiUploadFile parent;
|
||||
//private CountingHttpEntity counter;
|
||||
private String boundary = "*****";
|
||||
private GuiUploadFile parent;
|
||||
|
||||
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category) throws IOException, ApiException, RuntimeException {
|
||||
parent = gui;
|
||||
gui.onStartUploading();
|
||||
filesize = 0;
|
||||
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category) throws IOException, ApiException, RuntimeException {
|
||||
parent = gui;
|
||||
gui.onStartUploading();
|
||||
filesize = 0;
|
||||
|
||||
if(uploading) throw new RuntimeException("FileUploader is already uploading");
|
||||
uploading = true;
|
||||
if(uploading) throw new RuntimeException("FileUploader is already uploading");
|
||||
uploading = true;
|
||||
|
||||
String postData = "?auth="+auth+"&category="+category.getId();
|
||||
String postData = "?auth=" + auth + "&category=" + category.getId();
|
||||
|
||||
if(tags.size() > 0) {
|
||||
postData += "&tags=";
|
||||
for(String tag : tags) {
|
||||
postData += tag;
|
||||
if(!tag.equals(tags.get(tags.size()-1))) {
|
||||
postData += ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
if(tags.size() > 0) {
|
||||
postData += "&tags=";
|
||||
for(String tag : tags) {
|
||||
postData += tag;
|
||||
if(!tag.equals(tags.get(tags.size() - 1))) {
|
||||
postData += ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
postData +="&name="+URLEncoder.encode(filename, "UTF-8");
|
||||
System.out.println(postData);
|
||||
postData += "&name=" + URLEncoder.encode(filename, "UTF-8");
|
||||
System.out.println(postData);
|
||||
|
||||
String url = "http://ReplayMod.com/api/upload_file"+postData;
|
||||
HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
|
||||
con.setUseCaches(false);
|
||||
con.setDoOutput(true);
|
||||
con.setRequestMethod("POST");
|
||||
con.setChunkedStreamingMode(1024);
|
||||
con.setRequestProperty("Connection", "Keep-Alive");
|
||||
con.setRequestProperty("Cache-Control", "no-cache");
|
||||
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
|
||||
String url = "http://ReplayMod.com/api/upload_file" + postData;
|
||||
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
|
||||
con.setUseCaches(false);
|
||||
con.setDoOutput(true);
|
||||
con.setRequestMethod("POST");
|
||||
con.setChunkedStreamingMode(1024);
|
||||
con.setRequestProperty("Connection", "Keep-Alive");
|
||||
con.setRequestProperty("Cache-Control", "no-cache");
|
||||
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
|
||||
|
||||
HashMap<String, String> params = new HashMap<String, String>();
|
||||
params.put("auth", auth);
|
||||
params.put("name", filename);
|
||||
params.put("category", category.getId()+"");
|
||||
HashMap<String, String> params = new HashMap<String, String>();
|
||||
params.put("auth", auth);
|
||||
params.put("name", filename);
|
||||
params.put("category", category.getId() + "");
|
||||
|
||||
DataOutputStream request = new DataOutputStream(con.getOutputStream());
|
||||
DataOutputStream request = new DataOutputStream(con.getOutputStream());
|
||||
|
||||
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"" + this.attachmentName + "\";filename=\"" + this.attachmentFileName + "\"" + this.crlf);
|
||||
request.writeBytes(this.crlf);
|
||||
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"" + this.attachmentName + "\";filename=\"" + this.attachmentFileName + "\"" + this.crlf);
|
||||
request.writeBytes(this.crlf);
|
||||
|
||||
byte[] buf = new byte[1024];
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
filesize = fis.getChannel().size();
|
||||
current = 0;
|
||||
int len;
|
||||
while((len = fis.read(buf)) != -1) {
|
||||
request.write(buf);
|
||||
current += len;
|
||||
if(cancel) {
|
||||
uploading = false;
|
||||
current = 0;
|
||||
cancel = false;
|
||||
parent.onFinishUploading(false, "Upload has been canceled");
|
||||
fis.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
fis.close();
|
||||
byte[] buf = new byte[1024];
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
filesize = fis.getChannel().size();
|
||||
current = 0;
|
||||
int len;
|
||||
while((len = fis.read(buf)) != -1) {
|
||||
request.write(buf);
|
||||
current += len;
|
||||
if(cancel) {
|
||||
uploading = false;
|
||||
current = 0;
|
||||
cancel = false;
|
||||
parent.onFinishUploading(false, "Upload has been canceled");
|
||||
fis.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
fis.close();
|
||||
|
||||
request.writeBytes(this.crlf);
|
||||
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
|
||||
request.writeBytes(this.crlf);
|
||||
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
|
||||
|
||||
request.flush();
|
||||
request.close();
|
||||
request.flush();
|
||||
request.close();
|
||||
|
||||
boolean success = false;
|
||||
int responseCode = con.getResponseCode();
|
||||
InputStream is = null;
|
||||
if(responseCode == 200) {
|
||||
success = true;
|
||||
is = con.getInputStream();
|
||||
} else {
|
||||
is = con.getErrorStream();
|
||||
}
|
||||
boolean success = false;
|
||||
int responseCode = con.getResponseCode();
|
||||
InputStream is = null;
|
||||
if(responseCode == 200) {
|
||||
success = true;
|
||||
is = con.getInputStream();
|
||||
} else {
|
||||
is = con.getErrorStream();
|
||||
}
|
||||
|
||||
String info = null;
|
||||
|
||||
if(is != null) {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
||||
info = null;
|
||||
if(responseCode != 200) {
|
||||
ApiError error = new ApiError(-1, "An unknown error occured");
|
||||
String json = "";
|
||||
while(r.ready()) {
|
||||
json += r.readLine();
|
||||
}
|
||||
error = gson.fromJson(json, ApiError.class);
|
||||
info = error.getDesc();
|
||||
System.out.println(info);
|
||||
}
|
||||
}
|
||||
String info = null;
|
||||
|
||||
if(is != null) {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
||||
info = null;
|
||||
if(responseCode != 200) {
|
||||
String json = "";
|
||||
while(r.ready()) {
|
||||
json += r.readLine();
|
||||
}
|
||||
ApiError error = gson.fromJson(json, ApiError.class);
|
||||
info = error.getDesc();
|
||||
System.out.println(info);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
con.disconnect();
|
||||
con.disconnect();
|
||||
|
||||
if(info == null) info = "An unknown error occured";
|
||||
|
||||
parent.onFinishUploading(success, info);
|
||||
if(info == null) info = "An unknown error occured";
|
||||
|
||||
uploading = false;
|
||||
}
|
||||
parent.onFinishUploading(success, info);
|
||||
|
||||
public float getUploadProgress() {
|
||||
if(!uploading || filesize == 0) return 0;
|
||||
return (float)((double)current/(double)filesize);
|
||||
}
|
||||
uploading = false;
|
||||
}
|
||||
|
||||
public boolean isUploading() {
|
||||
return uploading;
|
||||
}
|
||||
public float getUploadProgress() {
|
||||
if(!uploading || filesize == 0) return 0;
|
||||
return (float) ((double) current / (double) filesize);
|
||||
}
|
||||
|
||||
public void cancelUploading() {
|
||||
cancel = true;
|
||||
}
|
||||
public boolean isUploading() {
|
||||
return uploading;
|
||||
}
|
||||
|
||||
public void cancelUploading() {
|
||||
cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user