webpub static method
Sniffs a Readium Web Publication, protected or not by LCP.
Implementation
static Future<MediaType?> webpub(SnifferContext context) async {
if (context.hasFileExtension(["audiobook"]) ||
context.hasMediaType("application/audiobook+zip")) {
return MediaType.readiumAudiobook;
}
if (context.hasMediaType("application/audiobook+json")) {
return MediaType.readiumAudiobookManifest;
}
if (context.hasFileExtension(["divina"]) ||
context.hasMediaType("application/divina+zip")) {
return MediaType.divina;
}
if (context.hasMediaType("application/divina+json")) {
return MediaType.divinaManifest;
}
if (context.hasFileExtension(["webpub"]) ||
context.hasMediaType("application/webpub+zip")) {
return MediaType.readiumWebpub;
}
if (context.hasMediaType("application/webpub+json")) {
return MediaType.readiumWebpubManifest;
}
if (context.hasFileExtension(["lcpa"]) ||
context.hasMediaType("application/audiobook+lcp")) {
return MediaType.lcpProtectedAudiobook;
}
if (context.hasFileExtension(["lcpdf"]) ||
context.hasMediaType("application/pdf+lcp")) {
return MediaType.lcpProtectedPdf;
}
// Reads a RWPM, either from a manifest.json file, or from a manifest.json archive entry, if
// the file is an archive.
bool isManifest = true;
Manifest? manifest;
try {
// manifest.json
manifest = await context.contentAsRwpm() ??
// Archive package
(await context.readArchiveEntryAt("manifest.json"))
?.let((it) => Manifest.fromJson(
utf8.decode(it.buffer.asUint8List()).toJsonOrNull()))
?.also((it) => isManifest = false);
} on Exception {
manifest = null;
}
if (manifest != null) {
bool isLcpProtected =
await context.containsArchiveEntryAt("license.lcpl");
if (manifest.metadata.type == "http://schema.org/Audiobook" ||
manifest.readingOrder.allAreAudio) {
if (isManifest) {
return MediaType.readiumAudiobookManifest;
} else {
return (isLcpProtected)
? MediaType.lcpProtectedAudiobook
: MediaType.readiumAudiobook;
}
}
if (manifest.readingOrder.allAreBitmap) {
return (isManifest) ? MediaType.divinaManifest : MediaType.divina;
}
if (isLcpProtected &&
manifest.readingOrder.allMatchMediaType(MediaType.pdf)) {
return MediaType.lcpProtectedPdf;
}
if (manifest
.linkWithRel("self")
?.mediaType
.matchesFromName("application/webpub+json") ==
true) {
return (isManifest)
? MediaType.readiumWebpubManifest
: MediaType.readiumWebpub;
}
}
return null;
}