downloadIfNeeded static method
Ensures the native library is available before NativeLibrary is used.
Returns true immediately when the library is already provided — by the
copy that ships inside this package (native/<rid>/), the environment
variable, the resolver seam, the sibling adapter, the
~/.html_to_markdown_ffi/ cache, or the system loader. Otherwise
downloads the matching GitHub release asset into
~/.html_to_markdown_ffi/.
The download is a last resort: it covers a platform that ships no bundled artifact (Linux, Windows) and a host that has lost the bundled one. On macOS a consumer never needs it.
Implementation
static Future<bool> downloadIfNeeded() async {
final bundled = _bundledLibraryPath();
if (bundled != null && File(bundled).existsSync()) return true;
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;
}