mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-01-15 09:08:40 +00:00
add error handling and media purge
This commit is contained in:
parent
6677f89a18
commit
b979f3c8d2
4 changed files with 74 additions and 41 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import 'dart:isolate';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
|
@ -46,10 +45,8 @@ void main() async {
|
|||
await twonlyDB.messagesDao.handleMediaFilesOlderThan7Days();
|
||||
|
||||
// purge media files in the background
|
||||
Isolate.run(() {
|
||||
purgeReceivedMediaFiles();
|
||||
purgeSendMediaFiles();
|
||||
});
|
||||
purgeReceivedMediaFiles();
|
||||
purgeSendMediaFiles();
|
||||
|
||||
await initMediaUploader();
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import 'package:cryptography_plus/cryptography_plus.dart';
|
|||
import 'package:twonly/src/services/api/utils.dart';
|
||||
import 'package:twonly/src/utils/log.dart';
|
||||
import 'package:twonly/src/utils/storage.dart';
|
||||
import 'package:twonly/src/views/camera/share_image_editor_view.dart';
|
||||
|
||||
Map<int, DateTime> downloadStartedForMediaReceived = {};
|
||||
|
||||
|
|
@ -311,14 +312,35 @@ Future<void> purgeMediaFiles(Directory directory) async {
|
|||
bool canBeDeleted = true;
|
||||
|
||||
for (final message in messages) {
|
||||
if ((message.openedAt == null && !message.errorWhileSending)) {
|
||||
canBeDeleted = false;
|
||||
} else if (message.mediaStored) {
|
||||
if (!file.path.contains(".original.") &&
|
||||
!file.path.contains(".encrypted")) {
|
||||
try {
|
||||
MediaMessageContent content = MediaMessageContent.fromJson(
|
||||
jsonDecode(message.contentJson!),
|
||||
);
|
||||
|
||||
DateTime oneDayAgo = DateTime.now().subtract(Duration(days: 1));
|
||||
|
||||
if (((message.openedAt == null ||
|
||||
oneDayAgo.isBefore(message.openedAt!)) &&
|
||||
!message.errorWhileSending)) {
|
||||
canBeDeleted = false;
|
||||
} else if (message.mediaStored) {
|
||||
if (!file.path.contains(".original.") &&
|
||||
!file.path.contains(".encrypted")) {
|
||||
canBeDeleted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (message.acknowledgeByServer) {
|
||||
// preserve images which can be stored by the other person...
|
||||
if (content.maxShowTime != gMediaShowInfinite) {
|
||||
canBeDeleted = true;
|
||||
}
|
||||
// encrypted or upload data can be removed when acknowledgedByServer
|
||||
if (file.path.contains(".upload") ||
|
||||
file.path.contains(".encrypted")) {
|
||||
canBeDeleted = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
if (canBeDeleted) {
|
||||
Log.info("purged media file ${file.path} ");
|
||||
|
|
|
|||
|
|
@ -56,18 +56,25 @@ Future initMediaUploader() async {
|
|||
FileDownloader().updates.listen((update) async {
|
||||
switch (update) {
|
||||
case TaskStatusUpdate():
|
||||
if (update.status == TaskStatus.complete) {
|
||||
int mediaUploadId = int.parse(update.task.taskId);
|
||||
MediaUpload? media = await twonlyDB.mediaUploadsDao
|
||||
.getMediaUploadById(mediaUploadId)
|
||||
.getSingleOrNull();
|
||||
if (media == null) {
|
||||
Log.error(
|
||||
"Got an upload task but no upload media in the mediaupload atabase");
|
||||
return;
|
||||
}
|
||||
bool failed = false;
|
||||
int mediaUploadId = int.parse(update.task.taskId);
|
||||
MediaUpload? media = await twonlyDB.mediaUploadsDao
|
||||
.getMediaUploadById(mediaUploadId)
|
||||
.getSingleOrNull();
|
||||
if (media == null) {
|
||||
Log.error(
|
||||
"Got an upload task but no upload media in the media upload database",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (update.status == TaskStatus.failed ||
|
||||
update.status == TaskStatus.canceled) {
|
||||
Log.error("Upload failed: ${update.status}");
|
||||
failed = true;
|
||||
} else if (update.status == TaskStatus.complete) {
|
||||
if (update.responseStatusCode == 200) {
|
||||
Log.info("Upload was success!");
|
||||
Log.info("Upload of $mediaUploadId success!");
|
||||
|
||||
await twonlyDB.mediaUploadsDao.updateMediaUpload(
|
||||
mediaUploadId,
|
||||
|
|
@ -89,18 +96,23 @@ Future initMediaUploader() async {
|
|||
} else if (update.responseStatusCode != null) {
|
||||
if (update.responseStatusCode! >= 400 &&
|
||||
update.responseStatusCode! < 500) {
|
||||
for (final messageId in media.messageIds!) {
|
||||
await twonlyDB.messagesDao.updateMessageByMessageId(
|
||||
messageId,
|
||||
MessagesCompanion(
|
||||
acknowledgeByServer: Value(true),
|
||||
errorWhileSending: Value(true),
|
||||
),
|
||||
);
|
||||
}
|
||||
failed = true;
|
||||
}
|
||||
Log.error(
|
||||
"Got error while uploading: ${update.responseStatusCode}");
|
||||
"Got error while uploading: ${update.responseStatusCode}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
for (final messageId in media.messageIds!) {
|
||||
await twonlyDB.messagesDao.updateMessageByMessageId(
|
||||
messageId,
|
||||
MessagesCompanion(
|
||||
acknowledgeByServer: Value(true),
|
||||
errorWhileSending: Value(true),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -117,14 +129,16 @@ Future initMediaUploader() async {
|
|||
(Config.bypassTLSCertificateValidation, kDebugMode),
|
||||
]);
|
||||
|
||||
FileDownloader().configureNotification(
|
||||
running: TaskNotification(
|
||||
'Uploading',
|
||||
'Uploading your {filename} ({progress}).',
|
||||
),
|
||||
complete: null,
|
||||
progressBar: true,
|
||||
);
|
||||
if (kDebugMode) {
|
||||
FileDownloader().configureNotification(
|
||||
running: TaskNotification(
|
||||
'Uploading',
|
||||
'Uploading your {filename} ({progress}).',
|
||||
),
|
||||
complete: null,
|
||||
progressBar: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// States:
|
||||
|
|
|
|||
|
|
@ -260,7 +260,7 @@ class _MediaViewerViewState extends State<MediaViewerView> {
|
|||
}
|
||||
|
||||
if (!content.isVideo) {
|
||||
if (content.maxShowTime != 999999) {
|
||||
if (content.maxShowTime != gMediaShowInfinite) {
|
||||
canBeSeenUntil = DateTime.now().add(
|
||||
Duration(seconds: content.maxShowTime),
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue