|
1 | 1 | import 'package:drift/drift.dart'; |
| 2 | +import 'package:fixnum/fixnum.dart'; |
2 | 3 | import 'package:meesign_native/meesign_native.dart'; |
3 | 4 | import 'package:meesign_network/grpc.dart' as rpc; |
4 | 5 |
|
@@ -34,13 +35,51 @@ class DecryptRepository extends TaskRepository<Decrypt> { |
34 | 35 | List<int> gid, |
35 | 36 | ) async { |
36 | 37 | final enc = ElGamalWrapper.encrypt(data, gid); |
37 | | - await _dispatcher.unauth.decrypt( |
38 | | - rpc.DecryptRequest() |
39 | | - ..groupId = gid |
40 | | - ..name = description |
41 | | - ..dataType = dataType.value |
42 | | - ..data = enc, |
43 | | - ); |
| 38 | + |
| 39 | + // Use streaming for large encrypted data (> 1MB) |
| 40 | + if (enc.length > 1024 * 1024) { |
| 41 | + await _encryptStreaming(description, dataType, enc, gid); |
| 42 | + } else { |
| 43 | + // Use regular RPC for small data |
| 44 | + await _dispatcher.unauth.decrypt( |
| 45 | + rpc.DecryptRequest() |
| 46 | + ..groupId = gid |
| 47 | + ..name = description |
| 48 | + ..dataType = dataType.value |
| 49 | + ..data = enc, |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Stream large encrypted data in chunks to avoid gRPC message size limits. |
| 55 | + Future<void> _encryptStreaming( |
| 56 | + String description, |
| 57 | + MimeType dataType, |
| 58 | + List<int> encryptedData, |
| 59 | + List<int> gid, |
| 60 | + ) async { |
| 61 | + const chunkSize = 256 * 1024; // 256KB chunks |
| 62 | + |
| 63 | + Stream<rpc.DecryptRequestChunk> chunks() async* { |
| 64 | + // First chunk: metadata |
| 65 | + yield rpc.DecryptRequestChunk() |
| 66 | + ..metadata = (rpc.DecryptMetadata() |
| 67 | + ..name = description |
| 68 | + ..groupId = gid |
| 69 | + ..dataType = dataType.value |
| 70 | + ..totalSize = Int64(encryptedData.length)); |
| 71 | + |
| 72 | + // Subsequent chunks: data |
| 73 | + for (var i = 0; i < encryptedData.length; i += chunkSize) { |
| 74 | + final end = (i + chunkSize < encryptedData.length) |
| 75 | + ? i + chunkSize |
| 76 | + : encryptedData.length; |
| 77 | + |
| 78 | + yield rpc.DecryptRequestChunk()..chunk = encryptedData.sublist(i, end); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + await _dispatcher.unauth.decryptStream(chunks()); |
44 | 83 | } |
45 | 84 |
|
46 | 85 | @override |
|
0 commit comments