rusefi/java_console/shared_ui/src/com/rusefi/tools/online/Online.java

63 lines
2.2 KiB
Java
Raw Normal View History

2020-05-19 23:16:15 -07:00
package com.rusefi.tools.online;
2020-06-04 20:32:52 -07:00
import com.rusefi.tune.xml.Msq;
import com.rusefi.ui.AuthTokenPanel;
2020-05-22 11:03:10 -07:00
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
2020-06-04 20:32:52 -07:00
import javax.swing.*;
import javax.xml.bind.JAXBException;
2020-05-19 23:16:15 -07:00
import java.io.*;
2020-06-04 20:32:52 -07:00
import static com.rusefi.ui.AuthTokenPanel.TOKEN_WARNING;
2020-05-19 23:16:15 -07:00
public class Online {
2020-05-22 11:03:10 -07:00
private static final String url = "https://rusefi.com/online/upload.php";
2020-05-19 23:16:15 -07:00
2020-06-14 15:39:05 -07:00
public static void upload(File fileName, String authTokenValue) throws IOException {
2020-05-22 11:03:10 -07:00
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
2020-05-19 23:16:15 -07:00
2020-06-14 15:39:05 -07:00
FileBody uploadFilePart = new FileBody(fileName);
2020-05-22 11:03:10 -07:00
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
2020-05-22 11:25:03 -07:00
reqEntity.addPart("rusefi_token", new StringBody(authTokenValue));
2020-05-19 23:16:15 -07:00
2020-05-22 11:03:10 -07:00
httpPost.setEntity(reqEntity);
2020-05-19 23:16:15 -07:00
2020-05-22 11:03:10 -07:00
HttpResponse response = httpclient.execute(httpPost);
System.out.println(response);
2020-05-19 23:16:15 -07:00
2020-05-22 11:03:10 -07:00
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
2020-05-19 23:16:15 -07:00
}
2020-06-04 20:32:52 -07:00
public static void uploadTune(Msq tune, AuthTokenPanel authTokenPanel, JComponent parent) {
String authToken = authTokenPanel.getToken();
if (authToken.contains(TOKEN_WARNING)) {
JOptionPane.showMessageDialog(parent, "Does not work without auth token");
return;
}
new Thread(() -> doUpload(authToken, tune)).start();
}
private static void doUpload(String authToken, Msq tune) {
try {
tune.writeXmlFile(Msq.outputXmlFileName);
// todo: network upload should not happen on UI thread
upload(new File(Msq.outputXmlFileName), authToken);
} catch (JAXBException | IOException ex) {
throw new IllegalStateException(ex);
}
}
2020-05-19 23:16:15 -07:00
}