Finished File Upload GUI

This commit is contained in:
Marius Metzger
2015-01-31 21:04:25 +01:00
parent c8918c933a
commit 521bf52e79
5 changed files with 130 additions and 46 deletions

View File

@@ -14,7 +14,9 @@ import java.util.HashMap;
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();
@@ -23,29 +25,34 @@ public class FileUploader {
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 boolean cancel = false;
private String boundary = "*****";
private GuiUploadFile parent;
//private CountingHttpEntity counter;
public void uploadFile(String auth, String filename, File file, Category category) throws IOException, ApiException, RuntimeException {
public void uploadFile(GuiUploadFile gui, String auth, String filename, 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;
filesize = file.length();
current = 0;
String postData = "?auth="+auth+"&category="+category.getId()+"&name="+filename;
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);
@@ -60,45 +67,70 @@ public class FileUploader {
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();
request.writeBytes(this.crlf);
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
request.flush();
request.close();
int responseCode = ((HttpURLConnection)con).getResponseCode();
boolean success = false;
int responseCode = con.getResponseCode();
InputStream is = null;
if(responseCode == 200) {
success = true;
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
BufferedReader r = new BufferedReader(new InputStreamReader(is));
while(r.ready()) {
System.out.println(r.readLine());
String info = null;
if(responseCode != 200) {
ApiError error = new ApiError(-1, "An unknown error occured");
while(r.ready()) {
error = gson.fromJson(r.readLine(), ApiError.class);
}
info = error.getDesc();
}
System.out.println(responseCode); // Should be 200
con.disconnect();
parent.onFinishUploading(success, info);
uploading = false;
}
public float getUploadProgress() {
if(!uploading) return 0.1f;
if(!uploading || filesize == 0) return 0;
return (float)((double)current/(double)filesize);
}
public boolean isUploading() {
return uploading;
}
public void cancelUploading() {
cancel = true;
}
}