uploadFile method

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

Uploads a file from a localPath to a remotePath.

Implementation

@override
Future<String> uploadFile({
  required String localPath,
  required String remotePath,
  Map<String, dynamic>? metadata,
}) {
  return _executeRequest(() async {
    // Check if a file already exists at the remote path.
    final existingFile = await _getFileByPath(remotePath);
    if (existingFile != null && existingFile.id != null) {
      // If it exists, update it using its file ID.
      return uploadFileByShareToken(
        localPath: localPath,
        shareToken: existingFile.id!,
        metadata: metadata,
      );
    } else {
      // If it doesn't exist, create it.
      final file = File(localPath);
      final fileName = basename(remotePath);
      final remoteDir = dirname(remotePath) == '.' ? '' : dirname(remotePath);
      // Ensure the parent directory exists.
      final folder = await _getOrCreateFolder(remoteDir);
      final driveFile = drive.File()
        ..name = fileName
        ..parents = [folder.id!];
      final media = drive.Media(file.openRead(), await file.length());
      final uploadedFile = await driveApi.files
          .create(driveFile, uploadMedia: media, $fields: 'id, name');
      return uploadedFile.id!;
    }
  });
}