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 in the dropbox.

Implementation

@override
Future<String> uploadFile(
    {required String localPath,
    required String remotePath,
    Map<String, dynamic>? metadata}) {
  return _executeRequest(() async {
    final file = File(localPath);
    final fileSize = await file.length();
    final normalizedPath = _normalizePath(remotePath);
    debugPrint('Uploading $localPath to Dropbox at $normalizedPath');
    final response = await _dio.post(
      'https://content.dropboxapi.com/2/files/upload',
      data: file.openRead(),
      options: Options(
        headers: {
          // Dropbox API arguments are passed in a JSON header.
          'Dropbox-API-Arg': jsonEncode({
            'path': normalizedPath,
            'mode': 'overwrite',
            'autorename': false,
          }),
          'Content-Type': 'application/octet-stream',
          'Content-Length': fileSize,
        },
      ),
    );
    debugPrint(
        'Successfully uploaded file to Dropbox, ID: ${response.data['id']}');
    return response.data['id'];
  });
}