getSharedFileById method

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

Implementation

@override
Future<String> getSharedFileById({
  required String fileId,
  required String localPath,
}) async {
  _checkAuth();

  final output = File(localPath);
  final sink = output.openWrite();

  try {
    final media = await driveApi.files.get(
      fileId,
      downloadOptions: drive.DownloadOptions.fullMedia,
    ) as drive.Media;

    await media.stream.pipe(sink);
    await sink.close();
  } catch (e) {
    await sink.close();
    if (await output.exists()) {
      await output.delete();
    }
    debugPrint("Error downloading shared file by ID: $e");
    if (e is drive.DetailedApiRequestError &&
        (e.status == 401 || e.status == 403)) {
      debugPrint("Authentication error during download.");
      _isAuthenticated = false;
    }
    rethrow;
  }

  return localPath;
}