diff --git a/.gitignore b/.gitignore index 78f1ae6..7b9ba80 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .packages .tmp/ +.data/** # Conventional directory for build output. build/ diff --git a/lib/database.dart b/lib/database.dart index 6d761f6..e49df33 100644 --- a/lib/database.dart +++ b/lib/database.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-06 16:15:01 - * @LastEditTime : 2022-10-09 20:14:06 + * @LastEditTime : 2022-10-09 20:28:49 * @Description : */ @@ -482,7 +482,6 @@ class DataBaseHelper { } var filePath = '${Directory.current.path}/.data/files/$fileMd5'; await tempFile.copy(filePath); - tempFile.delete(); try { await _database.insert( 'files', diff --git a/lib/tcpcontroller/controller.dart b/lib/tcpcontroller/controller.dart index 5037e2a..f0548e8 100644 --- a/lib/tcpcontroller/controller.dart +++ b/lib/tcpcontroller/controller.dart @@ -1,7 +1,7 @@ /* * @Author : Linloir * @Date : 2022-10-08 15:10:04 - * @LastEditTime : 2022-10-09 17:55:26 + * @LastEditTime : 2022-10-09 20:21:53 * @Description : */ @@ -17,8 +17,6 @@ class TCPController { //Stores the incoming bytes of the TCP connection temporarily final List buffer = []; - //Stores the fetched require temporarily - List _requestBytes = []; //Byte length for json object int requestLength = 0; @@ -66,23 +64,19 @@ class TCPController { var tempFile = File('${Directory.current.path}/.tmp/${DateTime.now().microsecondsSinceEpoch}')..createSync(); //Initialize payload transmission controller _payloadStreamController = StreamController(); - //Bind file to stream - _payloadStreamController.stream.listen( - (data) { - tempFile.writeAsBytesSync(data, mode: FileMode.append, flush: true); - }, - onDone: () { - //Payload definetely ends after request is buffered - //Therefore transmit the request and payload to stream here - _inStreamController.add(TCPRequest(_requestBytes, tempFile)); + //Create a future that listens to the status of the payload transmission + var payloadTransmission = Future(() async { + await for(var data in _payloadStreamController.stream) { + await tempFile.writeAsBytes(data, mode: FileMode.append, flush: true); } - ); + }); //Bind request construction on stream _requestStreamController = StreamController(); _requestStreamController.stream.listen((requestBytes) { //When request stream is closed by controller - //Request is intact, save to _request temporarily - _requestBytes = requestBytes; + payloadTransmission.then((_) { + _inStreamController.add(TCPRequest(requestBytes, tempFile)); + }); }); } else {