retrieveFile method

Future<void> retrieveFile(
  1. String filename
)

Implementation

Future<void> retrieveFile(String filename) async {
  if (!openDataConnection()) {
    return;
  }

  try {
    transferInProgress = true;

    if (!fileOperations.exists(filename)) {
      sendResponse('550 File not found $filename');
      transferInProgress = false;
      return;
    }
    String fullPath = fileOperations.resolvePath(filename);

    File file = File(fullPath);
    if (await file.exists()) {
      Stream<List<int>> fileStream = file.openRead();
      fileStream.listen(
        (data) {
          if (transferInProgress) {
            dataSocket?.add(data);
          }
        },
        onDone: () async {
          if (transferInProgress) {
            transferInProgress = false;
            await dataSocket?.close();
            dataSocket = null;
            sendResponse('226 Transfer complete');
          }
        },
        onError: (error) async {
          if (transferInProgress) {
            sendResponse('426 Connection closed; transfer aborted');
            transferInProgress = false;
            await dataSocket?.close();
            dataSocket = null;
          }
        },
        cancelOnError: true,
      );
    } else {
      sendResponse('550 File not found $fullPath');
      transferInProgress = false;
    }
  } catch (e) {
    sendResponse('550 File transfer failed');
    transferInProgress = false;
    dataSocket = null;
  }
}