mediatype

package
v0.0.0-...-70e1626 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2023 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const MaxReadSize = 5 * 1024 * 1024 // 5MB

Variables

View Source
var AAC, _ = New("audio/aac", "", "aac")
View Source
var ACSM, _ = New("application/vnd.adobe.adept+xml", "Adobe Content Server Message", "acsm")
View Source
var AIFF, _ = New("audio/aiff", "", "aiff")
View Source
var AVI, _ = New("video/x-msvideo", "", "avi")
View Source
var AVIF, _ = New("image/avif", "", "avif")
View Source
var BMP, _ = New("image/bmp", "Bitmap Image File", "bmp")
View Source
var Binary, _ = New("application/octet-stream", "", "")
View Source
var CBZ, _ = New("application/vnd.comicbook+zip", "Comic Book Archive", "cbz")
View Source
var CSS, _ = New("text/css", "Cascading Style Sheets", "css")
View Source
var Divina, _ = New("application/divina+zip", "Digital Visual Narratives", "divina")
View Source
var DivinaManifest, _ = New("application/divina+json", "Digital Visual Narratives", "json")
View Source
var EPUB, _ = New("application/epub+zip", "EPUB", "epub")
View Source
var GIF, _ = New("image/gif", "", "gif")
View Source
var GZ, _ = New("application/gzip", "", "gz")
View Source
var HTML, _ = New("text/html", "Hypertext Markup Language", "html")
View Source
var JPEG, _ = New("image/jpeg", "", "jpeg")
View Source
var JSON, _ = New("application/json", "JSON", "json")
View Source
var JXL, _ = New("image/jxl", "JPEG XL", "jxl")
View Source
var JavaScript, _ = New("text/javascript", "JavaScript", "js")
View Source
var LCPLicenseDocument, _ = New("application/vnd.readium.lcp.license.v1.0+json", "LCP License", "lcpl")
View Source
var LCPProtectedAudiobook, _ = New("application/audiobook+lcp", "LCP Protected Audiobook", "lcpa")
View Source
var LCPProtectedPDF, _ = New("application/pdf+lcp", "LCP Protected PDF", "lcpdf")
View Source
var LCPStatusDocument, _ = New("application/vnd.readium.license.status.v1.0+json", "LCP Status Document", "")
View Source
var LPF, _ = New("application/lpf+zip", "Lightweight Packaging Format", "lpf")
View Source
var MP3, _ = New("audio/mpeg", "", "mp3")
View Source
var MPEG, _ = New("video/mpeg", "", "mpeg")
View Source
var NCX, _ = New("application/x-dtbncx+xml", "Navigation Control File", "ncx")
View Source
var OGG, _ = New("audio/ogg", "", "oga")
View Source
var OGV, _ = New("video/ogg", "", "ogv")
View Source
var OPDS1, _ = New("application/atom+xml;profile=opds-catalog", "", "")
View Source
var OPDS1Entry, _ = New("application/atom+xml;type=entry;profile=opds-catalog", "", "")
View Source
var OPDS2, _ = New("application/opds+json", "", "")
View Source
var OPDS2Publication, _ = New("application/opds-publication+json", "", "")
View Source
var OPDSAuthentication, _ = New("application/opds-authentication+json", "", "")
View Source
var OPUS, _ = New("audio/opus", "", "opus")
View Source
var OTF, _ = New("font/otf", "OpenType Font", "otf")
View Source
var PDF, _ = New("application/pdf", "PDF", "pdf")
View Source
var PNG, _ = New("image/png", "Portable Network Graphics", "png")
View Source
var ReadiumAudiobook, _ = New("application/audiobook+zip", "Readium Audiobook", "audiobook")
View Source
var ReadiumAudiobookManifest, _ = New("application/audiobook+json", "Readium Audiobook", "json")
View Source
var ReadiumWebpub, _ = New("application/webpub+zip", "Readium Web Publication", "webpub")
View Source
var ReadiumWebpubManifest, _ = New("application/webpub+json", "Readium Web Publication", "json")
View Source
var SMIL, _ = New("application/smil+xml", "Synchronized Multimedia Integration Language", "smil")
View Source
var SVG, _ = New("image/svg+xml", "Scalable Vector Graphics", "svg")

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.

