Skip to content

Commit 09d63f0

Browse files
committed
feat: clean PDF.js cache if needed
1 parent bfc184c commit 09d63f0

1 file changed

Lines changed: 12 additions & 50 deletions

File tree

src/main/java/com.outsystems.plugins.inappbrowser/osinappbrowserlib/views/OSIABWebViewActivity.kt

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ import androidx.lifecycle.lifecycleScope
3838
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents
3939
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents.OSIABWebViewEvent
4040
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.R
41+
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.helpers.OSIABPdfHelper
4142
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABToolbarPosition
4243
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABWebViewOptions
4344
import kotlinx.coroutines.Dispatchers
4445
import kotlinx.coroutines.launch
4546
import kotlinx.coroutines.withContext
46-
import java.io.File
4747
import java.io.IOException
48-
import java.net.HttpURLConnection
49-
import java.net.URL
5048

5149
class OSIABWebViewActivity : AppCompatActivity() {
5250

@@ -219,8 +217,8 @@ class OSIABWebViewActivity : AppCompatActivity() {
219217

220218
private fun handleLoadUrl(url: String, additionalHttpHeaders: Map<String, String>? = null) {
221219
lifecycleScope.launch(Dispatchers.IO) {
222-
if (isContentTypeApplicationPdf(url)) {
223-
val pdfFile = try { downloadPdfToCache(url) } catch (_: IOException) { null }
220+
if (OSIABPdfHelper.isContentTypeApplicationPdf(url)) {
221+
val pdfFile = try { OSIABPdfHelper.downloadPdfToCache(this@OSIABWebViewActivity, url) } catch (_: IOException) { null }
224222
if (pdfFile != null) {
225223
withContext(Dispatchers.Main) {
226224
webView.stopLoading()
@@ -239,51 +237,6 @@ class OSIABWebViewActivity : AppCompatActivity() {
239237
}
240238
}
241239

242-
fun isContentTypeApplicationPdf(urlString: String): Boolean {
243-
return try {
244-
// Try to identify if the URL is a PDF using a HEAD request.
245-
// If the server does not implement HEAD correctly or does not return the expected content-type,
246-
// fall back to a GET request, since some servers only return the correct type for GET.
247-
if (checkPdfByRequest(urlString, method = "HEAD")) {
248-
true
249-
} else {
250-
checkPdfByRequest(urlString, method = "GET")
251-
}
252-
} catch (_: Exception) {
253-
false
254-
}
255-
}
256-
257-
private fun checkPdfByRequest(urlString: String, method: String): Boolean {
258-
var conn: HttpURLConnection? = null
259-
return try {
260-
conn = (URL(urlString).openConnection() as? HttpURLConnection)
261-
conn?.run {
262-
instanceFollowRedirects = true
263-
requestMethod = method
264-
if (method == "GET") {
265-
setRequestProperty("Range", "bytes=0-0")
266-
}
267-
connect()
268-
val type = contentType?.lowercase()
269-
val disposition = getHeaderField("Content-Disposition")?.lowercase()
270-
type == "application/pdf" ||
271-
(type.isNullOrEmpty() && disposition?.contains(".pdf") == true)
272-
} ?: false
273-
} finally {
274-
conn?.disconnect()
275-
}
276-
}
277-
278-
private fun downloadPdfToCache(url: String): File {
279-
val pdfFile = File(cacheDir, "temp_${System.currentTimeMillis()}.pdf")
280-
URL(url).openStream().use { input ->
281-
pdfFile.outputStream().use { output ->
282-
input.copyTo(output)
283-
}
284-
}
285-
return pdfFile
286-
}
287240

288241
/**
289242
* Helper function to update navigation button states
@@ -418,6 +371,15 @@ class OSIABWebViewActivity : AppCompatActivity() {
418371
sendWebViewEvent(OSIABEvents.BrowserPageNavigationCompleted(browserId, resolvedUrl))
419372
}
420373

374+
if (url?.startsWith(PDF_VIEWER_URL_PREFIX) == true) {
375+
if (options.clearCache) {
376+
webView.evaluateJavascript(
377+
"localStorage.clear(); sessionStorage.clear();", null
378+
);
379+
}
380+
}
381+
382+
421383
// set back to false so that the next successful load
422384
// if the load fails, onReceivedError takes care of setting it back to true
423385
hasLoadError = false

0 commit comments

Comments
 (0)