get method

  1. @override
Resource get(
  1. Link link

Returns the Resource at the given link's HREF.

A Resource is always returned, since for some cases we can't know if it exists before actually fetching it, such as HTTP. Therefore, errors are handled at the Resource level.


Resource get(Link link) {
  String linkHref = link.href.addPrefix("/");
  for (MapEntry<String, FileSystemEntity> entry in paths.entries) {
    String itemHref = entry.key.addPrefix("/");
    FileSystemEntity itemFile = entry.value;
    if (linkHref.startsWith(itemHref)) {
      String path = p.join(
          itemFile.path, linkHref.removePrefix(itemHref).removePrefix("/"));
      File resourceFile = File(path);
      // Make sure that the requested resource is [path] or one of its descendant.
      if (resourceFile.canonicalPath.startsWith(itemFile.canonicalPath)) {
        Resource resource = FileResource(link, resourceFile);
        return resource;
  return FailureResource(link, ResourceException.notFound);