Documentation ¶
Overview ¶
Package media contains Media Type (MIME type) related types and functions.
Index ¶
- Constants
- Variables
- func DecodeTypes(in map[string]any) (*config.ConfigNamespace[map[string]MediaTypeConfig, Types], error)
- func InitMediaType(m *Type)
- type BuiltinTypes
- type MediaTypeConfig
- type SuffixInfo
- type Type
- type Types
- func (t Types) BySuffix(suffix string) []Type
- func (t Types) GetByMainSubType(mainType, subType string) (tp Type, found bool)
- func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool)
- func (t Types) GetByType(tp string) (Type, bool)
- func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool)
- func (t Types) IsTextSuffix(suffix string) bool
- func (t Types) Len() int
- func (t Types) Less(i, j int) bool
- func (t Types) Swap(i, j int)
Constants ¶
const (
DefaultDelimiter = "."
)
Variables ¶
var ( Builtin = BuiltinTypes{ CalendarType: Type{Type: "text/calendar"}, CSSType: Type{Type: "text/css"}, SCSSType: Type{Type: "text/x-scss"}, SASSType: Type{Type: "text/x-sass"}, CSVType: Type{Type: "text/csv"}, HTMLType: Type{Type: "text/html"}, JavascriptType: Type{Type: "text/javascript"}, TypeScriptType: Type{Type: "text/typescript"}, TSXType: Type{Type: "text/tsx"}, JSXType: Type{Type: "text/jsx"}, JSONType: Type{Type: "application/json"}, WebAppManifestType: Type{Type: "application/manifest+json"}, RSSType: Type{Type: "application/rss+xml"}, XMLType: Type{Type: "application/xml"}, SVGType: Type{Type: "image/svg+xml"}, TextType: Type{Type: "text/plain"}, TOMLType: Type{Type: "application/toml"}, YAMLType: Type{Type: "application/yaml"}, PNGType: Type{Type: "image/png"}, JPEGType: Type{Type: "image/jpeg"}, GIFType: Type{Type: "image/gif"}, TIFFType: Type{Type: "image/tiff"}, BMPType: Type{Type: "image/bmp"}, WEBPType: Type{Type: "image/webp"}, TrueTypeFontType: Type{Type: "font/ttf"}, OpenTypeFontType: Type{Type: "font/otf"}, PDFType: Type{Type: "application/pdf"}, MarkdownType: Type{Type: "text/markdown"}, AVIType: Type{Type: "video/x-msvideo"}, MPEGType: Type{Type: "video/mpeg"}, MP4Type: Type{Type: "video/mp4"}, OGGType: Type{Type: "video/ogg"}, WEBMType: Type{Type: "video/webm"}, GPPType: Type{Type: "video/3gpp"}, WasmType: Type{Type: "application/wasm"}, OctetType: Type{Type: "application/octet-stream"}, } )
Functions ¶
func DecodeTypes ¶
func DecodeTypes(in map[string]any) (*config.ConfigNamespace[map[string]MediaTypeConfig, Types], error)
DecodeTypes decodes the given map of media types.
func InitMediaType ¶ added in v0.112.0
func InitMediaType(m *Type)
Types ¶
type BuiltinTypes ¶ added in v0.112.0
type BuiltinTypes struct { CalendarType Type CSSType Type SCSSType Type SASSType Type CSVType Type HTMLType Type JavascriptType Type TypeScriptType Type TSXType Type JSXType Type JSONType Type WebAppManifestType Type RSSType Type XMLType Type SVGType Type TextType Type TOMLType Type YAMLType Type // Common image types PNGType Type JPEGType Type GIFType Type TIFFType Type BMPType Type WEBPType Type // Common font types TrueTypeFontType Type OpenTypeFontType Type // Common document types PDFType Type MarkdownType Type // Common video types AVIType Type MPEGType Type MP4Type Type OGGType Type WEBMType Type GPPType Type // wasm WasmType Type OctetType Type }
type MediaTypeConfig ¶ added in v0.112.0
type MediaTypeConfig struct { // The file suffixes used for this media type. Suffixes []string // Delimiter used before suffix. Delimiter string }
Hold the configuration for a given media type.
type SuffixInfo ¶ added in v0.82.0
type SuffixInfo struct { // Suffix is the suffix without the delimiter, e.g. "xml". Suffix string `json:"suffix"` // FullSuffix is the suffix with the delimiter, e.g. ".xml". FullSuffix string `json:"fullSuffix"` }
SuffixInfo holds information about a Media Type's suffix.
type Type ¶
type Type struct { // The full MIME type string, e.g. "application/rss+xml". Type string `json:"-"` // The top-level type name, e.g. "application". MainType string `json:"mainType"` // The subtype name, e.g. "rss". SubType string `json:"subType"` // The delimiter before the suffix, e.g. ".". Delimiter string `json:"delimiter"` // FirstSuffix holds the first suffix defined for this MediaType. FirstSuffix SuffixInfo `json:"-"` // E.g. "jpg,jpeg" // Stored as a string to make Type comparable. // For internal use only. SuffixesCSV string `json:"-"` // contains filtered or unexported fields }
MediaType (also known as MIME type and content type) is a two-part identifier for file formats and format contents transmitted on the Internet. For Hugo's use case, we use the top-level type name / subtype name + suffix. One example would be application/svg+xml If suffix is not provided, the sub type will be used. <docsmeta>{ "name": "MediaType" }</docsmeta>
func FromContent ¶ added in v0.91.0
FromContent resolve the Type primarily using http.DetectContentType. If http.DetectContentType resolves to application/octet-stream, a zero Type is returned. If http.DetectContentType resolves to text/plain or application/xml, we try to get more specific using types and ext.
func FromString ¶
FromString creates a new Type given a type string on the form MainType/SubType and an optional suffix, e.g. "text/html" or "text/html+html".
func FromStringAndExt ¶ added in v0.45.1
FromStringAndExt creates a Type from a MIME string and a given extension.
func MustFromString ¶ added in v0.112.0
MustFromString is like FromString but panics on error.
func WithDelimiterAndSuffixes ¶ added in v0.82.0
WithDelimiterAndSuffixes is used in tests.
func (Type) IsText ¶ added in v0.91.0
IsText returns whether this Type is a text format. Note that this may currently return false negatives. TODO(bep) improve For internal use.
func (Type) IsZero ¶ added in v0.74.3
IsZero reports whether this Type represents a zero value. For internal use.
func (Type) MarshalJSON ¶
MarshalJSON returns the JSON encoding of m. For internal use.
type Types ¶
type Types []Type
Types is a slice of media types. <docsmeta>{ "name": "MediaTypes" }</docsmeta>
var DefaultTypes Types
DefaultTypes is the default media types supported by Hugo.
func (Types) GetByMainSubType ¶ added in v0.45.1
GetByMainSubType gets a media type given a main and a sub type e.g. "text" and "plain". It will return false if no format could be found, or if the combination given is ambiguous. The lookup is case insensitive.
func (Types) GetBySuffix ¶
func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool)
GetBySuffix gets a media type given as suffix, e.g. "html". It will return false if no format could be found, or if the suffix given is ambiguous. The lookup is case insensitive.
func (Types) GetFirstBySuffix ¶ added in v0.45.1
func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool)
GetFirstBySuffix will return the first type matching the given suffix.