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(
key,
Headers.build {
append(HttpHeaders.ContentType, value.mimeType)
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
},
block = value.file::input

View File

@ -48,29 +48,30 @@ object InputFileSerializer : KSerializer<InputFile> {
@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)

View File

@ -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)

View File

@ -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()
}

View File

@ -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)

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)