uploadFileById method

  1. @override
Future<String> uploadFileById({
  1. required String localPath,
  2. required String fileId,
  3. Map<String, dynamic>? metadata,
})
override

Uploads a file to the cloud storage.

Implementation

@override
Future<String> uploadFileById({
  required String localPath,
  required String fileId,
  Map<String, dynamic>? metadata,
}) async {
  _checkAuth();

  final file = File(localPath);

  final driveFile =
      drive.File(); // Metadata changes can be added here if needed

  final media = drive.Media(file.openRead(), await file.length());
  drive.File updatedFile;
  try {
    updatedFile = await driveApi.files.update(
      driveFile,
      fileId,
      uploadMedia: media,
      $fields: 'id',
    );
  } catch (e) {
    debugPrint("Error uploading file by ID: $e");
    if (e is drive.DetailedApiRequestError &&
        (e.status == 401 || e.status == 403)) {
      debugPrint(
          "Authentication error during upload. The user may not have permission, or the token is invalid.");
      _isAuthenticated = false;
    }
    rethrow;
  }

  return updatedFile.id!;
}