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 {
  if (!_isAuthenticated) {
    throw Exception('Not authenticated');
  }

  final accessToken = await DefaultTokenManager(
    tokenEndpoint: OneDrive.tokenEndpoint,
    clientID: client.clientID,
    redirectURL: client.redirectURL,
    scope: client.scopes,
  ).getAccessToken();

  if (accessToken == null || accessToken.isEmpty) {
    throw Exception('Access token is null or empty');
  }

  final fileBytes = await File(localPath).readAsBytes();

  final response = await http.put(
    Uri.parse(
        'https://graph.microsoft.com/v1.0/me/drive/items/$fileId/content'),
    headers: {
      'Authorization': 'Bearer $accessToken',
      'Content-Type': 'application/octet-stream',
    },
    body: fileBytes,
  );

  if (response.statusCode >= 200 && response.statusCode < 300) {
    return fileId;
  } else {
    throw Exception(
        'Failed to upload file by ID: ${response.statusCode}, ${response.body}');
  }
}