diff --git a/library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java b/library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java index d11b53c9..206efd7b 100644 --- a/library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java +++ b/library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java @@ -1,5 +1,8 @@ package com.pengrad.telegrambot.impl; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + /** * Stas Parshin * 16 October 2015 @@ -15,6 +18,13 @@ public FileApi(String token) { } public String getFullFilePath(String filePath) { - return apiUrl + filePath; + int slash = filePath.lastIndexOf('/') + 1; + String path = filePath.substring(0, slash); + String fileName = filePath.substring(slash); + try { + return apiUrl + path + URLEncoder.encode(fileName, "UTF-8").replace("+", "%20"); + } catch (UnsupportedEncodingException e) { + return apiUrl + filePath; + } } } diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 1f015371..4bd02e94 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -4,6 +4,9 @@ import com.pengrad.telegrambot.model.request.*; import com.pengrad.telegrambot.request.*; import com.pengrad.telegrambot.response.*; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import org.junit.Test; import java.io.File; @@ -45,6 +48,7 @@ public class TelegramBotTest { String videoFileId = "BAADAgADZAADuYNZSXhLnzJTZ2yvAg"; String photoFileId = "AgADAgADDKgxG7mDWUlvyFIJ9XfF9yszSw0ABBhVadWwbAK1z-wIAAEC"; String gifFileId = "CgADAgADfQADgNqgSTt9SzatJhc3Ag"; + String withSpaceFileId = "BAADAgADZwADkg-4SQI5WM0SPNHrAg"; public TelegramBotTest() throws IOException { String token, chat, forwardMessage, sticker; @@ -91,11 +95,14 @@ public void getUpdates() { } @Test - public void getFile() { - GetFileResponse response = bot.execute(new GetFile(audioFileId)); + public void getFile() throws IOException { + GetFileResponse response = bot.execute(new GetFile(withSpaceFileId)); FileTest.check(response.file()); String path = bot.getFullFilePath(response.file()); - assertTrue(path.contains(response.file().filePath())); + + Request request = new Request.Builder().head().url(path).build(); + Response pathResponse = new OkHttpClient().newCall(request).execute(); + assertTrue(pathResponse.isSuccessful()); } @Test