Skip to content

Commit 941e2ba

Browse files
committed
Encode file path url #90
1 parent 4e2e360 commit 941e2ba

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.pengrad.telegrambot.impl;
22

3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLEncoder;
5+
36
/**
47
* Stas Parshin
58
* 16 October 2015
@@ -15,6 +18,13 @@ public FileApi(String token) {
1518
}
1619

1720
public String getFullFilePath(String filePath) {
18-
return apiUrl + filePath;
21+
int slash = filePath.lastIndexOf('/') + 1;
22+
String path = filePath.substring(0, slash);
23+
String fileName = filePath.substring(slash);
24+
try {
25+
return apiUrl + path + URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
26+
} catch (UnsupportedEncodingException e) {
27+
return apiUrl + filePath;
28+
}
1929
}
2030
}

library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import com.pengrad.telegrambot.model.request.*;
55
import com.pengrad.telegrambot.request.*;
66
import com.pengrad.telegrambot.response.*;
7+
import okhttp3.OkHttpClient;
8+
import okhttp3.Request;
9+
import okhttp3.Response;
710
import org.junit.Test;
811

912
import java.io.File;
@@ -45,6 +48,7 @@ public class TelegramBotTest {
4548
String videoFileId = "BAADAgADZAADuYNZSXhLnzJTZ2yvAg";
4649
String photoFileId = "AgADAgADDKgxG7mDWUlvyFIJ9XfF9yszSw0ABBhVadWwbAK1z-wIAAEC";
4750
String gifFileId = "CgADAgADfQADgNqgSTt9SzatJhc3Ag";
51+
String withSpaceFileId = "BAADAgADZwADkg-4SQI5WM0SPNHrAg";
4852

4953
public TelegramBotTest() throws IOException {
5054
String token, chat, forwardMessage, sticker;
@@ -91,11 +95,14 @@ public void getUpdates() {
9195
}
9296

9397
@Test
94-
public void getFile() {
95-
GetFileResponse response = bot.execute(new GetFile(audioFileId));
98+
public void getFile() throws IOException {
99+
GetFileResponse response = bot.execute(new GetFile(withSpaceFileId));
96100
FileTest.check(response.file());
97101
String path = bot.getFullFilePath(response.file());
98-
assertTrue(path.contains(response.file().filePath()));
102+
103+
Request request = new Request.Builder().head().url(path).build();
104+
Response pathResponse = new OkHttpClient().newCall(request).execute();
105+
assertTrue(pathResponse.isSuccessful());
99106
}
100107

101108
@Test

0 commit comments

Comments
 (0)