v0.112.0 Latest Latest

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

Go to latest
Published: May 23, 2023 License: Apache-2.0 Imports: 12 Imported by: 160



Package media contains Media Type (MIME type) related types and functions.



View Source
const (
	DefaultDelimiter = "."


View Source
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"},


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)


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

func FromContent(types Types, extensionHints []string, content []byte) Type

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

func FromString(t string) (Type, error)

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

func FromStringAndExt(t, ext string) (Type, error)

FromStringAndExt creates a Type from a MIME string and a given extension.

func MustFromString added in v0.112.0

func MustFromString(t string) Type

MustFromString is like FromString but panics on error.

func WithDelimiterAndSuffixes added in v0.82.0

func WithDelimiterAndSuffixes(t Type, delimiter, suffixesCSV string) Type

WithDelimiterAndSuffixes is used in tests.

func (Type) IsText added in v0.91.0

func (m Type) IsText() bool

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

func (m Type) IsZero() bool

IsZero reports whether this Type represents a zero value. For internal use.

func (Type) MarshalJSON

func (m Type) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of m. For internal use.

func (Type) String

func (m Type) String() string

For internal use.

func (Type) Suffixes added in v0.45.1

func (m Type) Suffixes() []string

Suffixes returns all valid file suffixes for this type.

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) BySuffix added in v0.49.1

func (t Types) BySuffix(suffix string) []Type

BySuffix will return all media types matching a suffix.

func (Types) GetByMainSubType added in v0.45.1

func (t Types) GetByMainSubType(mainType, subType string) (tp Type, found bool)

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

func (t Types) GetByType(tp string) (Type, bool)

GetByType returns a media type for tp.

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.

func (Types) IsTextSuffix added in v0.112.0

func (t Types) IsTextSuffix(suffix string) bool

func (Types) Len

func (t Types) Len() int

func (Types) Less

func (t Types) Less(i, j int) bool

func (Types) Swap

func (t Types) Swap(i, j int)

Jump to

Keyboard shortcuts

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