Skip to content

Commit

Permalink
🔍️ feat: Optimize undownloaded file acquisition logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvis2f committed Jan 14, 2025
1 parent e17930f commit c92a780
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
36 changes: 29 additions & 7 deletions api/src/main/java/telegram/files/TdApiHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ public TdApi.File getPreviewFileId(Tuple tuple) {
public T getContent() {
return content;
}

public abstract TdApi.File getFile();
}

public static class PhotoHandler extends FileHandler<TdApi.MessagePhoto> {
Expand Down Expand Up @@ -263,17 +265,17 @@ public TdApi.File getPreviewFileId(Tuple tuple) {

@Override
public FileRecord convertFileRecord(long telegramId) {
TdApi.PhotoSize photoSize = content.photo.sizes[content.photo.sizes.length - 1];
TdApi.File file = getFile();
return new FileRecord(
getFileId(),
photoSize.photo.remote.uniqueId,
file.remote.uniqueId,
telegramId,
message.chatId,
message.id,
message.date,
message.hasSensitiveContent,
photoSize.photo.size == 0 ? photoSize.photo.expectedSize : photoSize.photo.size,
photoSize.photo.local == null ? 0 : photoSize.photo.local.downloadedSize,
file.size == 0 ? file.expectedSize : file.size,
file.local == null ? 0 : file.local.downloadedSize,
"photo",
null,
null,
Expand All @@ -285,6 +287,11 @@ public FileRecord convertFileRecord(long telegramId) {
null
);
}

@Override
public TdApi.File getFile() {
return content.photo.sizes[content.photo.sizes.length - 1].photo;
}
}

public static class VideoHandler extends FileHandler<TdApi.MessageVideo> {
Expand Down Expand Up @@ -318,7 +325,7 @@ public TdApi.File getPreviewFileId(Tuple tuple) {

@Override
public FileRecord convertFileRecord(long telegramId) {
TdApi.File file = content.video.video;
TdApi.File file = getFile();
return new FileRecord(
file.id,
file.remote.uniqueId,
Expand All @@ -340,6 +347,11 @@ public FileRecord convertFileRecord(long telegramId) {
null
);
}

@Override
public TdApi.File getFile() {
return content.video.video;
}
}

public static class AudioHandler extends FileHandler<TdApi.MessageAudio> {
Expand All @@ -360,7 +372,7 @@ public String getFileUniqueId() {

@Override
public FileRecord convertFileRecord(long telegramId) {
TdApi.File file = content.audio.audio;
TdApi.File file = getFile();
return new FileRecord(
file.id,
file.remote.uniqueId,
Expand All @@ -382,6 +394,11 @@ public FileRecord convertFileRecord(long telegramId) {
null
);
}

@Override
public TdApi.File getFile() {
return content.audio.audio;
}
}

public static class DocumentHandler extends FileHandler<TdApi.MessageDocument> {
Expand All @@ -402,7 +419,7 @@ public String getFileUniqueId() {

@Override
public FileRecord convertFileRecord(long telegramId) {
TdApi.File file = content.document.document;
TdApi.File file = getFile();
return new FileRecord(
file.id,
file.remote.uniqueId,
Expand All @@ -424,6 +441,11 @@ public FileRecord convertFileRecord(long telegramId) {
null
);
}

@Override
public TdApi.File getFile() {
return content.document.document;
}
}

public static class ComparablePhotoSize implements Comparable<TdApi.PhotoSize> {
Expand Down
45 changes: 37 additions & 8 deletions api/src/main/java/telegram/files/TelegramVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,41 @@ public Future<JsonObject> getChatFiles(long chatId, MultiMap filter) {
searchChatMessages.limit = Convert.toInt(filter.get("limit"), 20);
searchChatMessages.filter = TdApiHelp.getSearchMessagesFilter(filter.get("type"));

return this.execute(searchChatMessages)
return (Objects.equals(filter.get("status"), FileRecord.DownloadStatus.idle.name()) ?
this.getIdleChatFiles(searchChatMessages) :
this.execute(searchChatMessages))
.compose(foundChatMessages ->
DataVerticle.fileRepository.getFilesByUniqueId(TdApiHelp.getFileUniqueIds(Arrays.asList(foundChatMessages.messages)))
.map(fileRecords -> Tuple.tuple(foundChatMessages, fileRecords)))
.compose(r -> this.convertFiles(r, filter));
.compose(this::convertFiles);
}
}

private Future<TdApi.FoundChatMessages> getIdleChatFiles(TdApi.SearchChatMessages searchChatMessages) {
return this.execute(searchChatMessages)
.compose(foundChatMessages -> {
TdApi.Message[] messages = Stream.of(foundChatMessages.messages)
.filter(message ->
TdApiHelp.getFileHandler(message)
.map(TdApiHelp.FileHandler::getFile)
.map(file -> file.local == null || (
!file.local.isDownloadingActive
&& !file.local.isDownloadingCompleted
&& file.local.downloadedSize == 0
))
.orElse(false)
)
.toArray(TdApi.Message[]::new);
if (ArrayUtil.isEmpty(messages)) {
searchChatMessages.fromMessageId = foundChatMessages.nextFromMessageId;
return getIdleChatFiles(searchChatMessages);
} else {
foundChatMessages.messages = messages;
return Future.succeededFuture(foundChatMessages);
}
});
}

public Future<JsonObject> getChatFilesCount(long chatId) {
return Future.all(
Stream.of(new TdApi.SearchMessagesFilterPhotoAndVideo(),
Expand Down Expand Up @@ -363,7 +390,13 @@ public Future<TdApi.File> startDownload(Long chatId, Long messageId, Integer fil
TdApi.Message message = results.resultAt(1);
if (file.local != null) {
if (file.local.isDownloadingCompleted) {
return Future.failedFuture("File already downloaded");
return DataVerticle.fileRepository.updateStatus(
file.id,
file.remote.uniqueId,
file.local.path,
FileRecord.DownloadStatus.completed,
System.currentTimeMillis()
).compose(r -> Future.failedFuture("File is already downloaded successfully"));
}
if (file.local.isDownloadingActive) {
return Future.failedFuture("File is downloading");
Expand Down Expand Up @@ -862,10 +895,9 @@ private Future<JsonArray> convertChat(List<TdApi.Chat> chats) {
));
}

private Future<JsonObject> convertFiles(Tuple2<TdApi.FoundChatMessages, Map<String, FileRecord>> tuple, MultiMap filter) {
private Future<JsonObject> convertFiles(Tuple2<TdApi.FoundChatMessages, Map<String, FileRecord>> tuple) {
TdApi.FoundChatMessages foundChatMessages = tuple.v1;
Map<String, FileRecord> fileRecords = tuple.v2;
boolean searchIdle = Objects.equals(filter.get("status"), FileRecord.DownloadStatus.idle.name());

return DataVerticle.settingRepository.<Boolean>getByKey(SettingKey.uniqueOnly)
.map(uniqueOnly -> {
Expand All @@ -890,9 +922,6 @@ private Future<JsonObject> convertFiles(Tuple2<TdApi.FoundChatMessages, Map<Stri
} else {
fileRecord = fileRecord.withSourceField(source.id(), source.downloadedSize());
}
if (searchIdle && !Objects.equals(fileRecord.downloadStatus(), FileRecord.DownloadStatus.idle.name())) {
return null;
}

//TODO Processing of the same file under different accounts

Expand Down

0 comments on commit c92a780

Please sign in to comment.