downloadFile method

Future<void> downloadFile(
  1. NextcloudItem item,
  2. String savePath
)

Implementation

Future<void> downloadFile(NextcloudItem item, String savePath) async {
  try {
    // The href already contains the full path like /public.php/webdav/file.txt
    // Extract the server URL from baseUrl and combine with href
    final baseUri = Uri.parse(baseUrl);
    final serverUrl =
        '${baseUri.scheme}://${baseUri.host}${baseUri.hasPort ? ':${baseUri.port}' : ''}';
    final url = Uri.parse('$serverUrl${item.href}');

    final response = await http.get(url, headers: _getHeaders());

    if (response.statusCode == 200) {
      final file = File(savePath);
      await file.writeAsBytes(response.bodyBytes);
    } else {
      throw Exception('Failed to download file: ${response.statusCode}');
    }
  } catch (e) {
    rethrow;
  }
}