resolve method

Future<String> resolve(
  1. String documentPath, {
  2. String? cacheKey,
})

Resolves a document path to a native-accessible path.

For asset paths, copies the asset to a temporary location. For other paths, returns them as-is.

Implementation

Future<String> resolve(String documentPath, {String? cacheKey}) async {
  // If it's an asset path, copy to temp directory
  if (documentPath.startsWith('assets/') ||
      documentPath.startsWith('asset/')) {
    final key = cacheKey ?? documentPath;

    // Return cached path if available
    if (_resolvedPaths.containsKey(key)) {
      return _resolvedPaths[key]!;
    }

    // Copy asset to temp directory
    final tempDir = await getTemporaryDirectory();
    final fileName = documentPath.split('/').last;
    final tempFile = File('${tempDir.path}/$fileName');

    final byteData = await rootBundle.load(documentPath);
    final buffer = byteData.buffer;
    await tempFile.writeAsBytes(
      buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),
    );

    final resolvedPath = tempFile.path;
    _resolvedPaths[key] = resolvedPath;
    return resolvedPath;
  }

  // For other paths, return as-is
  return documentPath;
}