fetchFile method

Future<FetchResponse> fetchFile(
  1. String url
)

Fetches a public file (no authentication).

Returns FetchResponse with downloaded data. Throws JsInteropException on fetch errors.

Implementation

Future<FetchResponse> fetchFile(String url) async {
  try {
    // Fetch without auth
    final promise = _fetchJs(url.toJS, JSObject());
    final jsResponse = await promise.toDart;
    final response = jsResponse as _Response;

    // Check response status
    if (!response.isOk) {
      throw JsInteropException(
        'Failed to fetch file: ${response.statusMessage}',
        statusCode: response.statusCode,
      );
    }

    // Stream response body
    final chunks = await _streamResponseBody(
      response,
      _getContentLength(response),
      (_) {}, // No progress callback for public files
    );

    // Concatenate chunks
    final totalLength = chunks.fold<int>(0, (sum, chunk) => sum + chunk.length);
    final data = Uint8List(totalLength);
    int offset = 0;
    for (final chunk in chunks) {
      data.setRange(offset, offset + chunk.length, chunk);
      offset += chunk.length;
    }

    return FetchResponse(data, response.statusCode);
  } catch (e) {
    if (e is JsInteropException) rethrow;
    throw JsInteropException('Failed to fetch file: $e');
  }
}