new asStorageFile

This commit is contained in:
InsanusMokrassar 2021-08-10 11:08:08 +06:00
parent 40d94cca70
commit 2049fea82a
6 changed files with 57 additions and 5 deletions

View File

@ -2,6 +2,8 @@
## 0.35.5
* `Core`:
* Several new extensions `ByteReadChannel#asStorageFile` and `ByteReadChannelAllocator#asStorageFile`
* `API`:
* New extensions `TelegramBot#downloadFile` for writing of incoming bytes to the file
* New extensions `TelegramBot#downloadFileStream` and `TelegramBot#downloadFileStreamAllocator` for getting of input

View File

@ -0,0 +1,6 @@
package dev.inmo.tgbotapi.utils
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.Input
expect suspend fun ByteReadChannel.asInput(): Input

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.utils
import com.benasher44.uuid.uuid4
import io.ktor.utils.io.*
import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable
@ -50,8 +51,30 @@ inline fun StorageFile(
ByteReadPacket(bytes)
}
/**
*
*/
@Suppress("NOTHING_TO_INLINE")
suspend inline fun StorageFile(
fileName: String,
byteReadChannel: ByteReadChannel,
mimeType: MimeType
) = StorageFile(
StorageFileInfo(mimeType.raw, fileName),
byteReadChannel.asInput().let { { it } }
)
@Suppress("NOTHING_TO_INLINE", "unused")
inline fun ByteArray.asStorageFile(fileName: String, mimeType: MimeType) = StorageFile(fileName, this, mimeType)
inline fun ByteArray.asStorageFile(
fileName: String,
mimeType: MimeType
) = StorageFile(fileName, this, mimeType)
@Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannel.asStorageFile(
fileName: String,
mimeType: MimeType
) = StorageFile(fileName, this, mimeType)
@Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannelAllocator.asStorageFile(
fileName: String,
mimeType: MimeType
) = this.invoke().asStorageFile(fileName, mimeType)

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.utils
import io.ktor.util.toByteArray
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.Input
actual suspend fun ByteReadChannel.asInput(): Input = ByteReadPacket(toByteArray())

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.utils
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.Input
import io.ktor.utils.io.jvm.javaio.toInputStream
import io.ktor.utils.io.streams.asInput
import kotlinx.coroutines.job
import kotlin.coroutines.coroutineContext
actual suspend fun ByteReadChannel.asInput(): Input = toInputStream(coroutineContext.job).asInput()

View File

@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.files.PathedFile
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
import io.ktor.util.cio.use
import io.ktor.util.cio.writeChannel
import io.ktor.utils.io.copyTo
import kotlinx.coroutines.job
@ -23,7 +24,9 @@ suspend fun TelegramBot.downloadFile(
destFile.parentFile.mkdirs()
doOutsideOfCoroutine { destFile.createNewFile() }
readChannel.copyTo(destFile.writeChannel(coroutineContext.job))
destFile.writeChannel(coroutineContext.job).use {
readChannel.copyTo(this)
}
return destFile
}