Documentation ¶
Index ¶
- Constants
- Variables
- type MediaType
- func New(str string, name string, extension string) (mt MediaType, err error)
- func NewOfString(str string) (MediaType, error)
- func Of(mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType
- func OfBytes(bytes []byte, mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType
- func OfBytesOnly(bytes []byte) *MediaType
- func OfExtension(extension string) *MediaType
- func OfFile(file *os.File, mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType
- func OfFileOnly(file *os.File) *MediaType
- func OfString(mediaType string) *MediaType
- func OfStringAndExtension(mediaType string, extension string) *MediaType
- func SniffArchive(context SnifferContext) *MediaType
- func SniffAudio(context SnifferContext) *MediaType
- func SniffBitmap(context SnifferContext) *MediaType
- func SniffEPUB(context SnifferContext) *MediaType
- func SniffHTML(context SnifferContext) *MediaType
- func SniffLCPLicense(context SnifferContext) *MediaType
- func SniffLPF(context SnifferContext) *MediaType
- func SniffOPDS(context SnifferContext) *MediaType
- func SniffPDF(context SnifferContext) *MediaType
- func SniffSystem(context SnifferContext) *MediaType
- func SniffW3CWPUB(context SnifferContext) *MediaType
- func SniffWebpub(context SnifferContext) *MediaType
- func SniffXHTML(context SnifferContext) *MediaType
- func (mt MediaType) CanonicalMediaType() *MediaType
- func (mt MediaType) Charset() encoding.Encoding
- func (mt MediaType) Contains(other *MediaType) bool
- func (mt MediaType) ContainsFromString(other string) bool
- func (mt MediaType) Equal(other *MediaType) bool
- func (mt MediaType) IsAudio() bool
- func (mt MediaType) IsBitmap() bool
- func (mt MediaType) IsHTML() bool
- func (mt MediaType) IsImage() bool
- func (mt MediaType) IsJSON() bool
- func (mt MediaType) IsOPDS() bool
- func (mt MediaType) IsPublication() bool
- func (mt MediaType) IsRwpm() bool
- func (mt MediaType) IsVideo() bool
- func (mt MediaType) IsZIP() bool
- func (mt MediaType) MarshalText() ([]byte, error)
- func (mt MediaType) Matches(other ...*MediaType) bool
- func (mt MediaType) MatchesFromString(other ...string) bool
- func (mt MediaType) String() string
- func (mt MediaType) StructuredSyntaxSuffix() string
- type Sniffer
- type SnifferBytesContent
- type SnifferContent
- type SnifferContext
- func (s SnifferContext) ArchiveEntriesAllSatisfy() bool
- func (s *SnifferContext) Charset() encoding.Encoding
- func (s SnifferContext) ContainsArchiveEntryAt(path string) bool
- func (s SnifferContext) ContainsJSONKeys(keys ...string) bool
- func (s *SnifferContext) ContentAsArchive() (archive.Archive, error)
- func (s SnifferContext) ContentAsJSON() map[string]interface{}
- func (s SnifferContext) ContentAsRWPM()
- func (s SnifferContext) ContentAsString() (string, error)
- func (s SnifferContext) ContentAsXML() *XMLNode
- func (s SnifferContext) FileExtensions() []string
- func (s SnifferContext) HasFileExtension(fileExtensions ...string) bool
- func (s SnifferContext) HasMediaType(mediaTypes ...string) bool
- func (s SnifferContext) MediaTypes() []MediaType
- func (s SnifferContext) Read(start int64, end int64) []byte
- func (s SnifferContext) ReadArchiveEntryAt(path string) []byte
- func (s SnifferContext) Stream() io.Reader
- type SnifferFileContent
- type XMLNode
Constants ¶
const MaxReadSize = 5 * 1024 * 1024 // 5MB
Variables ¶
var AAC, _ = New("audio/aac", "", "aac")
var ACSM, _ = New("application/vnd.adobe.adept+xml", "Adobe Content Server Message", "acsm")
var AIFF, _ = New("audio/aiff", "", "aiff")
var AVI, _ = New("video/x-msvideo", "", "avi")
var AVIF, _ = New("image/avif", "", "avif")
var BMP, _ = New("image/bmp", "Bitmap Image File", "bmp")
var Binary, _ = New("application/octet-stream", "", "")
var CBZ, _ = New("application/vnd.comicbook+zip", "Comic Book Archive", "cbz")
var CSS, _ = New("text/css", "Cascading Style Sheets", "css")
var Divina, _ = New("application/divina+zip", "Digital Visual Narratives", "divina")
var DivinaManifest, _ = New("application/divina+json", "Digital Visual Narratives", "json")
var EPUB, _ = New("application/epub+zip", "EPUB", "epub")
var GIF, _ = New("image/gif", "", "gif")
var GZ, _ = New("application/gzip", "", "gz")
var HTML, _ = New("text/html", "Hypertext Markup Language", "html")
var JPEG, _ = New("image/jpeg", "", "jpeg")
var JSON, _ = New("application/json", "JSON", "json")
var JXL, _ = New("image/jxl", "JPEG XL", "jxl")
var JavaScript, _ = New("text/javascript", "JavaScript", "js")
var LCPLicenseDocument, _ = New("application/vnd.readium.lcp.license.v1.0+json", "LCP License", "lcpl")
var LCPProtectedAudiobook, _ = New("application/audiobook+lcp", "LCP Protected Audiobook", "lcpa")
var LCPProtectedPDF, _ = New("application/pdf+lcp", "LCP Protected PDF", "lcpdf")
var LCPStatusDocument, _ = New("application/vnd.readium.license.status.v1.0+json", "LCP Status Document", "")
var LPF, _ = New("application/lpf+zip", "Lightweight Packaging Format", "lpf")
var MP3, _ = New("audio/mpeg", "", "mp3")
var MPEG, _ = New("video/mpeg", "", "mpeg")
var NCX, _ = New("application/x-dtbncx+xml", "Navigation Control File", "ncx")
var OGG, _ = New("audio/ogg", "", "oga")
var OGV, _ = New("video/ogg", "", "ogv")
var OPDS1, _ = New("application/atom+xml;profile=opds-catalog", "", "")
var OPDS1Entry, _ = New("application/atom+xml;type=entry;profile=opds-catalog", "", "")
var OPDS2, _ = New("application/opds+json", "", "")
var OPDS2Publication, _ = New("application/opds-publication+json", "", "")
var OPDSAuthentication, _ = New("application/opds-authentication+json", "", "")
var OPUS, _ = New("audio/opus", "", "opus")
var OTF, _ = New("font/otf", "OpenType Font", "otf")
var PDF, _ = New("application/pdf", "PDF", "pdf")
var PNG, _ = New("image/png", "Portable Network Graphics", "png")
var ReadiumAudiobook, _ = New("application/audiobook+zip", "Readium Audiobook", "audiobook")
var ReadiumAudiobookManifest, _ = New("application/audiobook+json", "Readium Audiobook", "json")
var ReadiumWebpub, _ = New("application/webpub+zip", "Readium Web Publication", "webpub")
var ReadiumWebpubManifest, _ = New("application/webpub+json", "Readium Web Publication", "json")
var SMIL, _ = New("application/smil+xml", "Synchronized Multimedia Integration Language", "smil")
var SVG, _ = New("image/svg+xml", "Scalable Vector Graphics", "svg")
var Sniffers = []Sniffer{ SniffEPUB, SniffLPF, SniffArchive, SniffPDF, SniffXHTML, SniffHTML, SniffBitmap, SniffAudio, SniffOPDS, SniffLCPLicense, SniffW3CWPUB, SniffWebpub, }
The default sniffers provided by Readium 2 to resolve a MediaType. You can register additional sniffers globally by modifying this list. The sniffers order is important, because some formats are subsets of other formats.
var TIFF, _ = New("image/tiff", "", "tiff")
var TTF, _ = New("font/ttf", "TrueType Font", "ttf")
var Text, _ = New("text/plain", "Text", "txt")
var W3CWPUBManifest, _ = New("application/x.readium.w3c.wpub+json", "Web Publication", "json") // non-existent
var WAV, _ = New("audio/wav", "", "wav")
var WEBMAudio, _ = New("audio/webm", "", "webm")
var WEBMVideo, _ = New("video/webm", "", "webm")
var WEBP, _ = New("image/webp", "", "webp")
var WOFF, _ = New("font/woff", "", "woff")
var WOFF2, _ = New("font/woff2", "", "woff2")
var XHTML, _ = New("application/xhtml+xml", "", "xhtml")
var XML, _ = New("application/xml", "Xtensible Markup Language", "xml")
var ZAB, _ = New("application/x.readium.zab+zip", "Zipped Audio Book", "zab") // non-existent
var ZIP, _ = New("application/zip", "ZIP Archive", "zip")
Functions ¶
This section is empty.
Types ¶
type MediaType ¶
type MediaType struct { Type string // The type component, e.g. `application` in `application/epub+zip`. SubType string // The subtype component, e.g. `epub+zip` in `application/epub+zip`. Parameters map[string]string // The parameters in the media type, such as `charset=utf-8`. // contains filtered or unexported fields }
MediaType represents a document format, identified by a unique RFC 6838 media type. MediaType handles:
- components parsing – eg. type, subtype and parameters,
- media types comparison.
Comparing media types is more complicated than it looks, since they can contain parameters, such as `charset=utf-8`. We can't ignore them because some formats use parameters in their media type, for example `application/atom+xml;profile=opds-catalog` for an OPDS 1 catalog.
Specification: https://tools.ietf.org/html/rfc6838
func New ¶
Create a new MediaType. When an error is returned, do not use the resulting MediaType, as it will be incomplete/invalid
func NewOfString ¶
Create a new MediaType solely from a mime string. When an error is returned, do not use the resulting MediaType, as it will be incomplete/invalid
func OfBytesOnly ¶
Resolves a format from bytes, e.g. from an HTTP response, and nothing else
func OfExtension ¶
Resolves a format from a single file extension
func OfFileOnly ¶
Resolves a format from a file, and nothing else
func OfStringAndExtension ¶
func SniffArchive ¶
func SniffArchive(context SnifferContext) *MediaType
Sniffs a simple Archive-based format, like Comic Book Archive or Zipped Audio Book. Reference: https://wiki.mobileread.com/wiki/CBR_and_CBZ
func SniffEPUB ¶
func SniffEPUB(context SnifferContext) *MediaType
Sniffs an EPUB publication. Reference: https://www.w3.org/publishing/epub3/epub-ocf.html#sec-zip-container-mime
func SniffLCPLicense ¶
func SniffLCPLicense(context SnifferContext) *MediaType
Sniffs an LCP License Document.
func SniffLPF ¶
func SniffLPF(context SnifferContext) *MediaType
Sniffs a Lightweight Packaging Format (LPF). References:
func SniffPDF ¶
func SniffPDF(context SnifferContext) *MediaType
Sniffs a PDF document. Reference: https://www.loc.gov/preservation/digital/formats/fdd/fdd000123.shtml
func SniffSystem ¶
func SniffSystem(context SnifferContext) *MediaType
func SniffW3CWPUB ¶
func SniffW3CWPUB(context SnifferContext) *MediaType
Sniffs a W3C Web Publication Manifest.
func SniffWebpub ¶
func SniffWebpub(context SnifferContext) *MediaType
Sniffs a Readium Web Publication, protected or not by LCP.
func SniffXHTML ¶
func SniffXHTML(context SnifferContext) *MediaType
Sniffs an XHTML document. Must precede the HTML sniffer.
func (MediaType) CanonicalMediaType ¶
Returns the canonical version of this media type, if it is known.
This is useful to find the name and file extension of a known media type, or to get the canonical media type from an alias. For example, `application/x-cbz` is an alias of the canonical `application/vnd.comicbook+zip`.
Non-significant parameters are also discarded.
func (MediaType) Contains ¶
Returns whether the given [other] media type is included in this media type. For example, `text/html` contains `text/html;charset=utf-8`.
- [other] must match the parameters in the [parameters] property, but extra parameters are ignored. - Order of parameters is ignored. - Wildcards are supported, meaning that `image/*` contains `image/png` and `*/*` contains everything.
func (MediaType) ContainsFromString ¶
Returns whether the given [other] media type is included in this media type.
func (MediaType) IsBitmap ¶
Returns whether this media type is of a bitmap image, so excluding vector-based formats.
func (MediaType) IsPublication ¶
Returns whether this media type is of a publication file.
func (MediaType) MarshalText ¶
For JSON Marshaling
func (MediaType) Matches ¶
Returns whether this media type and `other` are the same, ignoring parameters that are not in both media types. For example, `text/html` matches `text/html;charset=utf-8`, but `text/html;charset=ascii` doesn't. This is basically like `contains`, but working in both directions.
func (MediaType) MatchesFromString ¶
Returns whether this media type and `other` are the same, ignoring parameters that are not in both media types.
func (MediaType) StructuredSyntaxSuffix ¶
Structured syntax suffix, e.g. `+zip` in `application/epub+zip`.
Gives a hint on the underlying structure of this media type. See https://tools.ietf.org/html/rfc6838#section-4.2.8
type Sniffer ¶
type Sniffer func(context SnifferContext) *MediaType
type SnifferBytesContent ¶
type SnifferBytesContent struct {
// contains filtered or unexported fields
}
Used to sniff a byte array.
func NewSnifferBytesContent ¶
func NewSnifferBytesContent(bytes []byte) SnifferBytesContent
func (SnifferBytesContent) Read ¶
func (s SnifferBytesContent) Read() []byte
Read implements SnifferContent
func (SnifferBytesContent) Stream ¶
func (s SnifferBytesContent) Stream() io.Reader
Stream implements SnifferContent
type SnifferContent ¶
type SnifferContext ¶
type SnifferContext struct {
// contains filtered or unexported fields
}
A companion type of Sniffer holding the type hints (file extensions, media types) and providing an access to the file content.
func (SnifferContext) ArchiveEntriesAllSatisfy ¶
func (s SnifferContext) ArchiveEntriesAllSatisfy() bool
func (*SnifferContext) Charset ¶
func (s *SnifferContext) Charset() encoding.Encoding
Finds the first [Charset] declared in the media types' `charset` parameter.
func (SnifferContext) ContainsArchiveEntryAt ¶
func (s SnifferContext) ContainsArchiveEntryAt(path string) bool
Returns whether an Archive entry exists in this file.
func (SnifferContext) ContainsJSONKeys ¶
func (s SnifferContext) ContainsJSONKeys(keys ...string) bool
Returns whether the content is a JSON object containing all of the given root keys.
func (*SnifferContext) ContentAsArchive ¶
func (s *SnifferContext) ContentAsArchive() (archive.Archive, error)
Content as an Archive instance. Warning: Archive is only supported for a local file, for now.
func (SnifferContext) ContentAsJSON ¶
func (s SnifferContext) ContentAsJSON() map[string]interface{}
Content parsed as generic JSON interface.
func (SnifferContext) ContentAsRWPM ¶
func (s SnifferContext) ContentAsRWPM()
Content parsed as a Readium Web Publication Manifest.
func (SnifferContext) ContentAsString ¶
func (s SnifferContext) ContentAsString() (string, error)
Content as plain text. Extracts the charset parameter from the media type hints to figure out an encoding. Otherwise, UTF-8 is assumed.
func (SnifferContext) ContentAsXML ¶
func (s SnifferContext) ContentAsXML() *XMLNode
Content as an XML document. TODO expand on this!
func (SnifferContext) FileExtensions ¶
func (s SnifferContext) FileExtensions() []string
File extension hints.
func (SnifferContext) HasFileExtension ¶
func (s SnifferContext) HasFileExtension(fileExtensions ...string) bool
Returns whether this context has any of the given file extensions, ignoring case.
func (SnifferContext) HasMediaType ¶
func (s SnifferContext) HasMediaType(mediaTypes ...string) bool
Returns whether this context has any of the given media type, ignoring case and extra parameters.
func (SnifferContext) MediaTypes ¶
func (s SnifferContext) MediaTypes() []MediaType
Media type hints.
func (SnifferContext) Read ¶
func (s SnifferContext) Read(start int64, end int64) []byte
Reads all the bytes or the given [range]. It can be used to check a file signature, aka magic number. See https://en.wikipedia.org/wiki/List_of_file_signatures Warning: This ignores errors, and just returns nil
func (SnifferContext) ReadArchiveEntryAt ¶
func (s SnifferContext) ReadArchiveEntryAt(path string) []byte
Returns the Archive entry data at the given path in this file.
func (SnifferContext) Stream ¶
func (s SnifferContext) Stream() io.Reader
Raw bytes stream of the content. A byte stream can be useful when sniffers only need to read a few bytes at the beginning of the file.
type SnifferFileContent ¶
type SnifferFileContent struct {
// contains filtered or unexported fields
}
Used to sniff a local file.
func NewSnifferFileContent ¶
func NewSnifferFileContent(file *os.File) SnifferFileContent
func (SnifferFileContent) Read ¶
func (s SnifferFileContent) Read() []byte
Read implements SnifferContent
func (SnifferFileContent) Stream ¶
func (s SnifferFileContent) Stream() io.Reader
Stream implements SnifferContent