Skip to content

Commit b5616fe

Browse files
committed
AbstractMultipartRequest and InputMedia can take fileName and contentType, #302
1 parent 9e062c3 commit b5616fe

21 files changed

+155
-27
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import com.pengrad.telegrambot.Callback;
5+
import com.pengrad.telegrambot.model.request.InputFile;
56
import com.pengrad.telegrambot.request.BaseRequest;
67
import com.pengrad.telegrambot.response.BaseResponse;
78
import okhttp3.Call;
@@ -106,9 +107,19 @@ private RequestBody createRequestBody(BaseRequest<?, ?> request) {
106107
String name = parameter.getKey();
107108
Object value = parameter.getValue();
108109
if (value instanceof byte[]) {
109-
builder.addFormDataPart(name, request.getFileName(), RequestBody.create(contentType, (byte[]) value));
110+
builder.addFormDataPart(name, request.getFileName(), RequestBody.create((byte[]) value, contentType));
110111
} else if (value instanceof File) {
111-
builder.addFormDataPart(name, request.getFileName(), RequestBody.create(contentType, (File) value));
112+
builder.addFormDataPart(name, request.getFileName(), RequestBody.create((File) value, contentType));
113+
} else if (value instanceof InputFile) {
114+
InputFile inputFile = (InputFile) value;
115+
contentType = MediaType.parse(inputFile.getContentType());
116+
RequestBody body;
117+
if (inputFile.getFile() != null) {
118+
body = RequestBody.create(inputFile.getFile(), contentType);
119+
} else {
120+
body = RequestBody.create(inputFile.getBytes(), contentType);
121+
}
122+
builder.addFormDataPart(name, ((InputFile) value).getFileName(), body);
112123
} else {
113124
builder.addFormDataPart(name, toParamValue(value));
114125
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import java.io.File;
4+
5+
public class InputFile {
6+
7+
private File file;
8+
private byte[] bytes;
9+
private String fileName;
10+
private String contentType;
11+
12+
public InputFile(File file, String fileName, String contentType) {
13+
this.file = file;
14+
this.fileName = fileName;
15+
this.contentType = contentType;
16+
}
17+
18+
public InputFile(byte[] bytes, String fileName, String contentType) {
19+
this.bytes = bytes;
20+
this.fileName = fileName;
21+
this.contentType = contentType;
22+
}
23+
24+
public File getFile() {
25+
return file;
26+
}
27+
28+
public byte[] getBytes() {
29+
return bytes;
30+
}
31+
32+
public String getFileName() {
33+
return fileName;
34+
}
35+
36+
public String getContentType() {
37+
return contentType;
38+
}
39+
40+
void setFileName(String fileName) {
41+
this.fileName = fileName;
42+
}
43+
44+
void setContentType(String contentType) {
45+
this.contentType = contentType;
46+
}
47+
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMedia.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.pengrad.telegrambot.AttachName;
44
import com.pengrad.telegrambot.model.MessageEntity;
5-
import com.pengrad.telegrambot.request.CopyMessage;
65

76
import java.io.File;
87
import java.io.Serializable;
@@ -27,7 +26,10 @@ abstract public class InputMedia<T extends InputMedia<T>> implements Serializabl
2726
private MessageEntity[] caption_entities;
2827

2928
transient private Map<String, Object> attachments = new HashMap<>();
30-
transient private String filename;
29+
transient private InputFile inputFile;
30+
transient private String inputFileAttachId;
31+
transient private String fileName;
32+
transient private String contentType;
3133

3234
InputMedia(String type, Object media) {
3335
this.type = type;
@@ -36,9 +38,14 @@ abstract public class InputMedia<T extends InputMedia<T>> implements Serializabl
3638
} else {
3739
String attachName = AttachName.next();
3840
this.media = "attach://" + attachName;
39-
attachments.put(attachName, media);
41+
inputFileAttachId = attachName;
4042
if (media instanceof File) {
41-
filename = ((File) media).getName();
43+
fileName = ((File) media).getName();
44+
inputFile = new InputFile((File) media, getFileName(), getContentType());
45+
} else if (media instanceof byte[]) {
46+
inputFile = new InputFile((byte[]) media, getFileName(), getContentType());
47+
} else {
48+
attachments.put(attachName, media);
4249
}
4350
}
4451
}
@@ -47,6 +54,14 @@ public Map<String, Object> getAttachments() {
4754
return attachments;
4855
}
4956

57+
public InputFile inputFile() {
58+
return inputFile;
59+
}
60+
61+
public String getInputFileId() {
62+
return inputFileAttachId;
63+
}
64+
5065
public T thumb(File thumb) {
5166
String attachName = AttachName.next();
5267
attachments.put(attachName, thumb);
@@ -76,11 +91,31 @@ public T captionEntities(MessageEntity... entities) {
7691
return thisAsT;
7792
}
7893

94+
public T fileName(String fileName) {
95+
if (inputFile != null) {
96+
inputFile.setFileName(fileName);
97+
}
98+
this.fileName = fileName;
99+
return thisAsT;
100+
}
101+
102+
public T contentType(String contentType) {
103+
if (inputFile != null) {
104+
inputFile.setContentType(contentType);
105+
}
106+
this.contentType = contentType;
107+
return thisAsT;
108+
}
109+
79110
public String getFileName() {
80-
return filename != null ? filename : getDefaultFileName();
111+
return (fileName != null && !fileName.isEmpty()) ? fileName : getDefaultFileName();
112+
}
113+
114+
public String getContentType() {
115+
return (contentType != null && !contentType.isEmpty()) ? contentType : getDefaultContentType();
81116
}
82117

83118
abstract protected String getDefaultFileName();
84119

85-
abstract public String getContentType();
120+
abstract protected String getDefaultContentType();
86121
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMediaAnimation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String getDefaultFileName() {
4747
}
4848

4949
@Override
50-
public String getContentType() {
50+
public String getDefaultContentType() {
5151
return ContentTypes.GIF_MIME_TYPE;
5252
}
5353
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMediaAudio.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String getDefaultFileName() {
4848
}
4949

5050
@Override
51-
public String getContentType() {
51+
public String getDefaultContentType() {
5252
return ContentTypes.AUDIO_MIME_TYPE;
5353
}
5454
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMediaDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public String getDefaultFileName() {
3737
}
3838

3939
@Override
40-
public String getContentType() {
40+
public String getDefaultContentType() {
4141
return ContentTypes.DOC_MIME_TYPE;
4242
}
4343
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMediaPhoto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public String getDefaultFileName() {
3030
}
3131

3232
@Override
33-
public String getContentType() {
33+
public String getDefaultContentType() {
3434
return ContentTypes.PHOTO_MIME_TYPE;
3535
}
3636
}

library/src/main/java/com/pengrad/telegrambot/model/request/InputMediaVideo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public String getDefaultFileName() {
5353
}
5454

5555
@Override
56-
public String getContentType() {
56+
public String getDefaultContentType() {
5757
return ContentTypes.VIDEO_MIME_TYPE;
5858
}
5959
}

library/src/main/java/com/pengrad/telegrambot/request/AbstractMultipartRequest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract public class AbstractMultipartRequest<T extends AbstractMultipartReques
1010

1111
private boolean isMultipart;
1212
private String fileName;
13+
private String contentType;
1314

1415
public AbstractMultipartRequest(Object chatId, Object file) {
1516
super(chatId);
@@ -31,6 +32,11 @@ public T fileName(String fileName) {
3132
return thisAsT;
3233
}
3334

35+
public T contentType(String contentType) {
36+
this.contentType = contentType;
37+
return thisAsT;
38+
}
39+
3440
protected T thumb(Object thumb) {
3541
isMultipart = true;
3642
return add("thumb", thumb);
@@ -51,9 +57,13 @@ public String getFileName() {
5157
}
5258

5359
@Override
54-
abstract public String getContentType();
60+
public String getContentType() {
61+
return (contentType != null && !contentType.isEmpty()) ? contentType : getDefaultContentType();
62+
}
5563

5664
abstract protected String getDefaultFileName();
5765

66+
abstract protected String getDefaultContentType();
67+
5868
abstract protected String getFileParamName();
5969
}

library/src/main/java/com/pengrad/telegrambot/request/EditMessageMedia.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pengrad.telegrambot.request;
22

33
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
4+
import com.pengrad.telegrambot.model.request.InputFile;
45
import com.pengrad.telegrambot.model.request.InputMedia;
56
import com.pengrad.telegrambot.response.BaseResponse;
67
import com.pengrad.telegrambot.response.SendResponse;
@@ -36,6 +37,10 @@ private void addMedia(InputMedia<?> media) {
3637
addAll(attachments);
3738
isMultipart = true;
3839
}
40+
if (media.inputFile() != null) {
41+
add(media.getInputFileId(), media.inputFile());
42+
isMultipart = true;
43+
}
3944
}
4045

4146
public EditMessageMedia replyMarkup(InlineKeyboardMarkup replyMarkup) {

0 commit comments

Comments
 (0)