handleCopy method

Future<void> handleCopy(
  1. String srcPath,
  2. String destPath
)

Implementation

Future<void> handleCopy(String srcPath, String destPath) async {
  await _prepareClient();
  final src = await client.resolvePath(srcPath);
  if (src['type'] == 'folder') _exit('Folder copy not yet supported.');

  Map<String, dynamic>? destFolder;
  String targetName;

  try {
    final destObj = await client.resolvePath(destPath);
    if (destObj['type'] == 'folder') {
      destFolder = destObj;
      targetName = p.basename(srcPath);
    } else {
      if (!force) _exit('Destination exists. Use -f to overwrite.');
      final parentPath = p.dirname(destPath);
      destFolder =
          await client.resolvePath(parentPath == '.' ? '/' : parentPath);
      targetName = p.basename(destPath);
    }
  } catch (_) {
    final parentPath = p.dirname(destPath);
    try {
      destFolder =
          await client.resolvePath(parentPath == '.' ? '/' : parentPath);
    } catch (e) {
      _exit('Destination parent not found.');
    }
    targetName = p.basename(destPath);
  }

  print(
      '\ud83d\udccb Copying "${src['path']}" to "${destFolder['path']}/$targetName"...');

  final tempDir = Directory.systemTemp.createTempSync('filen_cli_cp_');
  final tempFile = File(p.join(tempDir.path, targetName));

  try {
    stdout.write('   1/2 Downloading...  \r');
    await client.downloadFile(src['uuid'], savePath: tempFile.path);

    stdout.write('   2/2 Uploading...    \r');
    await client.uploadFile(tempFile, destFolder['uuid']);

    print('\n\u2705 Copy complete.');
  } catch (e) {
    print('');
    _exit('Copy failed: $e');
  } finally {
    if (tempFile.existsSync()) tempFile.deleteSync();
    if (tempDir.existsSync()) tempDir.deleteSync();
  }
}