generateShareLink method
Generates a shareable link for the file or directory at the path
.
Implementation
@override
Future<Uri?> generateShareLink(String path) {
return _executeRequest(() async {
final normalizedPath = _normalizePath(path);
debugPrint('Generating sharable link for Dropbox path: $normalizedPath');
try {
// Attempt to create a new public, editable share link.
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'),
);
final url = response.data['url'];
debugPrint('Created sharable link: $url');
return url == null ? null : Uri.parse(url);
} on DioException catch (e) {
// If a link already exists, fetch the existing one.
if (e.response?.data?['error_summary']
?.contains('shared_link_already_exists') ==
true) {
debugPrint(
'Share link already exists for $normalizedPath, fetching existing one.');
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'),
);
final links = listResponse.data['links'] as List?;
if (links != null && links.isNotEmpty) {
final url = links.first['url'];
debugPrint('Fetched existing sharable link: $url');
return url == null ? null : Uri.parse(url);
}
}
rethrow;
}
});
}