opds static method

Future<MediaType?> opds(
  1. SnifferContext context
)

Sniffs an OPDS document.

Implementation

static Future<MediaType?> opds(SnifferContext context) async {
  // OPDS 1
  if (context
      .hasMediaType("application/atom+xml;type=entry;profile=opds-catalog")) {
    return MediaType.opds1Entry;
  }
  if (context.hasMediaType("application/atom+xml;profile=opds-catalog")) {
    return MediaType.opds1;
  }
  MediaType? xmlMediaType = (await context.contentAsXml())?.let((xml) {
    if (xml.name.namespaceUri == "http://www.w3.org/2005/Atom") {
      if (xml.name.local == "feed") {
        return MediaType.opds1;
      } else if (xml.name.local == "entry") {
        return MediaType.opds1Entry;
      }
    }
    return null;
  });
  if (xmlMediaType != null) {
    return xmlMediaType;
  }

  // OPDS 2
  if (context.hasMediaType("application/opds+json")) {
    return MediaType.opds2;
  }
  if (context.hasMediaType("application/opds-publication+json")) {
    return MediaType.opds2Publication;
  }
  MediaType? rwpmMediaType = (await context.contentAsRwpm())?.let((rwpm) {
    if (rwpm
            .linkWithRel("self")
            ?.mediaType
            .matchesFromName("application/opds+json") ==
        true) {
      return MediaType.opds2;
    }
    if (rwpm.links._firstWithRelMatching(
            (it) => it.startsWith("http://opds-spec.org/acquisition")) !=
        null) {
      return MediaType.opds2Publication;
    }
    return null;
  });
  if (rwpmMediaType != null) {
    return rwpmMediaType;
  }

  // OPDS Authentication Document.
  if (context.hasAnyOfMediaTypes([
    "application/opds-authentication+json",
    "application/vnd.opds.authentication.v1.0+json"
  ])) {
    return MediaType.opdsAuthentication;
  }
  if (await context.containsJsonKeys(["id", "title", "authentication"])) {
    return MediaType.opdsAuthentication;
  }

  return null;
}