joinSharedFolder method

Future<Map<String, String>> joinSharedFolder({
  1. required String sharedLinkUrl,
})

Implementation

Future<Map<String, String>> joinSharedFolder(
    {required String sharedLinkUrl}) async {
  _checkAuth();

  String targetSharedFolderId;
  try {
    final metaResponse = await _dio.post(
      'https://api.dropboxapi.com/2/sharing/get_shared_link_metadata',
      data: jsonEncode({'url': sharedLinkUrl}),
      options: Options(contentType: 'application/json'),
    );
    targetSharedFolderId =
        metaResponse.data['id'].toString().replaceFirst('id:', '');
  } catch (e) {
    debugPrint("Could not get metadata from shared link: $e");
    throw Exception(
        "Invalid shared link or insufficient permissions to view it.");
  }

  try {
    debugPrint("Attempting to mount folder with ID: $targetSharedFolderId");
    final mountResponse = await _dio.post(
      'https://api.dropboxapi.com/2/sharing/mount_folder',
      data: jsonEncode({'shared_folder_id': targetSharedFolderId}),
      options: Options(contentType: 'application/json'),
    );

    debugPrint("Successfully mounted folder: ${mountResponse.data['name']}");
    return {
      'path_lower': mountResponse.data['path_lower'],
      'name': mountResponse.data['name']
    };
  } on DioException catch (e) {
    final error = e.response?.data?['error'];
    if (error != null && error['.tag'] == 'already_mounted') {
      debugPrint("Folder is already mounted. Locating it...");
      final folderInfo = await _findMountedFolderInfo(sharedLinkUrl);
      if (folderInfo == null) {
        throw Exception(
            "Could not locate the folder in Dropbox, even though it is reportedly mounted.");
      }
      return folderInfo;
    }
    debugPrint("Error mounting folder: $e");
    throw Exception(
        "Could not mount the shared folder. The user may not have permission or another API error occurred.");
  }
}