Skip to content

Commit

Permalink
TW-1561: move filePath in app downloads to utils class
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn authored and hoangdat committed Apr 1, 2024
1 parent 059b7fc commit 5175e2a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
10 changes: 7 additions & 3 deletions lib/pages/chat/events/message_download_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:fluffychat/utils/manager/download_manager/download_file_state.da
import 'package:fluffychat/utils/manager/download_manager/download_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/download_file_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/storage_directory_utils.dart';
import 'package:fluffychat/widgets/file_widget/download_file_tile_widget.dart';
import 'package:fluffychat/widgets/file_widget/file_tile_widget.dart';
import 'package:fluffychat/widgets/file_widget/message_file_tile_style.dart';
Expand Down Expand Up @@ -50,7 +51,10 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>

void checkDownloadFileState() async {
checkFileExistInMemory();
final filePath = await widget.event.getFileNameInAppDownload();
final filePath = await StorageDirectoryUtils.instance.getFilePathInAppDownloads(
eventId: widget.event.eventId,
fileName: widget.event.filename,
);
final file = File(filePath);
if (await file.exists() &&
await file.length() == widget.event.getFileSize()) {
Expand All @@ -68,9 +72,9 @@ class _MessageDownloadContentState extends State<MessageDownloadContent>

void checkFileExistInMemory() {
final filePathInMem = widget.event.getFilePathFromMem();
if (filePathInMem.isNotEmpty) {
if (filePathInMem?.isNotEmpty == true) {
downloadFileStateNotifier.value = DownloadedPresentationState(
filePath: filePathInMem,
filePath: filePathInMem!,
);
return;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/presentation/extensions/send_file_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,18 @@ extension SendFileExtension on Room {
required String fileName,
}) async {
try {
final downloadInAppFolder =
await StorageDirectoryUtils.instance.getDownloadFolderInApp();
final filePathInAppDownloads = '$downloadInAppFolder/$eventId/$fileName';
final fileInMem = sendingFilePlaceholders[sendingEventId]?.filePath;
final filePathInAppDownloads =
await StorageDirectoryUtils.instance.getFilePathInAppDownloads(
eventId: eventId,
fileName: fileName,
);
final sendingFilePath = sendingFilePlaceholders[sendingEventId]?.filePath;
final file = File(filePathInAppDownloads);
if (await file.exists() || fileInMem == null) {
if (await file.exists() || sendingFilePath == null) {
return;
}
await file.create(recursive: true);
await File(fileInMem).copy(filePathInAppDownloads);
await File(sendingFilePath).copy(filePathInAppDownloads);
Logs().d('File copied in app downloads folder', filePathInAppDownloads);
} catch (e) {
Logs().e('Error while copying file in app downloads folder', e);
Expand Down
9 changes: 4 additions & 5 deletions lib/utils/matrix_sdk_extensions/download_file_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ extension DownloadFileExtension on Event {

return downloadOrRetrieveAttachment(
mxcUrl,
await getFileNameInAppDownload(),
await StorageDirectoryUtils.instance.getFilePathInAppDownloads(
eventId: eventId,
fileName: filename,
),
downloadStreamController: downloadStreamController,
getThumbnail: getThumbnail,
cancelToken: cancelToken,
Expand Down Expand Up @@ -349,8 +352,4 @@ extension DownloadFileExtension on Event {

return fileInfo;
}

Future<String> getFileNameInAppDownload() async {
return '${await StorageDirectoryUtils.instance.getDownloadFolderInApp()}/$eventId/$filename';
}
}
4 changes: 2 additions & 2 deletions lib/utils/matrix_sdk_extensions/event_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ extension LocalizedBody on Event {
return room.sendingFilePlaceholders[txId];
}

String getFilePathFromMem() {
String? getFilePathFromMem() {
final matrixFile = getMatrixFile();
return matrixFile?.filePath ?? '';
return matrixFile?.filePath;
}

Size? getOriginalResolution() {
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/storage_directory_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ class StorageDirectoryUtils {
_tempDirectoryPath ??= (await getTemporaryDirectory()).path;
return '$_tempDirectoryPath/Downloads';
}

Future<String> getFilePathInAppDownloads({
required String eventId,
required String fileName,
}) async {
final downloadInAppFolder =
await StorageDirectoryUtils.instance.getDownloadFolderInApp();
return '$downloadInAppFolder/$eventId/$fileName';
}
}

0 comments on commit 5175e2a

Please sign in to comment.