storeFile method

Future<void> storeFile(
  1. String filename
)

Implementation

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

  try {
    String fullPath = fileOperations.resolvePath(filename);

    transferInProgress = true;

    // Create the directory if it doesn't exist
    final directory = Directory(fullPath).parent;
    if (!await directory.exists()) {
      await directory.create(recursive: true);
    }

    File file = File(fullPath);
    IOSink fileSink = file.openWrite();

    dataSocket!.listen(
      (data) {
        if (transferInProgress) {
          fileSink.add(data);
        }
      },
      onDone: () async {
        if (transferInProgress) {
          await fileSink.close();
          transferInProgress = false;
          await _closeDataSocket();
          sendResponse('226 Transfer complete');
        }
      },
      onError: (error) async {
        if (transferInProgress) {
          sendResponse('426 Connection closed; transfer aborted');
          await fileSink.close();
          transferInProgress = false;

          await dataSocket!.close();
          dataSocket = null;
        }
      },
      cancelOnError: true,
    );
  } catch (e) {
    sendResponse('550 Error creating file or directory: $e');
    transferInProgress = false;
    dataSocket = null;
  }
}