@@ -4,14 +4,20 @@ import android.content.Context
44import android.os.Handler
55import android.os.Looper
66import android.util.Log
7- import com.rajat.pdfviewer.util.CacheHelper
7+ import com.rajat.pdfviewer.util.CacheManager
88import com.rajat.pdfviewer.util.CacheStrategy
9- import com.rajat.pdfviewer.util.CommonUtils.Companion.MAX_CACHED_PDFS
109import com.rajat.pdfviewer.util.FileUtils.getCachedFileName
1110import com.rajat.pdfviewer.util.FileUtils.isValidPdf
1211import com.rajat.pdfviewer.util.FileUtils.writeFile
13- import kotlinx.coroutines.*
14- import okhttp3.*
12+ import kotlinx.coroutines.CoroutineScope
13+ import kotlinx.coroutines.Dispatchers
14+ import kotlinx.coroutines.delay
15+ import kotlinx.coroutines.launch
16+ import kotlinx.coroutines.withContext
17+ import okhttp3.OkHttpClient
18+ import okhttp3.Protocol
19+ import okhttp3.Request
20+ import okhttp3.Response
1521import java.io.File
1622import java.io.IOException
1723
@@ -52,16 +58,19 @@ class PdfDownloader(
5258
5359 private suspend fun checkAndDownload (downloadUrl : String ) {
5460 val cachedFileName = getCachedFileName(downloadUrl)
55- val cacheDir = File (listener.getContext().cacheDir, " ___pdf___cache___/ $cachedFileName " )
56- if (! cacheDir.exists()) {
57- cacheDir.mkdirs( )
61+
62+ if (cacheStrategy != CacheStrategy . DISABLE_CACHE ) {
63+ CacheManager .clearCacheDir(listener.getContext() )
5864 }
5965
60- val pdfFile = File (cacheDir, cachedFileName)
66+ val cacheDir = File (
67+ listener.getContext().cacheDir,
68+ " ___pdf___cache___/$cachedFileName "
69+ ).apply { mkdirs() }
6170
62- CacheHelper .handleCacheStrategy( TAG , cacheDir, cacheStrategy, cachedFileName, MAX_CACHED_PDFS )
71+ val pdfFile = File (cacheDir, cachedFileName )
6372
64- if (pdfFile.exists() && isValidPdf(pdfFile)) {
73+ if (cacheStrategy != CacheStrategy . DISABLE_CACHE && pdfFile.exists() && isValidPdf(pdfFile)) {
6574 withContext(Dispatchers .Main ) {
6675 listener.onDownloadSuccess(pdfFile)
6776 }
@@ -95,7 +104,12 @@ class PdfDownloader(
95104 delay(RETRY_DELAY )
96105 } else {
97106 withContext(Dispatchers .Main ) {
98- listener.onDownloadError(DownloadFailedException (" Failed after $MAX_RETRIES attempts" , e))
107+ listener.onDownloadError(
108+ DownloadFailedException (
109+ " Failed after $MAX_RETRIES attempts" ,
110+ e
111+ )
112+ )
99113 }
100114 }
101115 }
@@ -108,48 +122,49 @@ class PdfDownloader(
108122 message.contains(" Downloaded file is not a valid PDF" , ignoreCase = true )
109123 }
110124
111- private suspend fun downloadFile (downloadUrl : String , pdfFile : File ) = withContext(Dispatchers .IO ) {
112- val tempFile = File .createTempFile(" download_" , " .tmp" , pdfFile.parentFile)
125+ private suspend fun downloadFile (downloadUrl : String , pdfFile : File ) =
126+ withContext(Dispatchers .IO ) {
127+ val tempFile = File .createTempFile(" download_" , " .tmp" , pdfFile.parentFile)
113128
114- try {
115- if (pdfFile.exists() && ! isValidPdf(pdfFile)) {
116- pdfFile.delete()
117- }
129+ try {
130+ if (pdfFile.exists() && ! isValidPdf(pdfFile)) {
131+ pdfFile.delete()
132+ }
118133
119- val response = makeNetworkRequest(downloadUrl)
120- validateResponse(response)
134+ val response = makeNetworkRequest(downloadUrl)
135+ validateResponse(response)
121136
122- response.body?.use { body ->
123- body.byteStream().use { inputStream ->
124- writeFile(inputStream, tempFile, body.contentLength()) { progress ->
125- Handler (Looper .getMainLooper()).post {
126- listener.onDownloadProgress(progress, body.contentLength())
137+ response.body?.use { body ->
138+ body.byteStream().use { inputStream ->
139+ writeFile(inputStream, tempFile, body.contentLength()) { progress ->
140+ Handler (Looper .getMainLooper()).post {
141+ listener.onDownloadProgress(progress, body.contentLength())
142+ }
127143 }
128144 }
129- }
130- } ? : throw IOException (" Empty response body received for PDF" )
145+ } ? : throw IOException (" Empty response body received for PDF" )
131146
132- val renamed = tempFile.renameTo(pdfFile)
133- if (! renamed) {
134- tempFile.delete()
135- throw IOException (" Failed to rename temp file to final PDF path" )
136- }
147+ val renamed = tempFile.renameTo(pdfFile)
148+ if (! renamed) {
149+ tempFile.delete()
150+ throw IOException (" Failed to rename temp file to final PDF path" )
151+ }
137152
138- if (! isValidPdf(pdfFile)) {
139- pdfFile.delete()
140- throw InvalidPdfException (" Downloaded file is not a valid PDF" )
141- }
153+ if (! isValidPdf(pdfFile)) {
154+ pdfFile.delete()
155+ throw InvalidPdfException (" Downloaded file is not a valid PDF" )
156+ }
142157
143- Log .d(TAG , " Downloaded PDF to: ${pdfFile.absolutePath} " )
158+ Log .d(TAG , " Downloaded PDF to: ${pdfFile.absolutePath} " )
144159
145- withContext(Dispatchers .Main ) {
146- listener.onDownloadSuccess(pdfFile)
160+ withContext(Dispatchers .Main ) {
161+ listener.onDownloadSuccess(pdfFile)
162+ }
163+ } catch (e: Exception ) {
164+ tempFile.delete()
165+ throw e
147166 }
148- } catch (e: Exception ) {
149- tempFile.delete()
150- throw e
151167 }
152- }
153168
154169 private fun makeNetworkRequest (downloadUrl : String ): Response {
155170 val requestBuilder = Request .Builder ().url(downloadUrl)
@@ -164,11 +179,17 @@ class PdfDownloader(
164179 }
165180
166181 val contentType = response.header(" Content-Type" , " " )
167- if (! contentType.isNullOrEmpty() && ! contentType.contains(" application/pdf" , ignoreCase = true )) {
182+ if (! contentType.isNullOrEmpty() && ! contentType.contains(
183+ " application/pdf" ,
184+ ignoreCase = true
185+ )
186+ ) {
168187 throw InvalidPdfException (" Invalid content type received: $contentType . Expected a PDF file." )
169188 }
170189 }
171190}
172191
173- class DownloadFailedException (message : String , cause : Throwable ? = null ) : IOException(message, cause)
192+ class DownloadFailedException (message : String , cause : Throwable ? = null ) :
193+ IOException (message, cause)
194+
174195class InvalidPdfException (message : String ) : IOException(message)
0 commit comments