diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ddd062828..8a921a4196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/ByteReadChannelToInput.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/ByteReadChannelToInput.kt new file mode 100644 index 0000000000..e12b6bae15 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/ByteReadChannelToInput.kt @@ -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 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt index 6db07683ee..1638729cd7 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt @@ -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) diff --git a/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt b/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt new file mode 100644 index 0000000000..b64c816a97 --- /dev/null +++ b/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt @@ -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()) diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt new file mode 100644 index 0000000000..a68c1f8639 --- /dev/null +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/asInput.kt @@ -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() diff --git a/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFileToFile.kt b/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFileToFile.kt index a0934f5247..0e12b4c855 100644 --- a/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFileToFile.kt +++ b/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFileToFile.kt @@ -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 }