Skip to content

Commit da23a4b

Browse files
committed
fix: file writing regression
1 parent f2d7487 commit da23a4b

File tree

2 files changed

+67
-48
lines changed

2 files changed

+67
-48
lines changed

lib/app/view/error_page.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ErrorBody extends StatelessWidget {
7070
header: Text(
7171
error.toString().split(': ').firstOrNull ?? error.toString(),
7272
style: Theme.of(context).textTheme.bodyMedium,
73+
textAlign: TextAlign.center,
7374
),
7475
child: SelectableText(error.toString()),
7576
),
@@ -111,7 +112,7 @@ Add any other context about the problem here.
111112
```
112113
------------ Platform: ------------
113114
Platform: ${kIsWeb ? 'Web' : Platform.operatingSystem} ${kIsWeb ? '' : Platform.operatingSystemVersion}
114-
115+
115116
------------ Error: ------------
116117
${error.toString().splitMapJoin(RegExp('.{1,100}'), onMatch: (m) => '${m.group(0)}\n', onNonMatch: (n) => n)}
117118
```

lib/events/chat_download_manager.dart

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import 'dart:io';
44
import 'package:collection/collection.dart';
55
import 'package:flutter_it/flutter_it.dart';
66
import 'package:matrix/matrix.dart';
7+
import 'package:mime/mime.dart';
78
import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart';
89
import 'package:path_provider/path_provider.dart';
910
import 'package:safe_change_notifier/safe_change_notifier.dart';
1011
import 'package:xdg_directories/xdg_directories.dart';
12+
import 'package:path/path.dart' as p;
1113

14+
import '../app/app_config.dart';
1215
import '../common/platforms.dart';
1316
import '../extensions/event_x.dart';
1417
import '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

Comments
 (0)