diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/MultipartRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/MultipartRequestCallFactory.kt index ca41189baf..2d42d15ea9 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/MultipartRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/MultipartRequestCallFactory.kt @@ -24,7 +24,6 @@ class MultipartRequestCallFactory : AbstractRequestCallFactory() { is MultipartFile -> appendInput( key, Headers.build { - append(HttpHeaders.ContentType, value.mimeType) append(HttpHeaders.ContentDisposition, "filename=${value.fileId}") }, block = value.file::input diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt index a024cdbcac..b306112118 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt @@ -48,29 +48,30 @@ object InputFileSerializer : KSerializer { @Serializable(InputFileSerializer::class) data class MultipartFile ( val file: StorageFile, - val mimeType: String = file.storageFileInfo.contentType, val filename: String = file.storageFileInfo.fileName ) : InputFile() { 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") inline fun StorageFile.asMultipartFile() = MultipartFile(this) +@Deprecated("This method is redundant. Use asMultipartFile without mime type") @Suppress("NOTHING_TO_INLINE", "unused") inline fun ByteArray.asMultipartFile( fileName: String, mimeType: MimeType -) = MultipartFile(asStorageFile(fileName, mimeType)) +) = MultipartFile(asStorageFile(fileName)) @Suppress("NOTHING_TO_INLINE", "unused") suspend inline fun ByteReadChannel.asMultipartFile( - fileName: String, - mimeType: MimeType -) = MultipartFile(asStorageFile(fileName, mimeType)) + fileName: String +) = MultipartFile(asStorageFile(fileName)) @Suppress("NOTHING_TO_INLINE", "unused") suspend inline fun ByteReadChannelAllocator.asMultipartFile( - fileName: String, - mimeType: MimeType -) = this.invoke().asMultipartFile(fileName, mimeType) + fileName: String +) = this.invoke().asMultipartFile(fileName) 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 1638729cd7..a67bb9f19e 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 @@ -14,9 +14,14 @@ import kotlinx.serialization.Serializable */ @Serializable data class StorageFileInfo( - val contentType: 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 */ @@ -40,13 +45,24 @@ data class StorageFile( get() = inputSource() } +@Deprecated("This constructor is redundant. Use constructor without mime type") @Suppress("NOTHING_TO_INLINE") inline fun StorageFile( fileName: String, bytes: ByteArray, mimeType: MimeType ) = 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) } @@ -54,27 +70,30 @@ inline fun StorageFile( @Suppress("NOTHING_TO_INLINE") suspend inline fun StorageFile( fileName: String, - byteReadChannel: ByteReadChannel, - mimeType: MimeType + byteReadChannel: ByteReadChannel ) = StorageFile( - StorageFileInfo(mimeType.raw, fileName), + StorageFileInfo(fileName), 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") inline fun ByteArray.asStorageFile( fileName: String, mimeType: MimeType -) = StorageFile(fileName, this, mimeType) +) = asStorageFile(fileName) @Suppress("NOTHING_TO_INLINE", "unused") suspend inline fun ByteReadChannel.asStorageFile( - fileName: String, - mimeType: MimeType -) = StorageFile(fileName, this, mimeType) + fileName: String +) = StorageFile(fileName, this) @Suppress("NOTHING_TO_INLINE", "unused") suspend inline fun ByteReadChannelAllocator.asStorageFile( - fileName: String, - mimeType: MimeType -) = this.invoke().asStorageFile(fileName, mimeType) + fileName: String +) = this.invoke().asStorageFile(fileName) diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt index 8d7ebe5c53..9d26befaeb 100644 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt @@ -7,10 +7,7 @@ import java.nio.file.Files fun StorageFile( file: File ) = StorageFile( - StorageFileInfo( - Files.probeContentType(file.toPath()), - file.name - ) + StorageFileInfo(file.name) ) { file.inputStream().asInput() } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/ContentAsStorageFile.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/ContentAsStorageFile.kt index b9716833eb..53b192e74e 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/ContentAsStorageFile.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/ContentAsStorageFile.kt @@ -11,35 +11,28 @@ import dev.inmo.tgbotapi.utils.* suspend fun convertToStorageFile( downloadStreamAllocator: ByteReadChannelAllocator, - pathedFile: PathedFile, - mimeType: MimeType + pathedFile: PathedFile ): StorageFile { return downloadStreamAllocator.asStorageFile( - pathedFile.fileName, - mimeType + pathedFile.fileName ) } suspend fun TelegramBot.convertToStorageFile( - pathedFile: PathedFile, - mimeType: MimeType + pathedFile: PathedFile ): StorageFile = convertToStorageFile( execute(DownloadFileStream(pathedFile.filePath)), - pathedFile, - mimeType + pathedFile ) suspend fun TelegramBot.convertToStorageFile( - fileId: FileId, - mimeType: MimeType -): StorageFile = convertToStorageFile(execute(GetFile(fileId)), mimeType) + fileId: FileId +): StorageFile = convertToStorageFile(execute(GetFile(fileId))) suspend fun TelegramBot.convertToStorageFile( - file: TelegramMediaFile, - mimeType: MimeType -): StorageFile = convertToStorageFile(file.fileId, mimeType) + file: TelegramMediaFile +): StorageFile = convertToStorageFile(file.fileId) suspend fun TelegramBot.convertToStorageFile( - content: MediaContent, - mimeType: MimeType -): StorageFile = convertToStorageFile(content.media, mimeType) + content: MediaContent +): StorageFile = convertToStorageFile(content.media) diff --git a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/AdditionalContentAsStorageFile.kt b/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/AdditionalContentAsStorageFile.kt deleted file mode 100644 index 927e7d4c19..0000000000 --- a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/files/AdditionalContentAsStorageFile.kt +++ /dev/null @@ -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)