View Source
var TIFF, _ = New("image/tiff", "", "tiff")
View Source
var TTF, _ = New("font/ttf", "TrueType Font", "ttf")
View Source
var Text, _ = New("text/plain", "Text", "txt")
View Source
var W3CWPUBManifest, _ = New("application/x.readium.w3c.wpub+json", "Web Publication", "json") // non-existent
View Source
var WAV, _ = New("audio/wav", "", "wav")
View Source
var WEBMAudio, _ = New("audio/webm", "", "webm")
View Source
var WEBMVideo, _ = New("video/webm", "", "webm")
View Source
var WEBP, _ = New("image/webp", "", "webp")
View Source
var WOFF, _ = New("font/woff", "", "woff")
View Source
var WOFF2, _ = New("font/woff2", "", "woff2")
View Source
var XHTML, _ = New("application/xhtml+xml", "", "xhtml")
View Source
var XML, _ = New("application/xml", "Xtensible Markup Language", "xml")
View Source
var ZAB, _ = New("application/x.readium.zab+zip", "Zipped Audio Book", "zab") // non-existent
View Source
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

func New(str string, name string, extension string) (mt MediaType, err error)

Create a new MediaType. When an error is returned, do not use the resulting MediaType, as it will be incomplete/invalid

func NewOfString

func NewOfString(str string) (MediaType, error)

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 Of

func Of(mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType

Resolves a format from a list of mediatypes, list of extensions, and list of sniffers

func OfBytes

func OfBytes(bytes []byte, mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType

Resolves a format from bytes, e.g. from an HTTP response.

func OfBytesOnly

func OfBytesOnly(bytes []byte) *MediaType

Resolves a format from bytes, e.g. from an HTTP response, and nothing else

func OfExtension

func OfExtension(extension string) *MediaType

Resolves a format from a single file extension

func OfFile

func OfFile(file *os.File, mediaTypes []string, extensions []string, sniffers []Sniffer) *MediaType

Resolves a format from a file

func OfFileOnly

func OfFileOnly(file *os.File) *MediaType

Resolves a format from a file, and nothing else

func OfString

func OfString(mediaType string) *MediaType

Resolves a format from a single mediaType string

func OfStringAndExtension

func OfStringAndExtension(mediaType string, extension string) *MediaType

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 SniffBitmap

func SniffBitmap(context SnifferContext) *MediaType

Sniffs a bitmap image.

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 SniffHTML

func SniffHTML(context SnifferContext) *MediaType

Sniffs an HTML document.

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 SniffOPDS

func SniffOPDS(context SnifferContext) *MediaType

Sniffs an OPDS document.

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

func (mt MediaType) CanonicalMediaType() *MediaType

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) Charset

func (mt MediaType) Charset() encoding.Encoding

Encoding as declared in the `charset` parameter, if there's any.

func (MediaType) Contains

func (mt MediaType) Contains(other *MediaType) bool

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

func (mt MediaType) ContainsFromString(other string) bool

Returns whether the given [other] media type is included in this media type.

func (MediaType) Equal

func (mt MediaType) Equal(other *MediaType) bool

func (MediaType) IsAudio

func (mt MediaType) IsAudio() bool

Returns whether this media type is of an audio clip.

func (MediaType) IsBitmap

func (mt MediaType) IsBitmap() bool

Returns whether this media type is of a bitmap image, so excluding vector-based formats.

func (MediaType) IsHTML

func (mt MediaType) IsHTML() bool

Returns whether this media type is of an HTML document.

func (MediaType) IsImage

func (mt MediaType) IsImage() bool

Returns whether this media type is of an image.

func (MediaType) IsJSON

func (mt MediaType) IsJSON() bool

Returns whether this media type is structured as a JSON file.

func (MediaType) IsOPDS

func (mt MediaType) IsOPDS() bool

Returns whether this media type is of an OPDS feed.

func (MediaType) IsPublication

func (mt MediaType) IsPublication() bool

Returns whether this media type is of a publication file.

func (MediaType) IsRwpm

func (mt MediaType) IsRwpm() bool

Returns whether this media type is of a Readium Web Publication Manifest.

func (MediaType) IsVideo

func (mt MediaType) IsVideo() bool

Returns whether this media type is of a video clip.

func (MediaType) IsZIP

func (mt MediaType) IsZIP() bool

Returns whether this media type is structured as a ZIP archive.

func (MediaType) MarshalText

func (mt MediaType) MarshalText() ([]byte, error)

For JSON Marshaling

func (MediaType) Matches

func (mt MediaType) Matches(other ...*MediaType) bool

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

func (mt MediaType) MatchesFromString(other ...string) bool

Returns whether this media type and `other` are the same, ignoring parameters that are not in both media types.

func (MediaType) String

func (mt MediaType) String() string

The string representation of this media type.

func (MediaType) StructuredSyntaxSuffix

func (mt MediaType) StructuredSyntaxSuffix() string

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 SnifferContent interface {
	Read() []byte
	Stream() io.Reader
}

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

type XMLNode

type XMLNode struct {
	XMLName xml.Name
	Content []byte    `xml:",innerxml"`
	Nodes   []XMLNode `xml:",any"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL