urlForAbsPath method

Future<String> urlForAbsPath(
  1. String absPath
)

Implementation

Future<String> urlForAbsPath(String absPath) async {
  final root = await getApplicationDocumentsDirectory();
  final rootPath = p.normalize(root.path);
  final ap = p.normalize(absPath);

  if (!p.isWithin(rootPath, ap) && ap != rootPath) {
    throw StateError('absPath not under ApplicationSupportDirectory: $ap');
  }

  final rel = p.relative(ap, from: rootPath).replaceAll('\\', '/');
  final safePath = Uri(path: '/$rel').toString(); // 自动编码中文/空格
  return 'http://127.0.0.1:$port$safePath';
}