MCPcopy Create free account
hub / github.com/M66B/FairEmail / upload

Method upload

app/src/extra/java/eu/faircode/email/VirusTotal.java:96–170  ·  view source on GitHub ↗
(Context context, File file, String apiKey)

Source from the content-addressed store, hash-verified

94 }
95
96 static String upload(Context context, File file, String apiKey) throws IOException, JSONException {
97 // Get upload URL
98 Pair<Integer, String> response = call(context, "api/v3/files/upload_url", apiKey);
99 if (response.first != HttpsURLConnection.HTTP_OK)
100 throw new FileNotFoundException(response.second);
101 JSONObject jurl = new JSONObject(response.second);
102 String upload_url = jurl.getString("data");
103
104 // Upload file
105 String boundary = "----FairEmail." + System.currentTimeMillis();
106
107 URL url = new URL(upload_url);
108 Log.i("VT upload url=" + url);
109 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
110 connection.setRequestMethod("POST");
111 connection.setDoOutput(true);
112 connection.setReadTimeout(VT_TIMEOUT * 1000);
113 connection.setConnectTimeout(VT_TIMEOUT * 1000);
114 ConnectionHelper.setUserAgent(context, connection);
115 connection.setRequestProperty("x-apikey", apiKey);
116 connection.setRequestProperty("Accept", "application/json");
117 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
118 connection.connect();
119
120 try {
121 OutputStream os = connection.getOutputStream();
122
123 PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
124 writer
125 .append("--").append(boundary).append("\r\n")
126 .append("Content-Disposition: form-data;")
127 .append(" name=\"file\";")
128 .append(" filename=\"").append(file.getName()).append("\"").append("\r\n")
129 .append("Content-Type: application/octet-stream").append("\r\n")
130 .append("Content-Transfer-Encoding: binary").append("\r\n")
131 .append("\r\n")
132 .flush();
133
134 try (InputStream is = new FileInputStream(file)) {
135 Helper.copy(is, os);
136 }
137
138 os.flush();
139
140 writer
141 .append("\r\n")
142 .append("--").append(boundary).append("--").append("\r\n")
143 .flush();
144
145 writer.close();
146
147 int status = connection.getResponseCode();
148 if (status != HttpsURLConnection.HTTP_OK) {
149 String error = "Error " + status + ": " + connection.getResponseMessage();
150 try {
151 InputStream is = connection.getErrorStream();
152 if (is != null)
153 error += "\n" + Helper.readStream(is);

Callers

nothing calls this directly

Calls 15

callMethod · 0.95
iMethod · 0.95
setUserAgentMethod · 0.95
copyMethod · 0.95
readStreamMethod · 0.95
wMethod · 0.95
getOutputStreamMethod · 0.65
getNameMethod · 0.65
getInputStreamMethod · 0.65
getStringMethod · 0.45
connectMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected