increase fast download time
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run

This commit is contained in:
otsmr 2026-03-14 22:25:23 +01:00
parent cc0b88718b
commit 9b848138df

View file

@ -19,8 +19,8 @@ import 'package:twonly/src/utils/misc.dart';
Future<void> tryDownloadAllMediaFiles({bool force = false}) async { Future<void> tryDownloadAllMediaFiles({bool force = false}) async {
// This is called when WebSocket is newly connected, so allow all downloads to be restarted. // This is called when WebSocket is newly connected, so allow all downloads to be restarted.
final mediaFiles = final mediaFiles = await twonlyDB.mediaFilesDao
await twonlyDB.mediaFilesDao.getAllMediaFilesPendingDownload(); .getAllMediaFilesPendingDownload();
for (final mediaFile in mediaFiles) { for (final mediaFile in mediaFiles) {
if (await canMediaFileBeDownloaded(mediaFile)) { if (await canMediaFileBeDownloaded(mediaFile)) {
@ -30,8 +30,9 @@ Future<void> tryDownloadAllMediaFiles({bool force = false}) async {
} }
Future<bool> canMediaFileBeDownloaded(MediaFile mediaFile) async { Future<bool> canMediaFileBeDownloaded(MediaFile mediaFile) async {
final messages = final messages = await twonlyDB.messagesDao.getMessagesByMediaId(
await twonlyDB.messagesDao.getMessagesByMediaId(mediaFile.mediaId); mediaFile.mediaId,
);
// Verify that the sender of the original image / message does still exists. // Verify that the sender of the original image / message does still exists.
// If not delete the message as it can not be downloaded from the server anymore. // If not delete the message as it can not be downloaded from the server anymore.
@ -56,8 +57,9 @@ Future<bool> canMediaFileBeDownloaded(MediaFile mediaFile) async {
return false; return false;
} }
final contact = final contact = await twonlyDB.contactsDao.getContactById(
await twonlyDB.contactsDao.getContactById(messages.first.senderId!); messages.first.senderId!,
);
if (contact == null || contact.accountDeleted) { if (contact == null || contact.accountDeleted) {
Log.info( Log.info(
@ -98,23 +100,27 @@ Future<bool> isAllowedToDownload(MediaType type) async {
if (connectivityResult.contains(ConnectivityResult.mobile)) { if (connectivityResult.contains(ConnectivityResult.mobile)) {
if (type == MediaType.video) { if (type == MediaType.video) {
if (options[ConnectivityResult.mobile.name]! if (options[ConnectivityResult.mobile.name]!.contains(
.contains(DownloadMediaTypes.video.name)) { DownloadMediaTypes.video.name,
)) {
return true; return true;
} }
} else if (options[ConnectivityResult.mobile.name]! } else if (options[ConnectivityResult.mobile.name]!.contains(
.contains(DownloadMediaTypes.image.name)) { DownloadMediaTypes.image.name,
)) {
return true; return true;
} }
} }
if (connectivityResult.contains(ConnectivityResult.wifi)) { if (connectivityResult.contains(ConnectivityResult.wifi)) {
if (type == MediaType.video) { if (type == MediaType.video) {
if (options[ConnectivityResult.wifi.name]! if (options[ConnectivityResult.wifi.name]!.contains(
.contains(DownloadMediaTypes.video.name)) { DownloadMediaTypes.video.name,
)) {
return true; return true;
} }
} else if (options[ConnectivityResult.wifi.name]! } else if (options[ConnectivityResult.wifi.name]!.contains(
.contains(DownloadMediaTypes.image.name)) { DownloadMediaTypes.image.name,
)) {
return true; return true;
} }
} }
@ -230,8 +236,9 @@ Future<void> downloadFileFast(
String apiUrl, String apiUrl,
File filePath, File filePath,
) async { ) async {
final response = final response = await http
await http.get(Uri.parse(apiUrl)).timeout(const Duration(seconds: 10)); .get(Uri.parse(apiUrl))
.timeout(const Duration(seconds: 30));
if (response.statusCode == 200) { if (response.statusCode == 200) {
await filePath.writeAsBytes(response.bodyBytes); await filePath.writeAsBytes(response.bodyBytes);
@ -308,8 +315,10 @@ Future<void> handleEncryptedFile(String mediaId) async {
mac: Mac(mediaService.mediaFile.encryptionMac!), mac: Mac(mediaService.mediaFile.encryptionMac!),
); );
final plaintextBytes = final plaintextBytes = await chacha20.decrypt(
await chacha20.decrypt(secretBox, secretKey: secretKeyData); secretBox,
secretKey: secretKeyData,
);
final rawMediaBytes = Uint8List.fromList(plaintextBytes); final rawMediaBytes = Uint8List.fromList(plaintextBytes);
@ -337,8 +346,8 @@ Future<void> handleEncryptedFile(String mediaId) async {
} }
Future<void> makeMigrationToVersion91() async { Future<void> makeMigrationToVersion91() async {
final messages = final messages = await twonlyDB.mediaFilesDao
await twonlyDB.mediaFilesDao.getAllMediaFilesReuploadRequested(); .getAllMediaFilesReuploadRequested();
for (final message in messages) { for (final message in messages) {
await requestMediaReupload(message.mediaId); await requestMediaReupload(message.mediaId);
} }