All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.dev.inmo.tgbotapi.extensions.api.files.DownloadFileToTempFile.kt Maven / Gradle / Ivy

Go to download

API extensions with "Telegram Bot API"-like extensions for TelegramBot and RequestsExecutor

There is a newer version: 20.0.1
Show newest version
package dev.inmo.tgbotapi.extensions.api.files

import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.common.filename
import dev.inmo.micro_utils.coroutines.doOutsideOfCoroutine
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.files.PathedFile
import dev.inmo.tgbotapi.types.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.content.MediaContent
import dev.inmo.tgbotapi.utils.fileExtension
import io.ktor.util.cio.use
import io.ktor.util.cio.writeChannel
import io.ktor.utils.io.copyTo
import kotlinx.coroutines.job
import java.io.File
import kotlin.coroutines.coroutineContext

public suspend fun TelegramBot.downloadFileToTemp(
    filePath: String
): File {
    return downloadFile(
        filePath,
        File.createTempFile(uuid4().toString(), "_temp").apply {
            deleteOnExit()
        }
    )
}

public suspend fun TelegramBot.downloadFileToTemp(
    pathedFile: PathedFile
): File = downloadFileToTemp(
    pathedFile.filePath
).run {
    val newFile = File(parentFile, "$nameWithoutExtension.${pathedFile.fileName.fileExtension}")
    val success = runCatching {
        renameTo(newFile)
    }.getOrElse { false }
    if (success) {
        newFile
    } else {
        this@run
    }
}

public suspend fun TelegramBot.downloadFileToTemp(
    fileId: FileId
): File = downloadFileToTemp(
    getFileAdditionalInfo(fileId)
)

public suspend fun TelegramBot.downloadFileToTemp(
    file: TelegramMediaFile
): File = downloadFileToTemp(
    getFileAdditionalInfo(file)
)

public suspend fun TelegramBot.downloadFileToTemp(
    file: MediaContent
): File = downloadFileToTemp(
    getFileAdditionalInfo(file.media)
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy