import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.runInterruptible
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FilterOutputStream
suspend fun ApplicationCall.respondCompressedFile(file: File) {
val cache = compressedCache() ?: return respondFile(file)
response.header(HttpHeaders.ContentEncoding, cache.encoding)
- respondBytes(cache.getCompressed(file))
+ val compressedBytes = runInterruptible(Dispatchers.IO) { cache.getCompressed(file) }
+ respondBytes(compressedBytes)
}
private class CompressedCache(val encoding: String, private val compressor: (ByteArray) -> ByteArray) {