several refactoring things and docs appending

This commit is contained in:
InsanusMokrassar 2021-01-09 21:50:02 +06:00
parent a882a212c2
commit 7aa3ff180e
2 changed files with 29 additions and 6 deletions

View File

@ -25,10 +25,9 @@ object MultipartRequestCallFactory : AbstractRequestCallFactory() {
Headers.build {
append(HttpHeaders.ContentType, value.mimeType)
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
}
) {
value.file.asInput()
}
},
block = value.file::input
)
is FileId -> append(key, value.fileId)
else -> append(key, value.toString())
}

View File

@ -5,19 +5,40 @@ import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable
/**
* Information about file for [StorageFile]
*
* @param contentType Raw type like "application/json"
* @param fileName This filename will be used in telegram system as name of file
*/
@Serializable
data class StorageFileInfo(
val contentType: String,
val fileName: String
) {
/**
* This methods is required for random generation of name for keeping warranties about unique file name
*/
fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}"
}
/**
* Contains info about file, which potentially can be sent to telegram system.
*
* @param storageFileInfo Information about this file
* @param inputSource Lambda which able to allocate [Input] for uploading/manipulating data
*
* @see StorageFileInfo
* @see asStorageFile
*/
data class StorageFile(
val storageFileInfo: StorageFileInfo,
private val inputSource: () -> Input
) {
fun asInput() = inputSource()
val input: Input
get() = inputSource()
@Deprecated("This method will be fully replaced with input property", ReplaceWith("input"))
fun asInput() = input
}
@Suppress("NOTHING_TO_INLINE")
@ -31,5 +52,8 @@ inline fun StorageFile(
ByteReadPacket(bytes)
}
@Suppress("NOTHING_TO_INLINE")
/**
*
*/
@Suppress("NOTHING_TO_INLINE", "unused")
inline fun ByteArray.asStorageFile(fileName: String, mimeType: MimeType) = StorageFile(fileName, this, mimeType)