generateSharableLink method

  1. @override
Future<Uri?> generateSharableLink(
  1. String path
)
override

Generates a sharable link for a FOLDER that allows other users to edit its contents.

Implementation

@override
Future<Uri?> generateSharableLink(String path) async {
  _checkAuth();
  final normalizedPath = _normalizePath(path);

  try {
    await _dio.post(
      'https://api.dropboxapi.com/2/sharing/share_folder',
      data: jsonEncode({
        'path': normalizedPath,
        'acl_update_policy': 'owner',
        'shared_link_policy': 'anyone'
      }),
      options: Options(contentType: 'application/json'),
    );
  } on DioException catch (e) {
    if (e.response?.data?['error_summary']
            ?.contains('bad_path/already_shared') ==
        false) {
      rethrow;
    }
    debugPrint("Folder is already shared, proceeding...");
  }

  try {
    final response = await _dio.post(
      'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings',
      data: jsonEncode({
        'path': normalizedPath,
        'settings': {
          'requested_visibility': 'public',
          'access': 'editor',
        }
      }),
      options: Options(contentType: 'application/json'),
    );
    return Uri.parse(response.data['url']);
  } on DioException catch (e) {
    final errorData = e.response?.data;
    if (errorData is Map &&
        errorData['error_summary']
            .toString()
            .contains('shared_link_already_exists')) {
      final listResponse = await _dio.post(
        'https://api.dropboxapi.com/2/sharing/list_shared_links',
        data: jsonEncode({'path': normalizedPath, 'direct_only': true}),
        options: Options(contentType: 'application/json'),
      );
      if ((listResponse.data['links'] as List).isNotEmpty) {
        return Uri.parse(listResponse.data['links'][0]['url']);
      }
    }
    debugPrint("Failed to create or retrieve shared link: $e");
    rethrow;
  }
}