🐛 fix: Fixed an issue where file downloads could not be canceled

This commit is contained in:
jarvis2f 2024-12-27 16:06:04 +08:00
parent f9818cbbbd
commit f8679b5bb5
4 changed files with 22 additions and 5 deletions

View file

@ -353,12 +353,15 @@ public class TelegramVerticle extends AbstractVerticle {
public Future<Void> cancelDownload(Integer fileId) {
return this.execute(new TdApi.GetFile(fileId))
.compose(file -> {
if (file.local == null || !file.local.isDownloadingActive) {
if (file.local == null) {
return Future.failedFuture("File not started downloading");
}
return this.execute(new TdApi.CancelDownloadFile(fileId, false));
return this.execute(new TdApi.CancelDownloadFile(fileId, false))
.map(file);
})
.compose(file -> this.execute(new TdApi.DeleteFile(fileId)).map(file))
.compose(file -> DataVerticle.fileRepository.deleteByUniqueId(file.remote.uniqueId))
.onSuccess(r ->
sendHttpEvent(EventPayload.build(EventPayload.TYPE_FILE_STATUS, new JsonObject()
.put("fileId", fileId)

View file

@ -90,9 +90,6 @@ public record FileRecord(int id, //file id will change
));
public FileRecord withSourceField(int id, long downloadedSize) {
if (this.downloadedSize == downloadedSize) {
return this;
}
return new FileRecord(id, uniqueId, telegramId, chatId, messageId, date, hasSensitiveContent, size, downloadedSize, type, mimeType, fileName, thumbnail, caption, localPath, downloadStatus);
}
}

View file

@ -34,4 +34,6 @@ public interface FileRepository {
Future<Void> updateFileId(int fileId, String uniqueId);
Future<Void> deleteByUniqueId(String uniqueId);
}

View file

@ -311,4 +311,19 @@ public class FileRepositoryImpl implements FileRepository {
.mapEmpty();
});
}
@Override
public Future<Void> deleteByUniqueId(String uniqueId) {
if (StrUtil.isBlank(uniqueId)) {
return Future.succeededFuture();
}
return SqlTemplate
.forUpdate(pool, """
DELETE FROM file_record WHERE unique_id = #{uniqueId}
""")
.execute(Map.of("uniqueId", uniqueId))
.onFailure(err -> log.error("Failed to delete file record: %s".formatted(err.getMessage()))
)
.mapEmpty();
}
}