-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzip_isolate.dart
199 lines (172 loc) · 4.74 KB
/
zip_isolate.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'package:archive/archive_io.dart';
import 'package:path/path.dart';
import 'package:sentry/sentry.dart';
import 'backup_provider.dart';
import 'progress_update.dart';
/// All photos are stored under this path in the zip file
const zipPhotoRoot = 'photos/';
Future<void> zipBackup({
required BackupProvider provider,
required String pathToZip,
required String pathToBackupFile,
}) async {
// Set up communication channels
final receivePort = ReceivePort();
final errorPort = ReceivePort();
final exitPort = ReceivePort();
// Start the zipping isolate
await Isolate.spawn<_ZipParams>(
_zipFiles,
_ZipParams(
sendPort: receivePort.sendPort,
pathToZip: pathToZip,
pathToBackupFile: pathToBackupFile,
photosRootPath: await provider.photosRootPath,
zipPhotoRoot: zipPhotoRoot,
progressStageStart: 3,
progressStageEnd: 5,
),
onError: errorPort.sendPort,
onExit: exitPort.sendPort,
);
// Listen for progress updates from the isolate
final completer = Completer<void>();
errorPort.listen((error) {
Sentry.captureException(error);
completer.completeError(error as Object);
});
exitPort.listen((message) {
if (!completer.isCompleted) {
completer.complete();
}
});
receivePort.listen((message) {
if (message is ProgressUpdate) {
provider.emitProgress(
message.stageDescription,
message.stageNo,
message.stageCount,
);
}
});
// Wait for the isolate to finish
await completer.future;
receivePort.close();
errorPort.close();
exitPort.close();
}
// _ZipParams class
// _zipFiles function
Future<void> _zipFiles(_ZipParams params) async {
await Sentry.init((options) {
options
..dsn =
'https://17bb41df4a5343530bfcb92553f4c5a7@o4507706035994624.ingest.us.sentry.io/4507706038157312'
..tracesSampleRate = 1.0;
});
final encoder = ZipFileEncoder();
final sendPort = params.sendPort;
try {
encoder.create(params.pathToZip);
// Emit progress: Zipping database
sendPort.send(
ProgressUpdate(
'Zipping database ${params.pathToBackupFile}',
params.progressStageStart,
params.progressStageEnd,
),
);
await encoder.addFile(File(params.pathToBackupFile));
await encoder.close();
// Notify completion
sendPort.send(
ProgressUpdate(
'Zipping completed',
params.progressStageEnd,
params.progressStageEnd,
),
);
// ignore: avoid_catches_without_on_clauses
} catch (e, st) {
await Sentry.captureException(e, stackTrace: st);
// Send error back to the main isolate
sendPort.send(
ProgressUpdate(
'Error during zipping: $e',
params.progressStageEnd,
params.progressStageEnd,
),
);
}
/// Isolate won't shutdown if we don't terminate sentry.
await Sentry.close();
}
Future<String?> extractFiles(
BackupProvider provider,
File backupFile,
String tmpDir,
int stageNo,
int stageCount,
) async {
final encoder = ZipDecoder();
String? dbPath;
// Extract the ZIP file contents to a temporary directory
final archive = encoder.decodeStream(InputFileStream(backupFile.path));
const restored = 0;
for (final file in archive) {
final filename = file.name;
final filePath = join(tmpDir, filename);
provider.emitProgress(
'Restoring $restored/${archive.length}',
stageNo,
stageCount,
);
if (file.isFile) {
// If the file is the database extact it
// to a temp dir and return the path.
if (filename.endsWith('.db')) {
dbPath = filePath;
await _expandZippedFileToDisk(filePath, file);
}
// Write files to the temporary directory
if (filename.startsWith(zipPhotoRoot)) {
final parts = split(filename);
final photoDestPath = joinAll([
await provider.photosRootPath,
...parts.sublist(1),
]);
await _expandZippedFileToDisk(photoDestPath, file);
}
}
}
return dbPath;
}
Future<void> _expandZippedFileToDisk(
String photoDestPath,
ArchiveFile file,
) async {
File(photoDestPath)
..createSync(recursive: true)
..writeAsBytesSync(file.content as List<int>);
}
class _ZipParams {
_ZipParams({
required this.sendPort,
required this.pathToZip,
required this.pathToBackupFile,
required this.photosRootPath,
required this.zipPhotoRoot,
required this.progressStageStart,
required this.progressStageEnd,
});
final SendPort sendPort;
final String pathToZip;
final String pathToBackupFile;
final String photosRootPath;
final String zipPhotoRoot;
final int progressStageStart;
final int progressStageEnd;
}