downloadToOPFS method

Future<void> downloadToOPFS(
  1. String url,
  2. String filename, {
  3. String? authToken,
  4. required void onProgress(
    1. int percentage
    ),
  5. JSAny? abortSignal,
})

Download a model to OPFS with progress tracking and cancellation support

Parameters:

  • url: Download URL (HTTP/HTTPS)
  • filename: Filename to save in OPFS (used as cache key)
  • authToken: Optional authentication token (HuggingFace, etc.)
  • onProgress: Progress callback (receives percentage 0-100)
  • abortSignal: Optional JS AbortSignal for cancellation

Throws:

Implementation

Future<void> downloadToOPFS(
  String url,
  String filename, {
  String? authToken,
  required void Function(int percentage) onProgress,
  JSAny? abortSignal,
}) async {
  try {
    debugPrint('[WebOPFSService] Starting download: $filename');

    // Create JS callback for progress
    final jsProgressCallback = (JSNumber percentJs) {
      final percent = percentJs.toDartInt;
      onProgress(percent);
    }.toJS;

    // Call OPFS download with abort signal
    final result = await _opfs
        .downloadToOPFS(
          url.toJS,
          filename.toJS,
          authToken?.toJS,
          jsProgressCallback,
          abortSignal,
        )
        .toDart;

    if (!result.toDart) {
      throw Exception('OPFS download returned false');
    }

    debugPrint('[WebOPFSService] Download complete: $filename');
  } catch (e, stackTrace) {
    debugPrint('[WebOPFSService] Download failed: $e');
    debugPrint('[WebOPFSService] Stack trace: $stackTrace');
    throw Exception('Failed to download to OPFS: $e');
  }
}