mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-04 03:14:40 -05:00
Delete tmp files on request failure (#582)
There is a possibility that a partially downloaded file remains in case of an error. In that case, the next time the image gets requested the existing file would be handled as a successfully cached image.
This commit is contained in:
@@ -11,6 +11,7 @@ import okhttp3.Response
|
|||||||
import okhttp3.internal.closeQuietly
|
import okhttp3.internal.closeQuietly
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
object ImageResponse {
|
object ImageResponse {
|
||||||
@@ -37,12 +38,18 @@ object ImageResponse {
|
|||||||
* @param cacheSavePath where to save the cached image. Caller should decide to use perma cache or temp cache (OS temp dir)
|
* @param cacheSavePath where to save the cached image. Caller should decide to use perma cache or temp cache (OS temp dir)
|
||||||
* @param fileName what the saved cache file should be named
|
* @param fileName what the saved cache file should be named
|
||||||
*/
|
*/
|
||||||
suspend fun getCachedImageResponse(saveDir: String, fileName: String, fetcher: suspend () -> Response): Pair<InputStream, String> {
|
suspend fun getCachedImageResponse(
|
||||||
|
saveDir: String,
|
||||||
|
fileName: String,
|
||||||
|
fetcher: suspend () -> Response
|
||||||
|
): Pair<InputStream, String> {
|
||||||
File(saveDir).mkdirs()
|
File(saveDir).mkdirs()
|
||||||
|
|
||||||
val cachedFile = findFileNameStartingWith(saveDir, fileName)
|
val cachedFile = findFileNameStartingWith(saveDir, fileName)
|
||||||
val filePath = "$saveDir/$fileName"
|
val filePath = "$saveDir/$fileName"
|
||||||
if (cachedFile != null) {
|
|
||||||
|
// in case the cached file is a ".tmp" file something went wrong with the previous download, and it has to be downloaded again
|
||||||
|
if (cachedFile != null && !cachedFile.endsWith(".tmp")) {
|
||||||
val fileType = cachedFile.substringAfter("$filePath.")
|
val fileType = cachedFile.substringAfter("$filePath.")
|
||||||
return Pair(
|
return Pair(
|
||||||
pathToInputStream(cachedFile),
|
pathToInputStream(cachedFile),
|
||||||
@@ -52,12 +59,19 @@ object ImageResponse {
|
|||||||
|
|
||||||
val response = fetcher()
|
val response = fetcher()
|
||||||
|
|
||||||
if (response.code == 200) {
|
try {
|
||||||
val (actualSavePath, imageType) = saveImage(filePath, response.body!!.byteStream())
|
if (response.code == 200) {
|
||||||
return pathToInputStream(actualSavePath) to imageType
|
val (actualSavePath, imageType) = saveImage(filePath, response.body.byteStream())
|
||||||
} else {
|
return pathToInputStream(actualSavePath) to imageType
|
||||||
|
} else {
|
||||||
|
throw Exception("request error! ${response.code}")
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
// make sure no partial download remains
|
||||||
|
clearCachedImage(saveDir, fileName)
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
response.closeQuietly()
|
response.closeQuietly()
|
||||||
throw Exception("request error! ${response.code}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user