[Review] FileCacheStrategy: delete temp file on closure

This commit is contained in:
Him188 2021-01-08 09:44:29 +08:00
parent 2abeb329f6
commit 98545e6f6f

View File

@ -82,10 +82,15 @@ public interface FileCacheStrategy {
@Throws(IOException::class)
override fun newCache(input: InputStream, formatName: String?): ExternalResource {
return createTempFile().apply {
val file = createTempFile()
return file.apply {
deleteOnExit()
outputStream().use { out -> input.copyTo(out) }
}.toExternalResource(formatName)
}.toExternalResource(formatName).apply {
closed.invokeOnCompletion {
kotlin.runCatching { file.delete() }
}
}
}
}