@@ -4,11 +4,14 @@ import 'dart:io';
44import 'package:collection/collection.dart' ;
55import 'package:flutter_it/flutter_it.dart' ;
66import 'package:matrix/matrix.dart' ;
7+ import 'package:mime/mime.dart' ;
78import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart' ;
89import 'package:path_provider/path_provider.dart' ;
910import 'package:safe_change_notifier/safe_change_notifier.dart' ;
1011import 'package:xdg_directories/xdg_directories.dart' ;
12+ import 'package:path/path.dart' as p;
1113
14+ import '../app/app_config.dart' ;
1215import '../common/platforms.dart' ;
1316import '../extensions/event_x.dart' ;
1417import 'chat_download_service.dart' ;
@@ -30,17 +33,25 @@ class ChatDownloadManager extends SafeChangeNotifier {
3033 _tempDirectory ?? = (Platforms .isLinux
3134 ? configHome
3235 : await getTemporaryDirectory ());
36+
37+ if (_tempDirectory == null ) throw Exception ('No temp dir!' );
38+ final baseDirPath = p.join (_tempDirectory! .path, AppConfig .appName);
3339 for (final event in events) {
34- final filePath = '${ _tempDirectory ?. path }/${ event .fileName }' ;
40+ final filePath = p. join (baseDirPath, event.fileName) ;
3541 if (File (filePath).existsSync () &&
3642 downloadedEventsInTemp.none (
3743 (c) => c.event.fileName == event.fileName,
3844 )) {
45+ final file = File (filePath);
3946 downloadedEventsInTemp.add (
4047 DownloadCapsule (
4148 event: event,
42- file: File (filePath),
43- matrixFile: null ,
49+ file: file,
50+ matrixFile: MatrixFile .fromMimeType (
51+ name: p.basename (file.path),
52+ bytes: file.readAsBytesSync (),
53+ mimeType: lookupMimeType (file.path),
54+ ),
4455 ),
4556 );
4657 }
@@ -64,51 +75,58 @@ class ChatDownloadManager extends SafeChangeNotifier {
6475 }, initialValue: null );
6576
6677 final _downloadCommands = < Event , Command <void , DownloadCapsule ?>> {};
67- Command <void , DownloadCapsule ?> getDownloadCommand (Event event) =>
68- _downloadCommands.putIfAbsent (
69- event,
70- () => Command .createAsyncWithProgress ((_, handle) async {
71- Directory ? tempDir;
72-
73- tempDir = await getTemporaryDirectory ();
74-
75- File ? file;
76- MatrixFile ? matrixFile;
77-
78- final path = '${tempDir .path }/${event .fileName }' ;
79- if (File (path).existsSync ()) {
80- file = File (path);
81- } else {
82- matrixFile = await event.downloadAndDecryptAttachment (
83- onDownloadProgress: (v) {
84- // the amount of downloaded bytes is v
85- // the total size of the file is event.fileSize
86- final progress = event.fileSize != null && event.fileSize! > 0
87- ? v / event.fileSize!
88- : null ;
89- handle.updateProgress (progress ?? 0 );
90- },
91- );
92- }
93-
94- if (file != null &&
95- Platform .isIOS &&
96- matrixFile? .mimeType.toLowerCase () == 'audio/ogg' ) {
97- Logs ().v ('Convert ogg audio file for iOS...' );
98- final convertedFile = File ('${file .path }.caf' );
99- if (await convertedFile.exists () == false ) {
100- OpusCaf ().convertOpusToCaf (file.path, convertedFile.path);
101- }
102- file = convertedFile;
103- }
78+ Command <void , DownloadCapsule ?> getDownloadCommand (
79+ Event event,
80+ ) => _downloadCommands.putIfAbsent (
81+ event,
82+ () => Command .createAsyncWithProgress ((_, handle) async {
83+ _tempDirectory ?? = (Platforms .isLinux
84+ ? configHome
85+ : await getTemporaryDirectory ());
86+
87+ if (_tempDirectory == null ) throw Exception ('No temp dir!' );
88+ final baseDirPath = p.join (_tempDirectory! .path, AppConfig .appName);
89+
90+ File ? file;
91+ MatrixFile ? matrixFile;
92+
93+ if (! Directory (baseDirPath).existsSync ()) {
94+ Directory (baseDirPath).createSync ();
95+ }
96+
97+ final path = p.join (baseDirPath, event.fileName);
98+ if (File (path).existsSync ()) {
99+ file = File (path);
100+ matrixFile = MatrixFile .fromMimeType (
101+ name: p.basename (file.path),
102+ bytes: file.readAsBytesSync (),
103+ mimeType: lookupMimeType (file.path),
104+ );
105+ } else {
106+ matrixFile = await event.downloadAndDecryptAttachment (
107+ onDownloadProgress: (v) {
108+ final progress = event.fileSize != null && event.fileSize! > 0
109+ ? v / event.fileSize!
110+ : null ;
111+ handle.updateProgress (progress ?? 0 );
112+ },
113+ );
114+
115+ file = File (path)..writeAsBytesSync (matrixFile.bytes);
116+ }
117+
118+ if (Platform .isIOS && matrixFile.mimeType.toLowerCase () == 'audio/ogg' ) {
119+ Logs ().v ('Convert ogg audio file for iOS...' );
120+ final convertedFile = File ('${file .path }.caf' );
121+ if (await convertedFile.exists () == false ) {
122+ OpusCaf ().convertOpusToCaf (file.path, convertedFile.path);
123+ }
124+ file = convertedFile;
125+ }
104126
105- return DownloadCapsule (
106- event: event,
107- file: file,
108- matrixFile: matrixFile,
109- );
110- }, initialValue: null ),
111- );
127+ return DownloadCapsule (event: event, file: file, matrixFile: matrixFile);
128+ }, initialValue: null ),
129+ );
112130
113131 final _saveFileCommands =
114132 <
0 commit comments