Skip to content

Commit 8e1945e

Browse files
authored
Support live photos conversion (#507)
1 parent c9ed3d7 commit 8e1945e

6 files changed

Lines changed: 84 additions & 21 deletions

File tree

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.github.stickerifier.stickerify.telegram.model;
22

3-
public record TelegramFile(String id, long size) {
3+
import org.jspecify.annotations.Nullable;
4+
5+
public record TelegramFile(String id, @Nullable Long size) {
46
private static final String INVALID_ID = "";
5-
private static final long INVALID_SIZE = -1L;
67
private static final long MAX_DOWNLOADABLE_FILE_SIZE_IN_BYTES = 20_000_000L;
78

8-
public static final TelegramFile NOT_SUPPORTED = new TelegramFile(INVALID_ID);
9+
public static final TelegramFile NOT_SUPPORTED = new TelegramFile(INVALID_ID, null);
910
public static final TelegramFile TOO_LARGE = new TelegramFile(INVALID_ID, MAX_DOWNLOADABLE_FILE_SIZE_IN_BYTES + 1);
1011

11-
public TelegramFile(String id) {
12-
this(id, INVALID_SIZE);
12+
public boolean canBeDownloaded() {
13+
return size != null && size > 0 && size <= MAX_DOWNLOADABLE_FILE_SIZE_IN_BYTES;
1314
}
1415

15-
public boolean canBeDownloaded() {
16-
return size <= MAX_DOWNLOADABLE_FILE_SIZE_IN_BYTES;
16+
public long sizeValue() {
17+
return size == null ? -1L : size;
1718
}
1819
}

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

Lines changed: 5 additions & 13 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,18 +35,19 @@ public record TelegramRequest(Message message) {
3435
public @Nullable TelegramFile getFile() {
3536
return getMessageMedia()
3637
.map(media -> switch (media) {
38+
case LivePhoto livePhoto -> new TelegramFile(livePhoto.fileId(), livePhoto.fileSize());
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());
40-
case Video video -> getVideo(video);
42+
case Video video -> new TelegramFile(video.fileId(), video.fileSize());
4143
case VideoNote videoNote -> new TelegramFile(videoNote.fileId(), videoNote.fileSize());
4244
default -> TelegramFile.NOT_SUPPORTED;
4345
})
4446
.orElse(null);
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)
@@ -56,20 +58,10 @@ private TelegramFile getBestPhoto(PhotoSize[] photos) {
5658
return Arrays.stream(photos)
5759
.map(photo -> new TelegramFile(photo.fileId(), photo.fileSize()))
5860
.filter(TelegramFile::canBeDownloaded)
59-
.max(comparing(TelegramFile::size))
61+
.max(comparing(TelegramFile::sizeValue))
6062
.orElse(TelegramFile.TOO_LARGE);
6163
}
6264

63-
private TelegramFile getVideo(Video video) {
64-
var id = video.fileId();
65-
var fileSize = video.fileSize();
66-
if (fileSize == null) {
67-
return new TelegramFile(id);
68-
} else {
69-
return new TelegramFile(id, fileSize);
70-
}
71-
}
72-
7365
public long getChatId() {
7466
return message.chat().id();
7567
}

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)