generateShareLink method

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

Generates a shareable link for the file or directory at the path.

Implementation

@override
Future<Uri?> generateShareLink(String path) {
  return _executeRequest(
    () async {
      final accessToken = await _getAccessToken();
      if (accessToken.isEmpty) {
        debugPrint(
            "OneDriveProvider: No access token available for generating share link.");
        return null;
      }
      // Construct the Microsoft Graph API path to create a share link for the item.
      final encodedPath = Uri.encodeComponent(
          path.startsWith('/') ? path.substring(1) : path);
      final driveItemPath = "/me/drive/root:/$encodedPath:/createLink";

      final response = await http.post(
        Uri.parse("https://graph.microsoft.com/v1.0$driveItemPath"),
        headers: {
          'Authorization': 'Bearer $accessToken',
          'Content-Type': 'application/json',
        },
        body: jsonEncode({
          "type": "edit",
          "scope": "anonymous"
        }), // Request an editable, public link.
      );
      if (response.statusCode != 200 && response.statusCode != 201) {
        debugPrint(
            "Failed to create shareable link. Status: ${response.statusCode}, Body: ${response.body}");
        return null;
      }
      // Parse the link from the JSON response.
      final json = jsonDecode(response.body);
      final link = json['link']?['webUrl'];
      return link != null ? Uri.parse(link) : null;
    },
    operation: 'generateSharableLink for $path',
  );
}