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-06-17 20:50:57 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-06-04 20:32:52 -07:00
|
|
|
|
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();
|
2020-06-17 20:50:57 -07:00
|
|
|
if (!authTokenPanel.hasToken()) {
|
|
|
|
authTokenPanel.showError(parent);
|
2020-06-04 20:32:52 -07:00
|
|
|
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
|
|
|
}
|