mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
add download and downloadToTemporalFile extensions
This commit is contained in:
parent
76e214fc08
commit
8f25c123dd
@ -9,6 +9,7 @@
|
|||||||
* New universal `uniUpload` extension for `HttpClient`
|
* New universal `uniUpload` extension for `HttpClient`
|
||||||
* `Server`:
|
* `Server`:
|
||||||
* New universal `handleUniUpload` extension for `ApplicationCall`
|
* New universal `handleUniUpload` extension for `ApplicationCall`
|
||||||
|
* Add extensions `download` and `downloadToTemporalFile`
|
||||||
|
|
||||||
## 0.14.2
|
## 0.14.2
|
||||||
|
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
package dev.inmo.micro_utils.ktor.server
|
||||||
|
|
||||||
|
import com.benasher44.uuid.uuid4
|
||||||
|
import io.ktor.http.content.PartData
|
||||||
|
import io.ktor.utils.io.copyTo
|
||||||
|
import io.ktor.utils.io.core.copyTo
|
||||||
|
import io.ktor.utils.io.jvm.javaio.copyTo
|
||||||
|
import io.ktor.utils.io.streams.asOutput
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun PartData.FileItem.download(target: File) {
|
||||||
|
provider().use { input ->
|
||||||
|
target.outputStream().use {
|
||||||
|
input.copyTo(it.asOutput())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PartData.FileItem.downloadToTemporalFile(): File {
|
||||||
|
val outputFile = File.createTempFile(uuid4().toString(), ".temp").apply {
|
||||||
|
deleteOnExit()
|
||||||
|
}
|
||||||
|
download(outputFile)
|
||||||
|
return outputFile
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PartData.BinaryItem.download(target: File) {
|
||||||
|
provider().use { input ->
|
||||||
|
target.outputStream().use {
|
||||||
|
input.copyTo(it.asOutput())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PartData.BinaryItem.downloadToTemporalFile(): File {
|
||||||
|
val outputFile = File.createTempFile(uuid4().toString(), ".temp").apply {
|
||||||
|
deleteOnExit()
|
||||||
|
}
|
||||||
|
download(outputFile)
|
||||||
|
return outputFile
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun PartData.BinaryChannelItem.download(target: File) {
|
||||||
|
val input = provider()
|
||||||
|
target.outputStream().use {
|
||||||
|
input.copyTo(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun PartData.BinaryChannelItem.downloadToTemporalFile(): File {
|
||||||
|
val outputFile = File.createTempFile(uuid4().toString(), ".temp").apply {
|
||||||
|
deleteOnExit()
|
||||||
|
}
|
||||||
|
download(outputFile)
|
||||||
|
return outputFile
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user