open static method

Future<UDocByteSource> open(
  1. String url, {
  2. Map<String, String>? headers,
  3. Directory? spillDirectory,
})

Implementation

static Future<UDocByteSource> open(String url, {Map<String, String>? headers, Directory? spillDirectory}) async {
  final Client client = Client();
  final Map<String, String> requestHeaders = <String, String>{...?headers};
  try {
    final Response head = await client.head(Uri.parse(url), headers: requestHeaders).timeout(uDocNetworkTimeout);
    final int size = int.tryParse(head.headers["content-length"] ?? "") ?? -1;
    final bool ranges = (head.headers["accept-ranges"] ?? "").toLowerCase().contains("bytes");
    if (ranges && size > 0) return UHttpRangeByteSource._(url, size, requestHeaders, client, ranges: true);
    final Response full = await client.get(Uri.parse(url), headers: requestHeaders).timeout(uDocNetworkTimeout);
    final Uint8List bytes = full.bodyBytes;
    client.close();
    if (spillDirectory != null && bytes.length > uDocSourceCacheBytes) {
      final File spill = File("${spillDirectory.path}${Platform.pathSeparator}u_doc_${bytes.length}_${DateTime.now().microsecondsSinceEpoch}.bin");
      await spill.writeAsBytes(bytes, flush: true);
      return await UFileByteSource.open(spill.path);
    }
    return UMemoryByteSource(bytes, id: "network:$url");
  } on TimeoutException {
    client.close();
    throw UDocError(code: UDocErrorCode.timeout, message: "Timed out opening document", path: url);
  } on Object catch (e) {
    client.close();
    throw UDocError(code: UDocErrorCode.network, message: "Cannot open document", detail: "$e", path: url);
  }
}