package com.ironsource.sdk.utils; import android.content.Context; import android.os.Build; import com.ironsource.ce; import com.ironsource.el; import com.ironsource.hs; import com.ironsource.i9; import com.ironsource.mediationsdk.logger.IronLog; import com.ironsource.v8; import com.ironsource.zf; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes4.dex */ public class IronSourceStorageUtils { private static final String a = "supersonicads"; private static hs b; private static boolean c; private static void a(Context context) { hs hsVar = b; if (hsVar != null && hsVar.b()) { deleteCacheDirectories(context); } hs hsVar2 = b; if (hsVar2 == null || !hsVar2.c()) { return; } deleteFilesDirectories(context); } private static void a(File file) { if (file != null) { deleteFolder(b(file).getPath()); } } private static boolean a() { hs hsVar; return Build.VERSION.SDK_INT > 29 && (hsVar = b) != null && hsVar.a(); } private static File b(Context context) { ce f = el.N().f(); hs hsVar = b; return (hsVar == null || !hsVar.d()) ? f.t(context) : f.e(context); } private static File b(File file) { StringBuilder sb = new StringBuilder(); sb.append(file.getAbsolutePath()); String str = File.separator; sb.append(str); sb.append(a); sb.append(str); return new File(sb.toString()); } public static String buildAbsolutePathToDirInCache(String str, String str2) { if (str2 == null) { return str; } return str + File.separator + str2; } public static JSONObject buildFilesMap(String str, String str2) { Object c2; String name; File file = new File(str, str2); JSONObject jSONObject = new JSONObject(); File[] listFiles = file.listFiles(); if (listFiles != null) { for (File file2 : listFiles) { try { c2 = c(file2); } catch (JSONException e) { i9.d().a(e); IronLog.INTERNAL.error(e.toString()); } if (c2 instanceof JSONArray) { name = "files"; } else if (c2 instanceof JSONObject) { name = file2.getName(); } jSONObject.put(name, c(file2)); } } return jSONObject; } public static JSONObject buildFilesMapOfDirectory(zf zfVar, JSONObject jSONObject) throws Exception { String name; JSONObject buildFilesMapOfDirectory; if (zfVar == null || !zfVar.isDirectory()) { return new JSONObject(); } File[] listFiles = zfVar.listFiles(); if (listFiles == null) { return new JSONObject(); } JSONObject jSONObject2 = new JSONObject(); for (File file : listFiles) { zf zfVar2 = new zf(file.getPath()); if (zfVar2.isFile()) { name = zfVar2.getName(); buildFilesMapOfDirectory = zfVar2.a(); if (jSONObject.has(name)) { buildFilesMapOfDirectory = SDKUtils.mergeJSONObjects(buildFilesMapOfDirectory, jSONObject.getJSONObject(name)); } } else if (zfVar2.isDirectory()) { name = zfVar2.getName(); buildFilesMapOfDirectory = buildFilesMapOfDirectory(zfVar2, jSONObject); } jSONObject2.put(name, buildFilesMapOfDirectory); } return jSONObject2; } private static File c(Context context) { ce f = el.N().f(); hs hsVar = b; return (hsVar == null || !hsVar.d()) ? f.v(context) : f.k(context); } private static Object c(File file) { JSONObject jSONObject = new JSONObject(); JSONArray jSONArray = new JSONArray(); try { } catch (JSONException e) { i9.d().a(e); IronLog.INTERNAL.error(e.toString()); } if (file.isFile()) { jSONArray.put(file.getName()); return jSONArray; } for (File file2 : file.listFiles()) { if (file2.isDirectory()) { jSONObject.put(file2.getName(), c(file2)); } else { jSONArray.put(file2.getName()); jSONObject.put("files", jSONArray); } } return jSONObject; } public static void deleteCacheDirectories(Context context) { ce f = el.N().f(); a(f.e(context)); a(f.k(context)); } public static synchronized boolean deleteFile(zf zfVar) { synchronized (IronSourceStorageUtils.class) { if (!zfVar.exists()) { return false; } return zfVar.delete(); } } public static void deleteFilesDirectories(Context context) { ce f = el.N().f(); a(f.t(context)); a(f.v(context)); } public static synchronized boolean deleteFolder(String str) { boolean z; synchronized (IronSourceStorageUtils.class) { File file = new File(str); if (deleteFolderContentRecursive(file)) { z = file.delete(); } } return z; } public static boolean deleteFolderContentRecursive(File file) { File[] listFiles = file.listFiles(); boolean z = true; if (listFiles != null) { for (File file2 : listFiles) { if (file2.isDirectory()) { z &= deleteFolderContentRecursive(file2); } if (!file2.delete()) { z = false; } } } return z; } public static void ensurePathSafety(File file, String str) throws Exception { hs hsVar = b; if (hsVar == null || !hsVar.e()) { String canonicalPath = new File(str).getCanonicalPath(); String canonicalPath2 = file.getCanonicalPath(); if (canonicalPath2.startsWith(canonicalPath)) { return; } throw new Exception(v8.c.u + canonicalPath2); } } public static String getCachedFilesMap(String str, String str2) { JSONObject buildFilesMap = buildFilesMap(str, str2); try { buildFilesMap.put("path", str2); } catch (JSONException e) { i9.d().a(e); IronLog.INTERNAL.error(e.toString()); } return buildFilesMap.toString(); } public static String getDiskCacheDirPath(Context context) { File b2; if (!a() || !SDKUtils.isExternalStorageAvailable() || (b2 = b(context)) == null || !b2.canWrite()) { return c(context).getPath(); } c = true; return b2.getPath(); } public static ArrayList getFilesInFolderRecursive(zf zfVar) { if (zfVar == null || !zfVar.isDirectory()) { return new ArrayList<>(); } ArrayList arrayList = new ArrayList<>(); File[] listFiles = zfVar.listFiles(); if (listFiles != null) { for (File file : listFiles) { zf zfVar2 = new zf(file.getPath()); if (zfVar2.isDirectory()) { arrayList.addAll(getFilesInFolderRecursive(zfVar2)); } if (zfVar2.isFile()) { arrayList.add(zfVar2); } } } return arrayList; } public static String getNetworkStorageDir(Context context) { File b2 = b(new File(getDiskCacheDirPath(context))); if (!b2.exists()) { b2.mkdir(); } return b2.getPath(); } public static long getTotalSizeOfDir(zf zfVar) { long totalSizeOfDir; long j = 0; if (zfVar != null && zfVar.isDirectory()) { File[] listFiles = zfVar.listFiles(); if (listFiles == null) { return 0L; } for (File file : listFiles) { zf zfVar2 = new zf(file.getPath()); if (zfVar2.isFile()) { totalSizeOfDir = zfVar2.length(); } else if (zfVar2.isDirectory()) { totalSizeOfDir = getTotalSizeOfDir(zfVar2); } j += totalSizeOfDir; } } return j; } public static void initializeCacheDirectory(Context context, hs hsVar) { b = hsVar; a(context); } public static boolean isPathExist(String str, String str2) { return new File(str, str2).exists(); } public static boolean isUxt() { return c; } public static String makeDir(String str) { File file = new File(str); if (file.exists() || file.mkdirs()) { return file.getPath(); } return null; } public static String readFile(zf zfVar) throws Exception { StringBuilder sb = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new FileReader(zfVar)); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { bufferedReader.close(); return sb.toString(); } sb.append(readLine); sb.append('\n'); } } public static boolean renameFile(String str, String str2) throws Exception { return new File(str).renameTo(new File(str2)); } public static int saveFile(byte[] bArr, String str) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(new File(str)); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr); try { byte[] bArr2 = new byte[102400]; int i = 0; while (true) { int read = byteArrayInputStream.read(bArr2); if (read == -1) { return i; } fileOutputStream.write(bArr2, 0, read); i += read; } } finally { fileOutputStream.close(); byteArrayInputStream.close(); } } }