@@ -578,19 +578,28 @@ object BookDownloader2Helper {
578
578
}
579
579
rFile.parentFile?.mkdirs()
580
580
if (rFile.isDirectory) rFile.delete()
581
-
581
+ val rateLimit = api.rateLimitTime > 0
582
582
for (i in 0 .. maxTries) {
583
- val page = api.loadHtml(data.url)
584
-
585
- if (! page.isNullOrBlank()) {
586
- rFile.createNewFile() // only create the file when actually needed
587
- rFile.writeText(" ${data.name} \n ${page} " )
588
- return @withContext true
589
- } else {
590
- delay(5000 ) // ERROR
583
+ if (rateLimit) {
584
+ api.api.rateLimitMutex.lock()
591
585
}
592
- if (api.rateLimitTime > 0 ) {
593
- delay(api.rateLimitTime)
586
+ try {
587
+ val page = api.loadHtml(data.url)
588
+
589
+ if (! page.isNullOrBlank()) {
590
+ rFile.createNewFile() // only create the file when actually needed
591
+ rFile.writeText(" ${data.name} \n ${page} " )
592
+ return @withContext true
593
+ } else {
594
+ delay(5000 ) // ERROR
595
+ }
596
+ if (api.rateLimitTime > 0 ) {
597
+ delay(api.rateLimitTime)
598
+ }
599
+ } finally {
600
+ if (rateLimit) {
601
+ api.api.rateLimitMutex.unlock()
602
+ }
594
603
}
595
604
}
596
605
return @withContext false
@@ -852,7 +861,6 @@ object NotificationHelper {
852
861
.setColor(context.colorFromAttribute(R .attr.colorPrimary))
853
862
.setContentText(
854
863
if (stateProgressState.total > 1 ) {
855
-
856
864
val extra = if (progressInBytes) {
857
865
val bytesToKiloBytes = 1024
858
866
" ${stateProgressState.progress / bytesToKiloBytes} Kb/${stateProgressState.total / bytesToKiloBytes} Kb"
@@ -985,23 +993,29 @@ object ImageDownloader {
985
993
private val cachedBitmaps = hashMapOf<String , Bitmap >()
986
994
987
995
suspend fun getImageBitmapFromUrl (url : String ): Bitmap ? {
988
- cachedBitmapMutex.withLock {
989
- if (cachedBitmaps.containsKey(url)) {
990
- return cachedBitmaps[url]
996
+ try {
997
+ cachedBitmapMutex.withLock {
998
+ if (cachedBitmaps.containsKey(url)) {
999
+ return cachedBitmaps[url]
1000
+ }
991
1001
}
992
- }
993
1002
994
- val bitmap =
995
- withContext(Dispatchers .IO ) {
996
- Glide .with (activity ? : return @withContext null )
997
- .asBitmap()
998
- .load(url).submit(720 , 720 ).get()
999
- } ? : return null
1003
+ val bitmap =
1004
+ withContext(Dispatchers .IO ) {
1005
+ Glide .with (activity ? : return @withContext null )
1006
+ .asBitmap()
1007
+ .load(url).submit(720 , 720 ).get()
1008
+ } ? : return null
1000
1009
1001
- cachedBitmapMutex.withLock {
1002
- cachedBitmaps[url] = bitmap
1010
+ cachedBitmapMutex.withLock {
1011
+ cachedBitmaps[url] = bitmap
1012
+ }
1013
+ return bitmap
1014
+ } catch (t: Throwable ) {
1015
+ logError(t)
1016
+ return null
1003
1017
}
1004
- return bitmap
1018
+
1005
1019
}
1006
1020
}
1007
1021
@@ -1681,8 +1695,8 @@ object BookDownloader2 {
1681
1695
pFile.writeBytes(bytes)
1682
1696
}
1683
1697
}
1684
- } catch (e : Exception ) {
1685
- logError(e )
1698
+ } catch (t : Throwable ) {
1699
+ logError(t )
1686
1700
// delay(1000)
1687
1701
}
1688
1702
}
@@ -1801,6 +1815,8 @@ object BookDownloader2 {
1801
1815
progressState
1802
1816
)
1803
1817
}
1818
+ } catch (t: Throwable ) {
1819
+ logError(t)
1804
1820
} finally {
1805
1821
currentDownloadsMutex.withLock {
1806
1822
currentDownloads - = id
0 commit comments