1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

remove mime types from requests

This commit is contained in:
InsanusMokrassar 2021-08-13 14:02:39 +06:00
parent 58dcbe8751
commit e75c93d626
6 changed files with 51 additions and 87 deletions

View File

@ -24,7 +24,6 @@ class MultipartRequestCallFactory : AbstractRequestCallFactory() {
is MultipartFile -> appendInput( is MultipartFile -> appendInput(
key, key,
Headers.build { Headers.build {
append(HttpHeaders.ContentType, value.mimeType)
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}") append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
}, },
block = value.file::input block = value.file::input

View File

@ -48,29 +48,30 @@ object InputFileSerializer : KSerializer<InputFile> {
@Serializable(InputFileSerializer::class) @Serializable(InputFileSerializer::class)
data class MultipartFile ( data class MultipartFile (
val file: StorageFile, val file: StorageFile,
val mimeType: String = file.storageFileInfo.contentType,
val filename: String = file.storageFileInfo.fileName val filename: String = file.storageFileInfo.fileName
) : InputFile() { ) : InputFile() {
override val fileId: String = file.storageFileInfo.generateCustomName() override val fileId: String = file.storageFileInfo.generateCustomName()
@Deprecated("This constructor is redundant. Use constructor without mime type")
constructor(file: StorageFile, mimeType: String, filename: String): this(file, filename)
} }
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
inline fun StorageFile.asMultipartFile() = MultipartFile(this) inline fun StorageFile.asMultipartFile() = MultipartFile(this)
@Deprecated("This method is redundant. Use asMultipartFile without mime type")
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
inline fun ByteArray.asMultipartFile( inline fun ByteArray.asMultipartFile(
fileName: String, fileName: String,
mimeType: MimeType mimeType: MimeType
) = MultipartFile(asStorageFile(fileName, mimeType)) ) = MultipartFile(asStorageFile(fileName))
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannel.asMultipartFile( suspend inline fun ByteReadChannel.asMultipartFile(
fileName: String, fileName: String
mimeType: MimeType ) = MultipartFile(asStorageFile(fileName))
) = MultipartFile(asStorageFile(fileName, mimeType))
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannelAllocator.asMultipartFile( suspend inline fun ByteReadChannelAllocator.asMultipartFile(
fileName: String, fileName: String
mimeType: MimeType ) = this.invoke().asMultipartFile(fileName)
) = this.invoke().asMultipartFile(fileName, mimeType)

View File

@ -14,9 +14,14 @@ import kotlinx.serialization.Serializable
*/ */
@Serializable @Serializable
data class StorageFileInfo( data class StorageFileInfo(
val contentType: String,
val fileName: String val fileName: String
) { ) {
@Deprecated("This constructor is redundant. Use constructor without mime type")
constructor(
contentType: String,
fileName: String
): this(fileName)
/** /**
* This methods is required for random generation of name for keeping warranties about unique file name * This methods is required for random generation of name for keeping warranties about unique file name
*/ */
@ -40,13 +45,24 @@ data class StorageFile(
get() = inputSource() get() = inputSource()
} }
@Deprecated("This constructor is redundant. Use constructor without mime type")
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun StorageFile( inline fun StorageFile(
fileName: String, fileName: String,
bytes: ByteArray, bytes: ByteArray,
mimeType: MimeType mimeType: MimeType
) = StorageFile( ) = StorageFile(
StorageFileInfo(mimeType.raw, fileName) StorageFileInfo(fileName)
) {
ByteReadPacket(bytes)
}
@Suppress("NOTHING_TO_INLINE")
inline fun StorageFile(
fileName: String,
bytes: ByteArray
) = StorageFile(
StorageFileInfo(fileName)
) { ) {
ByteReadPacket(bytes) ByteReadPacket(bytes)
} }
@ -54,27 +70,30 @@ inline fun StorageFile(
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
suspend inline fun StorageFile( suspend inline fun StorageFile(
fileName: String, fileName: String,
byteReadChannel: ByteReadChannel, byteReadChannel: ByteReadChannel
mimeType: MimeType
) = StorageFile( ) = StorageFile(
StorageFileInfo(mimeType.raw, fileName), StorageFileInfo(fileName),
byteReadChannel.asInput().let { { it } } byteReadChannel.asInput().let { { it } }
) )
@Suppress("NOTHING_TO_INLINE", "unused")
inline fun ByteArray.asStorageFile(
fileName: String
) = StorageFile(fileName, this)
@Deprecated("This constructor is redundant. Use constructor without mime type")
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
inline fun ByteArray.asStorageFile( inline fun ByteArray.asStorageFile(
fileName: String, fileName: String,
mimeType: MimeType mimeType: MimeType
) = StorageFile(fileName, this, mimeType) ) = asStorageFile(fileName)
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannel.asStorageFile( suspend inline fun ByteReadChannel.asStorageFile(
fileName: String, fileName: String
mimeType: MimeType ) = StorageFile(fileName, this)
) = StorageFile(fileName, this, mimeType)
@Suppress("NOTHING_TO_INLINE", "unused") @Suppress("NOTHING_TO_INLINE", "unused")
suspend inline fun ByteReadChannelAllocator.asStorageFile( suspend inline fun ByteReadChannelAllocator.asStorageFile(
fileName: String, fileName: String
mimeType: MimeType ) = this.invoke().asStorageFile(fileName)
) = this.invoke().asStorageFile(fileName, mimeType)

View File

@ -7,10 +7,7 @@ import java.nio.file.Files
fun StorageFile( fun StorageFile(
file: File file: File
) = StorageFile( ) = StorageFile(
StorageFileInfo( StorageFileInfo(file.name)
Files.probeContentType(file.toPath()),
file.name
)
) { ) {
file.inputStream().asInput() file.inputStream().asInput()
} }

View File

@ -11,35 +11,28 @@ import dev.inmo.tgbotapi.utils.*
suspend fun convertToStorageFile( suspend fun convertToStorageFile(
downloadStreamAllocator: ByteReadChannelAllocator, downloadStreamAllocator: ByteReadChannelAllocator,
pathedFile: PathedFile, pathedFile: PathedFile
mimeType: MimeType
): StorageFile { ): StorageFile {
return downloadStreamAllocator.asStorageFile( return downloadStreamAllocator.asStorageFile(
pathedFile.fileName, pathedFile.fileName
mimeType
) )
} }
suspend fun TelegramBot.convertToStorageFile( suspend fun TelegramBot.convertToStorageFile(
pathedFile: PathedFile, pathedFile: PathedFile
mimeType: MimeType
): StorageFile = convertToStorageFile( ): StorageFile = convertToStorageFile(
execute(DownloadFileStream(pathedFile.filePath)), execute(DownloadFileStream(pathedFile.filePath)),
pathedFile, pathedFile
mimeType
) )
suspend fun TelegramBot.convertToStorageFile( suspend fun TelegramBot.convertToStorageFile(
fileId: FileId, fileId: FileId
mimeType: MimeType ): StorageFile = convertToStorageFile(execute(GetFile(fileId)))
): StorageFile = convertToStorageFile(execute(GetFile(fileId)), mimeType)
suspend fun TelegramBot.convertToStorageFile( suspend fun TelegramBot.convertToStorageFile(
file: TelegramMediaFile, file: TelegramMediaFile
mimeType: MimeType ): StorageFile = convertToStorageFile(file.fileId)
): StorageFile = convertToStorageFile(file.fileId, mimeType)
suspend fun TelegramBot.convertToStorageFile( suspend fun TelegramBot.convertToStorageFile(
content: MediaContent, content: MediaContent
mimeType: MimeType ): StorageFile = convertToStorageFile(content.media)
): StorageFile = convertToStorageFile(content.media, mimeType)

View File

@ -1,45 +0,0 @@
package dev.inmo.tgbotapi.extensions.utils.types.files
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.asMimedMediaFile
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.*
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 = file.asMimedMediaFile() ?.mimeType ?.let {
convertToStorageFile(file.fileId, it)
} ?: convertToStorageFile(file.fileId)
suspend fun TelegramBot.convertToStorageFile(
content: MediaContent
): StorageFile = convertToStorageFile(content.media)