downloadFile method

  1. @override
Future<String> downloadFile({
  1. required String remotePath,
  2. required String localPath,
})
override

Downloads a file from a remotePath to a localPath on the device.

Implementation

@override
Future<String> downloadFile({
  required String remotePath,
  required String localPath,
}) {
  return _executeRequest(() async {
    // Find the file by its path to get its ID.
    final file = await _getFileByPath(remotePath);
    if (file == null || file.id == null) {
      throw Exception('GoogleDriveProvider: File not found at $remotePath');
    }
    final output = File(localPath);
    final sink = output.openWrite();
    try {
      // Download the file content by its ID.
      final media = await driveApi.files.get(file.id!,
          downloadOptions: drive.DownloadOptions.fullMedia) as drive.Media;
      // Stream the content to the local file.
      await media.stream.pipe(sink);
    } catch (e) {
      await sink.close(); // Ensure sink is closed on error.
      if (await output.exists()) {
        await output.delete(); // Clean up partial file on failure.
      }
      rethrow;
    }
    await sink.close();
    return localPath;
  });
}