🔍️ feat: Optimize undownloaded file acquisition logic.
This commit is contained in:
parent
e17930f6f6
commit
c92a780245
2 changed files with 66 additions and 15 deletions
|
|
@ -223,6 +223,8 @@ public class TdApiHelp {
|
||||||
public T getContent() {
|
public T getContent() {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract TdApi.File getFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class PhotoHandler extends FileHandler<TdApi.MessagePhoto> {
|
public static class PhotoHandler extends FileHandler<TdApi.MessagePhoto> {
|
||||||
|
|
@ -263,17 +265,17 @@ public class TdApiHelp {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRecord convertFileRecord(long telegramId) {
|
public FileRecord convertFileRecord(long telegramId) {
|
||||||
TdApi.PhotoSize photoSize = content.photo.sizes[content.photo.sizes.length - 1];
|
TdApi.File file = getFile();
|
||||||
return new FileRecord(
|
return new FileRecord(
|
||||||
getFileId(),
|
getFileId(),
|
||||||
photoSize.photo.remote.uniqueId,
|
file.remote.uniqueId,
|
||||||
telegramId,
|
telegramId,
|
||||||
message.chatId,
|
message.chatId,
|
||||||
message.id,
|
message.id,
|
||||||
message.date,
|
message.date,
|
||||||
message.hasSensitiveContent,
|
message.hasSensitiveContent,
|
||||||
photoSize.photo.size == 0 ? photoSize.photo.expectedSize : photoSize.photo.size,
|
file.size == 0 ? file.expectedSize : file.size,
|
||||||
photoSize.photo.local == null ? 0 : photoSize.photo.local.downloadedSize,
|
file.local == null ? 0 : file.local.downloadedSize,
|
||||||
"photo",
|
"photo",
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|
@ -285,6 +287,11 @@ public class TdApiHelp {
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TdApi.File getFile() {
|
||||||
|
return content.photo.sizes[content.photo.sizes.length - 1].photo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class VideoHandler extends FileHandler<TdApi.MessageVideo> {
|
public static class VideoHandler extends FileHandler<TdApi.MessageVideo> {
|
||||||
|
|
@ -318,7 +325,7 @@ public class TdApiHelp {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRecord convertFileRecord(long telegramId) {
|
public FileRecord convertFileRecord(long telegramId) {
|
||||||
TdApi.File file = content.video.video;
|
TdApi.File file = getFile();
|
||||||
return new FileRecord(
|
return new FileRecord(
|
||||||
file.id,
|
file.id,
|
||||||
file.remote.uniqueId,
|
file.remote.uniqueId,
|
||||||
|
|
@ -340,6 +347,11 @@ public class TdApiHelp {
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TdApi.File getFile() {
|
||||||
|
return content.video.video;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class AudioHandler extends FileHandler<TdApi.MessageAudio> {
|
public static class AudioHandler extends FileHandler<TdApi.MessageAudio> {
|
||||||
|
|
@ -360,7 +372,7 @@ public class TdApiHelp {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRecord convertFileRecord(long telegramId) {
|
public FileRecord convertFileRecord(long telegramId) {
|
||||||
TdApi.File file = content.audio.audio;
|
TdApi.File file = getFile();
|
||||||
return new FileRecord(
|
return new FileRecord(
|
||||||
file.id,
|
file.id,
|
||||||
file.remote.uniqueId,
|
file.remote.uniqueId,
|
||||||
|
|
@ -382,6 +394,11 @@ public class TdApiHelp {
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TdApi.File getFile() {
|
||||||
|
return content.audio.audio;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DocumentHandler extends FileHandler<TdApi.MessageDocument> {
|
public static class DocumentHandler extends FileHandler<TdApi.MessageDocument> {
|
||||||
|
|
@ -402,7 +419,7 @@ public class TdApiHelp {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileRecord convertFileRecord(long telegramId) {
|
public FileRecord convertFileRecord(long telegramId) {
|
||||||
TdApi.File file = content.document.document;
|
TdApi.File file = getFile();
|
||||||
return new FileRecord(
|
return new FileRecord(
|
||||||
file.id,
|
file.id,
|
||||||
file.remote.uniqueId,
|
file.remote.uniqueId,
|
||||||
|
|
@ -424,6 +441,11 @@ public class TdApiHelp {
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TdApi.File getFile() {
|
||||||
|
return content.document.document;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ComparablePhotoSize implements Comparable<TdApi.PhotoSize> {
|
public static class ComparablePhotoSize implements Comparable<TdApi.PhotoSize> {
|
||||||
|
|
|
||||||
|
|
@ -259,14 +259,41 @@ public class TelegramVerticle extends AbstractVerticle {
|
||||||
searchChatMessages.limit = Convert.toInt(filter.get("limit"), 20);
|
searchChatMessages.limit = Convert.toInt(filter.get("limit"), 20);
|
||||||
searchChatMessages.filter = TdApiHelp.getSearchMessagesFilter(filter.get("type"));
|
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 ->
|
.compose(foundChatMessages ->
|
||||||
DataVerticle.fileRepository.getFilesByUniqueId(TdApiHelp.getFileUniqueIds(Arrays.asList(foundChatMessages.messages)))
|
DataVerticle.fileRepository.getFilesByUniqueId(TdApiHelp.getFileUniqueIds(Arrays.asList(foundChatMessages.messages)))
|
||||||
.map(fileRecords -> Tuple.tuple(foundChatMessages, fileRecords)))
|
.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) {
|
public Future<JsonObject> getChatFilesCount(long chatId) {
|
||||||
return Future.all(
|
return Future.all(
|
||||||
Stream.of(new TdApi.SearchMessagesFilterPhotoAndVideo(),
|
Stream.of(new TdApi.SearchMessagesFilterPhotoAndVideo(),
|
||||||
|
|
@ -363,7 +390,13 @@ public class TelegramVerticle extends AbstractVerticle {
|
||||||
TdApi.Message message = results.resultAt(1);
|
TdApi.Message message = results.resultAt(1);
|
||||||
if (file.local != null) {
|
if (file.local != null) {
|
||||||
if (file.local.isDownloadingCompleted) {
|
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) {
|
if (file.local.isDownloadingActive) {
|
||||||
return Future.failedFuture("File is downloading");
|
return Future.failedFuture("File is downloading");
|
||||||
|
|
@ -862,10 +895,9 @@ public class TelegramVerticle extends AbstractVerticle {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
TdApi.FoundChatMessages foundChatMessages = tuple.v1;
|
||||||
Map<String, FileRecord> fileRecords = tuple.v2;
|
Map<String, FileRecord> fileRecords = tuple.v2;
|
||||||
boolean searchIdle = Objects.equals(filter.get("status"), FileRecord.DownloadStatus.idle.name());
|
|
||||||
|
|
||||||
return DataVerticle.settingRepository.<Boolean>getByKey(SettingKey.uniqueOnly)
|
return DataVerticle.settingRepository.<Boolean>getByKey(SettingKey.uniqueOnly)
|
||||||
.map(uniqueOnly -> {
|
.map(uniqueOnly -> {
|
||||||
|
|
@ -890,9 +922,6 @@ public class TelegramVerticle extends AbstractVerticle {
|
||||||
} else {
|
} else {
|
||||||
fileRecord = fileRecord.withSourceField(source.id(), source.downloadedSize());
|
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
|
//TODO Processing of the same file under different accounts
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue