From c91426a910823b0b3028873d28b82d3770c0a56a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 14 Aug 2020 19:26:51 +0600 Subject: [PATCH] filename created --- CHANGELOG.md | 3 +++ .../TelegramBotAPI/types/files/PathedFile.kt | 4 +++- .../TelegramBotAPI/utils/StringFileExtension.kt | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8fbffb0d7..082c23aba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ ### 0.27.11 +* `TelegramBotAPI`: + * Extension `String#filenameFromUrl` was created + * Extension `PathedFile#filename` was created * `TelegramBotAPI-extensions-utils`: * `Flow>.flatMap` extension was added * Extensions for `FlowUpdatesFilter` were added: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt index 24331c325a..06c755b2bc 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* -import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper +import com.github.insanusmokrassar.TelegramBotAPI.utils.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -20,5 +20,7 @@ data class PathedFile( override val fileSize: Long? = null ): TelegramMediaFile +val PathedFile.filename: FileName + get() = filePath.filenameFromUrl fun TelegramAPIUrlsKeeper.resolveFileURL(file: PathedFile): String = "$fileBaseUrl/${file.filePath}" fun PathedFile.fullUrl(keeper: TelegramAPIUrlsKeeper): String = keeper.resolveFileURL(this) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt index 5762b04a6c..1b9baab853 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt @@ -1,6 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils +private val filenameRegex = Regex("[^/]*$") private val extensionRegex = Regex("[^.]*$") +/** + * File name like hello.jpg + */ +typealias FileName = String + +val String.filenameFromUrl: FileName + get() = filenameRegex.find(this) ?.value ?: "" + val String.fileExtension get() = extensionRegex.find(this) ?.value ?: ""