Skip to content

Commit d9655df

Browse files
committed
Change TelegramFile to work with nullable size to comply with recent Telegram objects
1 parent f858345 commit d9655df

2 files changed

Lines changed: 11 additions & 30 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 <= 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: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public record TelegramRequest(Message message) {
3535
public @Nullable TelegramFile getFile() {
3636
return getMessageMedia()
3737
.map(media -> switch (media) {
38-
case LivePhoto livePhoto -> getLivePhoto(livePhoto);
38+
case LivePhoto livePhoto -> new TelegramFile(livePhoto.fileId(), livePhoto.fileSize());
3939
case PhotoSize[] photos when photos.length > 0 -> getBestPhoto(photos);
4040
case Document document -> new TelegramFile(document.fileId(), document.fileSize());
4141
case Sticker sticker -> new TelegramFile(sticker.fileId(), sticker.fileSize());
42-
case Video video -> getVideo(video);
42+
case Video video -> new TelegramFile(video.fileId(), video.fileSize());
4343
case VideoNote videoNote -> new TelegramFile(videoNote.fileId(), videoNote.fileSize());
4444
default -> TelegramFile.NOT_SUPPORTED;
4545
})
@@ -54,34 +54,14 @@ private Optional<?> getMessageMedia() {
5454
.findFirst();
5555
}
5656

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-
7157
private TelegramFile getBestPhoto(PhotoSize[] photos) {
7258
return Arrays.stream(photos)
7359
.map(photo -> new TelegramFile(photo.fileId(), photo.fileSize()))
7460
.filter(TelegramFile::canBeDownloaded)
75-
.max(comparing(TelegramFile::size))
61+
.max(comparing(TelegramFile::sizeValue))
7662
.orElse(TelegramFile.TOO_LARGE);
7763
}
7864

79-
private TelegramFile getVideo(Video video) {
80-
var id = video.fileId();
81-
var fileSize = video.fileSize();
82-
return getTelegramFile(id, fileSize);
83-
}
84-
8565
public long getChatId() {
8666
return message.chat().id();
8767
}

0 commit comments

Comments
 (0)