1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

StorageFile improvement

This commit is contained in:
InsanusMokrassar 2020-04-03 14:22:34 +06:00
parent a6aa4b8758
commit 21a15db031
6 changed files with 34 additions and 38 deletions

View File

@ -42,6 +42,10 @@
* `TelegramBotAPI`:
* `BotCommand` now will check and throw error in case when command or description lengths is/are incorrect
* `StorageFile` now is common for all platforms
* JavaScript realization was removed due to its redundancy
* JVM realization was replaced with `fun` factory
* `StorageFile` now able to accept any factory of `Input`
## 0.25.0

View File

@ -33,8 +33,8 @@ internal object InputFileSerializer : KSerializer<InputFile> {
@Serializable(InputFileSerializer::class)
data class MultipartFile (
val file: StorageFile,
val mimeType: String = file.contentType,
val filename: String = file.fileName
val mimeType: String = file.storageFileInfo.contentType,
val filename: String = file.storageFileInfo.fileName
) : InputFile() {
override val fileId: String = file.generateCustomName()
override val fileId: String = file.storageFileInfo.generateCustomName()
}

View File

@ -1,12 +1,21 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.benasher44.uuid.uuid4
import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
expect class StorageFile {
val contentType: String
data class StorageFileInfo(
val contentType: String,
val fileName: String
fun generateCustomName(): String
fun asInput(): Input
) {
fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}"
}
data class StorageFile(
val storageFileInfo: StorageFileInfo,
private val inputSource: () -> Input
) {
fun asInput() = inputSource()
}

View File

@ -0,0 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
private val extensionRegex = Regex("[^.]*$")
val String.fileExtension
get() = extensionRegex.find(this) ?.value ?: ""

View File

@ -1,19 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.benasher44.uuid.uuid4
import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
actual data class StorageFile(
actual val contentType: String,
actual val fileName: String,
@Transient
val inputGetter: () -> Input = throw IllegalStateException("Can't create object without input"),
@Transient
val extension: String = throw IllegalStateException("Can't create object without extension")
) {
actual fun asInput(): Input = inputGetter()
actual fun generateCustomName(): String = "${uuid4()}.$extension"
}

View File

@ -1,20 +1,16 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.benasher44.uuid.uuid4
import io.ktor.utils.io.core.Input
import io.ktor.utils.io.streams.asInput
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import java.io.File
import java.nio.file.Files
@Serializable
actual class StorageFile(
@Transient
private val file: File = throw IllegalStateException("Can't create object without file")
fun StorageFile(
file: File
) = StorageFile(
StorageFileInfo(
Files.probeContentType(file.toPath()),
file.name
)
) {
actual val contentType: String = Files.probeContentType(file.toPath())
actual val fileName: String = file.name
actual fun generateCustomName(): String = "${uuid4()}.${file.extension}"
actual fun asInput(): Input = Files.newInputStream(file.toPath()).asInput()
file.inputStream().asInput()
}