From fd022742aa751da0a8d960db4809217b6940db0e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 23 Aug 2020 23:37:29 +0600 Subject: [PATCH] files downloading extensions in utils --- CHANGELOG.md | 3 +++ .../utils/extensions/FilesDownloading.kt | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FilesDownloading.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 42fbeafba4..c30b921637 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,9 @@ * `replyTo` * `chat` * `createResend` (several extensions) + * Several extensions for downloading of files: + * `HttpClient#loadFile` + * `PathedFile#download` ## 0.27.0 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FilesDownloading.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FilesDownloading.kt new file mode 100644 index 0000000000..d1f8208284 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FilesDownloading.kt @@ -0,0 +1,21 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.extensions + +import com.github.insanusmokrassar.TelegramBotAPI.types.files.PathedFile +import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper +import io.ktor.client.HttpClient +import io.ktor.client.request.get + +suspend fun HttpClient.loadFile( + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, + filePath: String +) = get("${telegramAPIUrlsKeeper.fileBaseUrl}/$filePath") + +suspend fun HttpClient.loadFile( + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, + pathedFile: PathedFile +) = loadFile(telegramAPIUrlsKeeper, pathedFile.filePath) + +suspend fun PathedFile.download( + telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, + client: HttpClient = HttpClient() +) = client.loadFile(telegramAPIUrlsKeeper, this)