Skip to content

Commit f858345

Browse files
committed
Add live photo conversion support
1 parent c9ed3d7 commit f858345

5 files changed

Lines changed: 89 additions & 7 deletions

File tree

src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonProperty;
99
import com.github.stickerifier.stickerify.telegram.Answer;
1010
import com.pengrad.telegrambot.model.Document;
11+
import com.pengrad.telegrambot.model.LivePhoto;
1112
import com.pengrad.telegrambot.model.Message;
1213
import com.pengrad.telegrambot.model.PhotoSize;
1314
import com.pengrad.telegrambot.model.Sticker;
@@ -34,6 +35,7 @@ public record TelegramRequest(Message message) {
3435
public @Nullable TelegramFile getFile() {
3536
return getMessageMedia()
3637
.map(media -> switch (media) {
38+
case LivePhoto livePhoto -> getLivePhoto(livePhoto);
3739
case PhotoSize[] photos when photos.length > 0 -> getBestPhoto(photos);
3840
case Document document -> new TelegramFile(document.fileId(), document.fileSize());
3941
case Sticker sticker -> new TelegramFile(sticker.fileId(), sticker.fileSize());
@@ -45,13 +47,27 @@ public record TelegramRequest(Message message) {
4547
}
4648

4749
private Optional<?> getMessageMedia() {
48-
return Stream.of(message.photo(), message.document(), message.sticker(),
50+
return Stream.of(message.livePhoto(), message.photo(), message.document(), message.sticker(),
4951
message.video(), message.videoNote(),
5052
message.audio(), message.voice())
5153
.filter(Objects::nonNull)
5254
.findFirst();
5355
}
5456

57+
private TelegramFile getLivePhoto(LivePhoto livePhoto) {
58+
var id = livePhoto.fileId();
59+
var fileSize = livePhoto.fileSize();
60+
return getTelegramFile(id, fileSize);
61+
}
62+
63+
private TelegramFile getTelegramFile(String fileId, @Nullable Long size) {
64+
if (size == null) {
65+
return new TelegramFile(fileId);
66+
} else {
67+
return new TelegramFile(fileId, size);
68+
}
69+
}
70+
5571
private TelegramFile getBestPhoto(PhotoSize[] photos) {
5672
return Arrays.stream(photos)
5773
.map(photo -> new TelegramFile(photo.fileId(), photo.fileSize()))
@@ -63,11 +79,7 @@ private TelegramFile getBestPhoto(PhotoSize[] photos) {
6379
private TelegramFile getVideo(Video video) {
6480
var id = video.fileId();
6581
var fileSize = video.fileSize();
66-
if (fileSize == null) {
67-
return new TelegramFile(id);
68-
} else {
69-
return new TelegramFile(id, fileSize);
70-
}
82+
return getTelegramFile(id, fileSize);
7183
}
7284

7385
public long getChatId() {

src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public final class MockResponses {
261261
chat: {
262262
id: 1
263263
},
264-
sticker: {
264+
document: {
265265
file_id: "valid.gif",
266266
file_size: 200000
267267
}
@@ -271,6 +271,42 @@ public final class MockResponses {
271271
}
272272
""").build();
273273

274+
static final MockResponse LIVE_PHOTO_FILE = new MockResponse.Builder().body("""
275+
{
276+
ok: true,
277+
result: [
278+
{
279+
update_id: 1,
280+
message: {
281+
message_id: 1,
282+
from: {
283+
id: 123456
284+
},
285+
chat: {
286+
id: 1
287+
},
288+
photo: [
289+
{
290+
file_id: "first_frame",
291+
file_size: 200000
292+
}
293+
],
294+
live_photo: {
295+
file_id: "valid_live_photo",
296+
file_size: 200000,
297+
photo: [
298+
{
299+
file_id: "first_frame",
300+
file_size: 200000
301+
}
302+
]
303+
}
304+
}
305+
}
306+
]
307+
}
308+
""").build();
309+
274310
static final MockResponse DOCUMENT = new MockResponse.Builder().body("""
275311
{
276312
ok: true,

src/test/java/com/github/stickerifier/stickerify/bot/StickerifyTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ void convertedGif() throws Exception {
267267
}
268268
}
269269

270+
@Test
271+
void convertedLivePhoto() throws Exception {
272+
server.enqueue(MockResponses.LIVE_PHOTO_FILE);
273+
server.enqueue(MockResponses.fileInfo("valid_live_photo"));
274+
server.enqueue(MockResponses.fileDownload("valid_live_photo"));
275+
276+
try (var _ = runBot()) {
277+
var getUpdates = server.takeRequest();
278+
assertEquals("/api/token/getUpdates", getUpdates.getTarget());
279+
280+
var getFile = server.takeRequest();
281+
assertEquals("/api/token/getFile", getFile.getTarget());
282+
assertNotNull(getFile.getBody());
283+
assertEquals("file_id=valid_live_photo", getFile.getBody().utf8());
284+
285+
var download = server.takeRequest();
286+
assertEquals("/files/token/valid_live_photo", download.getTarget());
287+
288+
var sendDocument = server.takeRequest();
289+
assertEquals("/api/token/sendDocument", sendDocument.getTarget());
290+
assertNotNull(sendDocument.getBody());
291+
assertThat(sendDocument.getBody().utf8(), containsString(Answer.FILE_READY.getText()));
292+
}
293+
}
294+
270295
@Test
271296
void documentNotSupported() throws Exception {
272297
server.enqueue(MockResponses.DOCUMENT);

src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ void convertAviVideo() throws Exception {
245245
assertVideoConsistency(result, 512, 512, 30F, 2.966F);
246246
}
247247

248+
@Test
249+
@Tag(Tags.VIDEO)
250+
void convertLivePhoto() throws Exception {
251+
var livePhoto = loadResource("valid_live_photo");
252+
var result = MediaHelper.convert(livePhoto);
253+
254+
assertVideoConsistency(result, 384, 512, 30F, 2.966F);
255+
}
256+
248257
@Test
249258
@Tag(Tags.VIDEO)
250259
void noVideoConversionNeeded() throws Exception {
983 KB
Binary file not shown.

0 commit comments

Comments
 (0)