media

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	DefaultDelimiter = "."
)

Variables

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: "images/svg+xml"},
	TextType:           Type{Type: "text/plain"},
	TOMLType:           Type{Type: "application/toml"},
	YAMLType:           Type{Type: "application/yaml"},

	PNGType:  Type{Type: "images/png"},
	JPEGType: Type{Type: "images/jpeg"},
	GIFType:  Type{Type: "images/gif"},
	TIFFType: Type{Type: "images/tiff"},
	BMPType:  Type{Type: "images/bmp"},
	WEBPType: Type{Type: "images/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"},
}
View Source
var BuiltinCss = []Type{
	Builtin.CSSType,
	Builtin.SCSSType,
	Builtin.SASSType,
}
View Source
var BuiltinImages = []Type{
	Builtin.PNGType,
	Builtin.JPEGType,
	Builtin.GIFType,
	Builtin.TIFFType,
	Builtin.BMPType,
	Builtin.WEBPType,
}
View Source
var BuiltinJs = []Type{
	Builtin.JavascriptType,
}
View Source
var BuiltinJson = []Type{
	Builtin.JSONType,
}

Functions

func InitMediaType

func InitMediaType(m *Type)

Types

type BuiltinTypes

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 images 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 SuffixInfo

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

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

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

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

func (Type) IsText

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

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) Sub added in v0.0.5

func (m Type) Sub() string

func (Type) Suffixes

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>

func (Types) BySuffix

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

BySuffix will return all media types matching a suffix.

func (Types) GetByMainSubType

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

func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool)

GetFirstBySuffix will return the first type matching the given suffix.

func (Types) IsTextSuffix

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