download method

Future<bool> download({
  1. required String modelUrl,
  2. String? modelFilename,
  3. CactusProgressCallback? onProgress,
})

Implementation

Future<bool> download({
  required String modelUrl,
  String? modelFilename,
  CactusProgressCallback? onProgress,
}) async {
  try {
    final actualFilename = modelFilename ?? modelUrl.split('/').last;
    if (actualFilename.isEmpty) {
      throw ArgumentError('Cannot determine filename from URL and no filename provided');
    }

    final downloadParams = CactusInitParams(
      modelUrl: modelUrl,
      modelFilename: actualFilename,
      onInitProgress: onProgress,
    );

    try {
      final tempContext = await CactusContext.init(downloadParams);
      tempContext.release();
      _lastDownloadedFilename = actualFilename;
      return true;
    } catch (e) {
      final appDocDir = await getApplicationDocumentsDirectory();
      final modelPath = '${appDocDir.path}/$actualFilename';
      final file = File(modelPath);
      if (await file.exists()) {
        _lastDownloadedFilename = actualFilename;
        return true;
      }
      rethrow;
    }
  } catch (e) {
    if (onProgress != null) {
      onProgress(null, "Download failed: ${e.toString()}", true);
    }
    return false;
  }
}