package com.kwad.sdk.crash.report.upload; import android.text.TextUtils; import android.util.Base64; import androidx.core.app.NotificationCompat; import com.baidu.mobads.sdk.internal.ae; import com.kwad.components.offline.api.core.api.INet; import com.kwad.sdk.core.network.c; import com.kwad.sdk.core.network.idc.a; import com.kwad.sdk.core.network.o; import com.kwad.sdk.crash.utils.g; import com.kwad.sdk.utils.p; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.json.JSONException; import org.json.JSONObject; public final class b { private static Map<String, String> a(f fVar) { HashMap hashMap = new HashMap(); if (fVar == null) { return hashMap; } if (!TextUtils.isEmpty(fVar.bho)) { hashMap.put("uploadToken", fVar.bho); } if (!TextUtils.isEmpty(fVar.bhl)) { hashMap.put(NotificationCompat.CATEGORY_SYSTEM, fVar.bhl); } if (!TextUtils.isEmpty(fVar.bhk)) { hashMap.put("did", fVar.bhk); } if (!TextUtils.isEmpty(fVar.bhi)) { hashMap.put("sid", fVar.bhi); } if (!TextUtils.isEmpty(fVar.bgD)) { hashMap.put("appver", fVar.bgD); } if (!TextUtils.isEmpty(fVar.mTaskId)) { hashMap.put("taskId", fVar.mTaskId); } if (!TextUtils.isEmpty(fVar.bhj)) { hashMap.put("token", fVar.bhj); } if (!TextUtils.isEmpty(fVar.ajJ)) { hashMap.put("uid", fVar.ajJ); } if (!TextUtils.isEmpty(fVar.bhm)) { hashMap.put("extraInfo", fVar.bhm); } return hashMap; } public static void a(File file, f fVar, a aVar) { a(file, fVar.bhk, fVar.bhj, a(fVar), aVar); } /* JADX WARNING: Removed duplicated region for block: B:56:0x0219 A[Catch:{ all -> 0x0226 }] */ /* JADX WARNING: Removed duplicated region for block: B:57:0x021a A[Catch:{ all -> 0x0226 }] */ private static void a(File file, String str, String str2, Map<String, String> map, a aVar) { OutputStream outputStream; DataInputStream dataInputStream; Throwable th; Exception e; byte[] bytes; com.kwad.sdk.core.e.b.d("ExceptionCollector", "uploadLogFile " + Thread.currentThread()); c cVar = new c(); String uuid = UUID.randomUUID().toString(); String name = file.getName(); String str3 = "https://" + a.MK().P(INet.HostType.ULOG, "ulog-sdk.gifshow.com") + "/rest/log/sdk/file/upload"; int i = -1; HttpURLConnection httpURLConnection = null; try { HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL(str3).openConnection(); try { httpURLConnection2.setDoOutput(true); httpURLConnection2.setDoInput(true); httpURLConnection2.setUseCaches(false); httpURLConnection2.setConnectTimeout(5000); httpURLConnection2.setReadTimeout(5000); httpURLConnection2.setUseCaches(false); httpURLConnection2.setRequestMethod(ae.b); httpURLConnection2.setRequestProperty("connection", "Keep-Alive"); httpURLConnection2.setRequestProperty("User-Agent", o.getUserAgent()); httpURLConnection2.setRequestProperty("Charset", "UTF-8"); httpURLConnection2.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + uuid); httpURLConnection2.setRequestProperty("Content-MD5", Base64.encodeToString(com.kwad.sdk.utils.a.ff(file.getPath()), 2)); httpURLConnection2.setRequestProperty("file-type", "." + p.getExtension(file.getName())); httpURLConnection2.setRequestProperty("origin-name", name); httpURLConnection2.setRequestProperty("Cookie", "did=" + str); httpURLConnection2.connect(); outputStream = httpURLConnection2.getOutputStream(); } catch (Exception e2) { e = e2; dataInputStream = null; outputStream = null; httpURLConnection = httpURLConnection2; try { e eVar = e.bhd; e.getCause(); aVar.PZ(); a MK = a.MK(); if (cVar.code == 0) { i = cVar.code; } MK.a(str3, i, e); com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); } catch (Throwable th2) { th = th2; com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); throw th; } } catch (Throwable th3) { th = th3; dataInputStream = null; outputStream = null; httpURLConnection = httpURLConnection2; com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); throw th; } try { for (String str4 : map.keySet()) { outputStream.write(e(str4, map.get(str4), uuid)); } bytes = ("\r\n--" + uuid + "--\r\n").getBytes(); StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(uuid); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + name + "\"\r\n"); sb.append("Content-Type: application/octet-stream\r\n\r\n"); outputStream.write(sb.toString().getBytes()); dataInputStream = new DataInputStream(new FileInputStream(file)); } catch (Exception e3) { e = e3; dataInputStream = null; httpURLConnection = httpURLConnection2; e eVar2 = e.bhd; e.getCause(); aVar.PZ(); a MK2 = a.MK(); if (cVar.code == 0) { } MK2.a(str3, i, e); com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); } catch (Throwable th4) { th = th4; dataInputStream = null; httpURLConnection = httpURLConnection2; com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); throw th; } try { byte[] bArr = new byte[1024]; while (true) { int read = dataInputStream.read(bArr); if (read == -1) { break; } outputStream.write(bArr, 0, read); } outputStream.write("\r\n".getBytes()); outputStream.write(bytes); outputStream.flush(); int responseCode = httpURLConnection2.getResponseCode(); cVar.code = responseCode; cVar.aWy = responseCode; if (responseCode == 200) { cVar.aWA = g.b(httpURLConnection2.getInputStream()); try { if (new JSONObject(cVar.aWA).optInt("result", -1) == 1) { aVar.Qa(); } else { e eVar3 = e.bhe; aVar.PZ(); } } catch (JSONException unused) { e eVar4 = e.bhf; e eVar5 = e.bhf; aVar.PZ(); } com.kwad.sdk.core.e.b.d("ExceptionCollector", "response.body= " + cVar.aWA); } else { e eVar6 = e.bhd; e.bhd.Qc(); aVar.PZ(); a.MK().a(str3, cVar.code == 0 ? -1 : cVar.code, (Throwable) null); } com.kwad.sdk.crash.utils.b.a(httpURLConnection2); } catch (Exception e4) { e = e4; httpURLConnection = httpURLConnection2; e eVar22 = e.bhd; e.getCause(); aVar.PZ(); a MK22 = a.MK(); if (cVar.code == 0) { } MK22.a(str3, i, e); com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); } catch (Throwable th5) { th = th5; httpURLConnection = httpURLConnection2; com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); throw th; } } catch (Exception e5) { e = e5; dataInputStream = null; outputStream = null; e eVar222 = e.bhd; e.getCause(); aVar.PZ(); a MK222 = a.MK(); if (cVar.code == 0) { } MK222.a(str3, i, e); com.kwad.sdk.core.e.b.printStackTrace(e); com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); } catch (Throwable th6) { th = th6; dataInputStream = null; outputStream = null; com.kwad.sdk.crash.utils.b.a(httpURLConnection); com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); throw th; } com.kwad.sdk.crash.utils.b.closeQuietly(dataInputStream); com.kwad.sdk.crash.utils.b.closeQuietly(outputStream); } private static byte[] e(String str, String str2, String str3) { StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(str3); sb.append("\r\n"); sb.append("Content-Disposition: form-data; name=\"" + str + "\""); sb.append("\r\n"); sb.append("Content-Length: " + str2.length()); sb.append("\r\n"); sb.append("\r\n"); sb.append(str2); sb.append("\r\n"); return sb.toString().getBytes(); } }