This commit is contained in:
Aria Moradi 2021-05-28 20:21:05 +04:30
parent 1a99ec76e4
commit e5e875c54a

View File

@ -42,19 +42,20 @@ object CachedImageResponse {
val response = fetcher() val response = fetcher()
if (response.code == 200) { if (response.code == 200) {
val fullPath = "$filePath.tmp" val tmpSavePath = "$filePath.tmp"
val saveFile = File(fullPath) val tmpSaveFile = File(tmpSavePath)
response.body!!.source().saveTo(saveFile) response.body!!.source().saveTo(tmpSaveFile)
// find image type // find image type
val imageType = response.headers["content-type"] val imageType = response.headers["content-type"]
?: ImageUtil.findImageType { saveFile.inputStream() }?.mime ?: ImageUtil.findImageType { tmpSaveFile.inputStream() }?.mime
?: "image/jpeg" ?: "image/jpeg"
.substringAfter("image/")
saveFile.renameTo(File("$filePath.$imageType")) val actualSavePath = "$filePath.${imageType.substringAfter("/")}"
return pathToInputStream(fullPath) to imageType tmpSaveFile.renameTo(File(actualSavePath))
return pathToInputStream(actualSavePath) to imageType
} else { } else {
response.closeQuietly() response.closeQuietly()
throw Exception("request error! ${response.code}") throw Exception("request error! ${response.code}")