downloadIfNeeded static method

Future<bool> downloadIfNeeded()

Implementation

static Future<bool> downloadIfNeeded() async {
  final libName = _platformLibraryName();
  final target = _platformTarget();
  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 repoRoot = _findRepoRoot();
  if (repoRoot != null && File('${repoRoot.path}/target/release/$libName').existsSync()) return true;
  try {
    DynamicLibrary.open(libName);
    return true;
  } catch (_) {}

  final version = Platform.environment['HTML_TO_MARKDOWN_FFI_VERSION'] ?? _defaultVersion;
  final ext = Platform.isWindows ? 'dll' : libName.split('.').last;
  final url = 'https://github.com/$_repo/releases/download/v$version/libhtml_to_markdown_ffi-$target.$ext';
  try {
    final r = await http.get(Uri.parse(url));
    if (r.statusCode == 200) {
      await Directory('$home/.html_to_markdown_ffi').create(recursive: true);
      await File(cachePath).writeAsBytes(r.bodyBytes);
      return true;
    }
  } catch (_) {}
  return false;
}