Skip to content

Commit

Permalink
Encode file path url #90
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Jun 6, 2017
1 parent 4e2e360 commit 941e2ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 11 additions & 1 deletion library/src/main/java/com/pengrad/telegrambot/impl/FileApi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.pengrad.telegrambot.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
* Stas Parshin
* 16 October 2015
Expand All @@ -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;
}
}
}
13 changes: 10 additions & 3 deletions library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 941e2ba

Please sign in to comment.