downloadIfNeeded static method

Future<bool> downloadIfNeeded()

Download the native library from GitHub release (call before first use). Returns true if the library is available after download.

Implementation

static Future<bool> downloadIfNeeded() async {
  final libName = _platformLibraryName();
  final target = _platformTarget();
  final ext = Platform.isWindows ? 'dll' : libName.split('.').last;
  final home = Platform.environment['HOME'] ?? '/tmp';
  final cachePath = '$home/.html_to_markdown_ffi/$libName';

  if (File(cachePath).existsSync()) return true;

  final envPath = Platform.environment['HTML_TO_MARKDOWN_FFI_LIB_PATH'];
  if (envPath != null && File(envPath).existsSync()) return true;

  final version = Platform.environment['HTML_TO_MARKDOWN_FFI_VERSION'] ?? _defaultVersion;
  final url = 'https://github.com/$_repo/releases/download/v$version/libhtml_to_markdown_ffi-$target.$ext';

  try {
    final client = http.Client();
    final response = await client.get(Uri.parse(url));
    client.close();
    if (response.statusCode == 200) {
      final dir = Directory('$home/.html_to_markdown_ffi');
      await dir.create(recursive: true);
      await File(cachePath).writeAsBytes(response.bodyBytes);
      return true;
    }
  } catch (_) {}
  return false;
}