storeFile method

Future<void> storeFile(
  1. String filename
)

Implementation

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

  if (!_isPathAllowed(fullPath)) {
    sendResponse('550 Access denied');
    return;
  }

  try {
    // 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) {
        fileSink.add(data);
      },
      onDone: () async {
        await fileSink.close();
        await dataSocket!.close();
        dataSocket = null;
        sendResponse('226 Transfer complete');
      },
      onError: (error) async {
        sendResponse('426 Connection closed; transfer aborted');
        await fileSink.close();
        await dataSocket!.close();
        dataSocket = null;
      },
      cancelOnError: true, // This will cancel the subscription on error
    );
  } catch (e) {
    sendResponse('550 Error creating file or directory $e');
    dataSocket = null;
  }
}