mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-29 05:15:13 +02:00
VirusTotal: : use HttpStatusException
This commit is contained in:
@@ -30,10 +30,10 @@ import androidx.annotation.NonNull;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.jsoup.HttpStatusException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -62,7 +62,8 @@ public class VirusTotal {
|
||||
static Bundle lookup(Context context, File file, String apiKey) throws NoSuchAlgorithmException, IOException, JSONException {
|
||||
Bundle result = new Bundle();
|
||||
|
||||
Pair<Integer, String> response = call(context, "api/v3/files/" + getHash(file), apiKey);
|
||||
String u = "api/v3/files/";
|
||||
Pair<Integer, String> response = call(context, u + getHash(file), apiKey);
|
||||
if (response.first == HttpsURLConnection.HTTP_OK) {
|
||||
// https://developers.virustotal.com/reference/files
|
||||
// Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a
|
||||
@@ -88,16 +89,17 @@ public class VirusTotal {
|
||||
result.putString("label", label);
|
||||
}
|
||||
} else if (response.first != HttpsURLConnection.HTTP_NOT_FOUND)
|
||||
throw new FileNotFoundException(response.second);
|
||||
throw new HttpStatusException(response.second, response.first, u);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static String upload(Context context, File file, String apiKey) throws IOException, JSONException {
|
||||
// Get upload URL
|
||||
Pair<Integer, String> response = call(context, "api/v3/files/upload_url", apiKey);
|
||||
String u = "api/v3/files/upload_url";
|
||||
Pair<Integer, String> response = call(context, u, apiKey);
|
||||
if (response.first != HttpsURLConnection.HTTP_OK)
|
||||
throw new FileNotFoundException(response.second);
|
||||
throw new HttpStatusException(response.second, response.first, u);
|
||||
JSONObject jurl = new JSONObject(response.second);
|
||||
String upload_url = jurl.getString("data");
|
||||
|
||||
@@ -155,7 +157,7 @@ public class VirusTotal {
|
||||
Log.w(ex);
|
||||
}
|
||||
Log.w("VT " + error);
|
||||
throw new FileNotFoundException(error);
|
||||
throw new HttpStatusException(error, status, url.toString());
|
||||
}
|
||||
|
||||
String r = Helper.readStream(connection.getInputStream());
|
||||
@@ -172,9 +174,10 @@ public class VirusTotal {
|
||||
static void waitForAnalysis(Context context, String id, String apiKey) throws IOException, JSONException, InterruptedException, TimeoutException {
|
||||
// Get analysis result
|
||||
for (int i = 0; i < VT_ANALYSIS_CHECKS; i++) {
|
||||
Pair<Integer, String> analyses = call(context, "api/v3/analyses/" + id, apiKey);
|
||||
String u = "api/v3/analyses/";
|
||||
Pair<Integer, String> analyses = call(context, u + id, apiKey);
|
||||
if (analyses.first != HttpsURLConnection.HTTP_OK)
|
||||
throw new FileNotFoundException(analyses.second);
|
||||
throw new HttpStatusException(analyses.second, analyses.first, u);
|
||||
|
||||
JSONObject janalysis = new JSONObject(analyses.second);
|
||||
JSONObject jdata = janalysis.getJSONObject("data");
|
||||
|
||||
@@ -38,6 +38,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -190,7 +191,7 @@ public class FragmentDialogVirusTotal extends FragmentDialogBase {
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(getParentFragmentManager(), ex);
|
||||
Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof IOException));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user