mime

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 6 Imported by: 7

README

godoc codecov Go Report Card

mime - mime type system utilities

A collection of utilities for working with mime type systems.

Installation

> go get github.com/go-corelibs/mime@latest

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package mime provides mime type system utilities

Index

Constants

View Source
const (
	TextMimeType       = "text/plain"
	HtmlMimeType       = "text/html"
	CssMimeType        = "text/css"
	ScssMimeType       = "text/x-scss"
	JsonMimeType       = "application/json"
	JavaScriptMimeType = "text/javascript"
	BinaryMimeType     = "application/octet-stream"

	// DirectoryMimeType defines the mime type used for filesystem directories
	DirectoryMimeType = "inode/directory"
	// EnjinMimeType defines the mime type for the Go-Enjin project's `njn`
	// page format
	EnjinMimeType = "text/enjin"
	// OrgModeMimeType defines the mime type used by the Go-Enjin project to
	// identify the `org-mode` page format
	OrgModeMimeType = "text/org-mode"
	// MarkdownMimeType defines the mime type used by the Go-Enjin project to
	// identify the `markdown` page format
	MarkdownMimeType = "text/markdown"
)
View Source
const (
	// EnjinExtension defines the file extension associated with the
	// EnjinMimeType
	EnjinExtension = "njn"
	// OrgModeExtension defines the file extension associated with the
	// OrgModeMimeType
	OrgModeExtension = "org"
	// MarkdownExtension defines the file extension associated with the
	// MarkdownMimeType
	MarkdownExtension = "md"
)

Variables

This section is empty.

Functions

func FromPathOnly

func FromPathOnly(path string) (mime string)

FromPathOnly checks the given `path` for any extensions using github.com/go-corelibs/path.ExtExt and uses GetExtension with any extensions found

func GetCharset

func GetCharset(mime string) (charset string, ok bool)

GetCharset returns the `charset` internally associated with this package

func GetExtension

func GetExtension(extension string) (mime string, ok bool)

GetExtension returns the mime type internally associated with this package using SetExtension, or if not present uses mime.TypeByExtension to lookup further

func IsPlainText

func IsPlainText(mime string) (yes bool)

IsPlainText returns true if the given `mime` is of `text/plain` type. IsPlainText checks if it has an internally registered charset first and if so, returns true early. If the `mime` is not internally registered with a charset (via SetCharset), IsPlainText uses github.com/gabriel-vasile/mimetype.Lookup to check if the given `mime` is exactly TextMimeType or if any of the found mime type's parents are TextMimeType

func Mime

func Mime(path string) (mime string)

Mime returns the MIME type string of a local filesystem directory or file. The specific type returned for directories is defined by the DirectoryMimeType constant

func PlainTextDetector

func PlainTextDetector(raw []byte, limit uint32) bool

PlainTextDetector is the default detector used when RegisterTextType is given a `nil` value for it's `detector` argument. PlainTextDetector always returns true

func PruneCharset

func PruneCharset(mime string) (pruned string)

PruneCharset uses mime.ParseMediaType to parse the given mime string and returns only the media type value

func RegisterTextType

func RegisterTextType(mime, extension string, detector func(raw []byte, limit uint32) bool) (err error)

RegisterTextType associates the given `mime` with the given `extension` and if the `detector` is not nil, registers the given `mime` with TextMimeType as it's parent within the github.com/gabriel-vasile/mimetype system

func SetCharset

func SetCharset(mime, charset string)

SetCharset registers the given extension with the given charset string. There can only be one charset associated per extension and SetCharset will overwrite any existing value

func SetExtension

func SetExtension(extension, mime string)

SetExtension registers the given extension with the given mime type string. There can only be one mime type associated per extension and SetExtension will overwrite any existing value. If `mime` is empty, any internal association with the extension is cleared

Types

This section is empty.

Jump to

Keyboard shortcuts

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