package com.helpshift.cache; import com.helpshift.log.HSLogger; import com.helpshift.network.HSDownloaderNetwork; import com.helpshift.network.HSDownloaderResponse; import com.helpshift.storage.ISharedPreferencesStore; import com.helpshift.util.FileUtil; import com.helpshift.util.Utils; import com.vungle.ads.internal.signals.SignalManager; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; /* loaded from: classes3.dex */ public class HelpshiftResourceCacheManager { public final String appFileDirPath; public Map cacheURLMapping = new HashMap(); public String cacheUrlConfigFileName; public String cacheUrlConfigRoute; public final HSDownloaderNetwork hsDownloaderNetwork; public final ResourceCacheEvictStrategy resourceCacheEvictStrategy; public final ISharedPreferencesStore resourceCacheSharedPref; public String subdirPath; public HelpshiftResourceCacheManager(ISharedPreferencesStore iSharedPreferencesStore, HSDownloaderNetwork hSDownloaderNetwork, ResourceCacheEvictStrategy resourceCacheEvictStrategy, String str, String str2, String str3, String str4) { this.hsDownloaderNetwork = hSDownloaderNetwork; this.resourceCacheSharedPref = iSharedPreferencesStore; this.resourceCacheEvictStrategy = resourceCacheEvictStrategy; this.appFileDirPath = str; this.cacheUrlConfigRoute = str2; this.cacheUrlConfigFileName = str3; this.subdirPath = str4; } public boolean shouldCacheUrl(String str) { boolean z = false; if (Utils.isEmpty(str)) { return false; } Iterator it = this.cacheURLMapping.keySet().iterator(); while (true) { if (!it.hasNext()) { break; } if (str.startsWith((String) it.next())) { z = true; break; } } HSLogger.d("resCacheMngr", "Should cache url? " + z + " with path - " + str); return z; } public final long getTTLForResource(String str) { if (Utils.isEmpty(str)) { return 0L; } Long l = 0L; Iterator it = this.cacheURLMapping.keySet().iterator(); while (true) { if (!it.hasNext()) { break; } String str2 = (String) it.next(); if (str.startsWith(str2)) { l = (Long) this.cacheURLMapping.get(str2); break; } } if (l == null) { return 0L; } return l.longValue(); } public void ensureCacheURLsListAvailable() { String string = getString("url_mapping_etag"); long j = getLong("url_mapping_last_success_time"); File file = new File(getCacheURLsConfigFilePath()); boolean exists = file.exists(); if (!exists) { file.getParentFile().mkdirs(); string = ""; } if (!exists || Utils.isEmpty(string) || j < System.currentTimeMillis() - getCacheURLsConfigTTL() || j < System.currentTimeMillis() - 604800000) { fetchCacheURLsMapping(string, file); } this.cacheURLMapping = getCacheURLMapping(); } public final Map getCacheURLMapping() { HashMap hashMap = new HashMap(); try { JSONArray jSONArray = new JSONObject(FileUtil.readFileToString(getCacheURLsConfigFilePath())).getJSONArray("url_paths"); for (int i = 0; i < jSONArray.length(); i++) { JSONObject jSONObject = jSONArray.getJSONObject(i); hashMap.put(jSONObject.getString("path"), Long.valueOf(jSONObject.optLong("ttl", SignalManager.TWENTY_FOUR_HOURS_MILLIS))); } } catch (Exception e) { HSLogger.e("resCacheMngr", "Error getting URLs mapping", e); } return hashMap; } public final long getCacheURLsConfigTTL() { try { return new JSONObject(FileUtil.readFileToString(getCacheURLsConfigFilePath())).optLong("ttl", SignalManager.TWENTY_FOUR_HOURS_MILLIS); } catch (Exception e) { HSLogger.e("resCacheMngr", "Error getting cache mapping ttl", e); return SignalManager.TWENTY_FOUR_HOURS_MILLIS; } } public final void fetchCacheURLsMapping(String str, File file) { HashMap hashMap = new HashMap(); if (Utils.isNotEmpty(str)) { hashMap.put("If-None-Match", str); } HSDownloaderResponse downloadResource = this.hsDownloaderNetwork.downloadResource(this.cacheUrlConfigRoute, hashMap, file); if (!downloadResource.isSuccess) { HSLogger.e("resCacheMngr", "Failed to download the URLs mapping file"); } else { setString("url_mapping_etag", downloadResource.etag); setLong("url_mapping_last_success_time", System.currentTimeMillis()); } } public InputStream fetchCachedResource(String str, String str2, String str3, Map map) { String str4 = "text/html"; String generateStorageKey = generateStorageKey(str2, str3); String str5 = generateStorageKey + "_last_success_time"; long j = this.resourceCacheSharedPref.getLong(str5); String str6 = generateStorageKey + "_etag"; String string = this.resourceCacheSharedPref.getString(str6); long tTLForResource = getTTLForResource(str2); String resourceCacheDirPath = getResourceCacheDirPath(); String str7 = resourceCacheDirPath + File.separator + generateStorageKey; File file = new File(str7); boolean exists = file.exists(); if (exists) { try { if (!Utils.isEmpty(string)) { if (j >= System.currentTimeMillis() - tTLForResource) { if (j < System.currentTimeMillis() - 604800000) { } return new BufferedInputStream(new FileInputStream(file)); } } } catch (Exception e) { HSLogger.e("resCacheMngr", "Error while fetching resource file: " + str, e); return null; } } if (!exists) { file.getParentFile().mkdirs(); string = ""; } File file2 = new File(str7 + "_temp"); if (Utils.isNotEmpty(string)) { map.put("If-None-Match", string); } HSDownloaderResponse downloadResource = this.hsDownloaderNetwork.downloadResource(str, map, file2); if (!downloadResource.isSuccess) { return null; } setString(str6, downloadResource.etag); setLong(str5, System.currentTimeMillis()); int i = downloadResource.status; if (i >= 200 && i <= 300) { file.delete(); if (!file2.renameTo(file)) { return null; } String str8 = generateStorageKey + "_mimetype"; String str9 = downloadResource.mimetype; if (!str9.contains("text/html")) { str4 = str9; } if (Utils.isNotEmpty(str4)) { setString(str8, str4); } setString(generateStorageKey + "_headers", downloadResource.headers.toString()); } deleteOlderCachedResource(resourceCacheDirPath, str2, file.getName()); return new BufferedInputStream(new FileInputStream(file)); } public final void deleteOlderCachedResource(String str, String str2, String str3) { File[] listFiles = new File(str).listFiles(); if (listFiles == null || listFiles.length == 0) { return; } String generateStorageKey = generateStorageKey(str2, null); for (File file : listFiles) { String name = file.getName(); if (!name.equals(str3) && this.resourceCacheEvictStrategy.shouldEvictCache(name, generateStorageKey)) { file.delete(); } } } public Map getCachedResponseHeadersForResource(String str, String str2) { return Utils.jsonStringToStringMap(getString(generateStorageKey(str, str2) + "_headers")); } public String getResourceMimeType(String str, String str2) { return this.resourceCacheSharedPref.getString(generateStorageKey(str, str2) + "_mimetype"); } public final String generateStorageKey(String str, String str2) { StringBuilder sb = new StringBuilder(); sb.append(str); sb.append("_"); if (str2 == null) { str2 = ""; } sb.append(str2); return sb.toString().replaceAll("[^a-zA-Z0-9]", "_"); } public final String getResourceCacheDirPath() { StringBuilder sb = new StringBuilder(); sb.append(this.appFileDirPath); String str = File.separator; sb.append(str); sb.append("helpshift"); sb.append(str); sb.append("resource_cache"); sb.append(str); sb.append(this.subdirPath); return sb.toString(); } public final String getCacheURLsConfigFilePath() { return getResourceCacheDirPath() + File.separator + this.cacheUrlConfigFileName; } public final void setString(String str, String str2) { this.resourceCacheSharedPref.putString(str, str2); } public final void setLong(String str, long j) { this.resourceCacheSharedPref.putLong(str, j); } public final String getString(String str) { return this.resourceCacheSharedPref.getString(str); } public final long getLong(String str) { return this.resourceCacheSharedPref.getLong(str); } public void deleteAllCachedFiles() { FileUtil.deleteDir(getResourceCacheDirPath()); this.resourceCacheSharedPref.clear(); this.cacheURLMapping.clear(); } }