convertToStorageFile

This commit is contained in:
InsanusMokrassar 2021-08-13 11:37:01 +06:00
parent f7d2c8bbd2
commit 9eb6008a73
4 changed files with 93 additions and 0 deletions

View File

@ -14,6 +14,8 @@
`import dev.inmo.tgbotapi.extensions.api.downloadFile` with `import dev.inmo.tgbotapi.extensions.api.files.downloadFile`
* `PathedFile#filename` extension has been deprecated, and new property `PathedFile#fileName` has been included
directly in `PathedFile`
* `Utils`:
* Add several functions `convertToStorageFile` and extensions `TelegramBot#convertToStorageFile`
## 0.35.4 Hotfix

View File

@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.get.GetFile
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
suspend fun TelegramBot.getFileAdditionalInfo(
fileId: FileId
@ -14,3 +15,7 @@ suspend fun TelegramBot.getFileAdditionalInfo(
suspend fun TelegramBot.getFileAdditionalInfo(
file: TelegramMediaFile
) = getFileAdditionalInfo(file.fileId)
suspend fun TelegramBot.getFileAdditionalInfo(
content: MediaContent
) = getFileAdditionalInfo(content.media)

View File

@ -0,0 +1,45 @@
package dev.inmo.tgbotapi.extensions.utils.types.files
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.DownloadFileStream
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.get.GetFile
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 dev.inmo.tgbotapi.utils.*
suspend fun convertToStorageFile(
downloadStreamAllocator: ByteReadChannelAllocator,
pathedFile: PathedFile,
mimeType: MimeType
): StorageFile {
return downloadStreamAllocator.asStorageFile(
pathedFile.fileName,
mimeType
)
}
suspend fun TelegramBot.convertToStorageFile(
pathedFile: PathedFile,
mimeType: MimeType
): StorageFile = convertToStorageFile(
execute(DownloadFileStream(pathedFile.filePath)),
pathedFile,
mimeType
)
suspend fun TelegramBot.convertToStorageFile(
fileId: FileId,
mimeType: MimeType
): StorageFile = convertToStorageFile(execute(GetFile(fileId)), mimeType)
suspend fun TelegramBot.convertToStorageFile(
file: TelegramMediaFile,
mimeType: MimeType
): StorageFile = convertToStorageFile(file.fileId, mimeType)
suspend fun TelegramBot.convertToStorageFile(
content: MediaContent,
mimeType: MimeType
): StorageFile = convertToStorageFile(content.media, mimeType)

View File

@ -0,0 +1,41 @@
package dev.inmo.tgbotapi.types.files
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.DownloadFileStream
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.requests.get.GetFile
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
import dev.inmo.tgbotapi.utils.*
import java.nio.file.Files
import kotlin.io.path.Path
suspend fun convertToStorageFile(
downloadStreamAllocator: ByteReadChannelAllocator,
pathedFile: PathedFile
): StorageFile {
return downloadStreamAllocator.asStorageFile(
pathedFile.fileName,
Files.probeContentType(Path(pathedFile.fileName)).asMimeType()
)
}
suspend fun TelegramBot.convertToStorageFile(
pathedFile: PathedFile
): StorageFile = convertToStorageFile(
execute(DownloadFileStream(pathedFile.filePath)),
pathedFile
)
suspend fun TelegramBot.convertToStorageFile(
fileId: FileId
): StorageFile = convertToStorageFile(execute(GetFile(fileId)))
suspend fun TelegramBot.convertToStorageFile(
file: TelegramMediaFile
): StorageFile = convertToStorageFile(file.fileId)
suspend fun TelegramBot.convertToStorageFile(
content: MediaContent
): StorageFile = convertToStorageFile(content.media)