sendKml method

Future<void> sendKml(
  1. String kml, {
  2. List<String> images = const [],
})

Sends a KML file to the Liquid Galaxy system.

kml is the KML content to send.

Implementation

Future<void> sendKml(String kml, {List<String> images = const []}) async {
  if (!await isConnected()) {
    return;
  }

  try {
    for (String image in images) {
      await upload(image);
    }

    const fileName = 'upload.kml';

    SftpClient sftpClient = await getSftp();

    final remoteFile = await sftpClient.open('/var/www/html/$fileName',
        mode: SftpFileOpenMode.create |
            SftpFileOpenMode.write |
            SftpFileOpenMode.truncate);

    // Convert KML string to a stream and write it directly
    final kmlBytes = Uint8List.fromList(kml.codeUnits);
    await remoteFile.write(Stream.value(kmlBytes).cast<Uint8List>());
    await remoteFile.close();
    await sendCommand(
        'echo "http://$lgUrl/$fileName" > /var/www/html/kmls.txt');
  } catch (e) {
    print('Error during KML file upload: $e');
  }
}