media

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxMediaSize = 30 * 1024 * 1024

DefaultMaxMediaSize represents the default maximum number of bytes we are willing to download for an image. (10 MB)

Variables

This section is empty.

Functions

func ContentTypeByExtension

func ContentTypeByExtension(extension string) (string, error)

func ContentTypeFromURL

func ContentTypeFromURL(url string) (string, error)

ContentTypeFromURL returns the Content-Type header from the given URL. However, it does not download the image. It will attempt to get the Content-Type header by making a HEAD request to the URL. If the Content-Type header cannot be extracted using a HEAD request, it will attempt to get it by making a GET request to the URL and reviewing the first 512 KB. An error is returned if the Content-Type header still cannot be extracted.

func DataURIToBytes

func DataURIToBytes(dataURI string) ([]byte, error)

DataURIToBytes takes a Data URI and returns the byte data it contains.

func ExtensionByContentType

func ExtensionByContentType(contentType string) (string, error)

func GenerateCacheKey

func GenerateCacheKey(url string) string

GenerateCacheKey generates a cache key for the given URL

func IsValidFavicon

func IsValidFavicon(url string) bool

IsValidFavicon checks if the given URL is a valid favicon.

func IsValidFaviconContentType

func IsValidFaviconContentType(contentType string) bool

func IsValidImage

func IsValidImage(url string) bool

IsValidImage checks if the given URL is a valid image.

func IsValidImageContentType

func IsValidImageContentType(contentType string) bool

Types

type Cache

type Cache struct {
	Available      bool
	MaxAge         int
	Expires        time.Time
	NoCache        bool
	NoStore        bool
	MustRevalidate bool
}

Cache holds the information extracted from Cache-Control and Expires headers

func ParseCacheHeader

func ParseCacheHeader(header http.Header) Cache

ParseCacheHeader takes a http.Header and returns the parsed Cache

type Media

type Media struct {
	Metadata
	Bytes       []byte
	Cache       Cache
	ContentSize int64
	Format      string
	Image       image.Image // To hold the image.Image object if it is an image
	URL         string
	Extension   string
	MediaType   Type
}

func NewAudioVideoFromHTTPResponse

func NewAudioVideoFromHTTPResponse(resp *http.Response, mediaURL string, maxSize int) (*Media, error)

NewAudioVideoFromHTTPResponse retrieves an audio or video file from the given URL and returns it as raw bytes

func NewImageFromBytes

func NewImageFromBytes(data []byte) (*Media, error)

NewImageFromBytes returns the image and metadata from the given bytes. It will attempt to get the ContentType, Width, Height, Format, and ContentSize from the given bytes. If the metadata cannot be extracted, an error is returned.

func NewImageFromDataURI

func NewImageFromDataURI(dataURI string) (*Media, error)

NewImageFromDataURI will parse the data URI and return the image and metadata. It will attempt to get the ContentType, Width, Height, Format, and ContentSize from the data URI as well.

func NewImageFromHTTPResponse

func NewImageFromHTTPResponse(resp *http.Response, imgURL string, maxSize int) (*Media, error)

NewImageFromHTTPResponse will download the image from the given URL and return the image and metadata, but only if it is within the MaxImageSize.

func NewImageFromURL

func NewImageFromURL(imgURL string, maxSize int) (*Media, error)

NewImageFromURL will download the image from the given URL and return the image and metadata, but only if it is within the MaxImageSize.

func NewMediaFromURL

func NewMediaFromURL(mediaURL string, maxSize int) (*Media, error)

NewMediaFromURL will download the media from the given URL and return the media and metadata, but only if it is within the MaxMediaSize.

func NewVectorImageFromHTTPResponse

func NewVectorImageFromHTTPResponse(resp *http.Response, imgURL string, maxSize int) (*Media, error)

NewVectorImageFromHTTPResponse retrieves an SVG or ICO image from the given URL and returns it as raw bytes

func (*Media) GenerateUniqueFilename

func (i *Media) GenerateUniqueFilename() string

GenerateUniqueFilename generates a unique filename based on the Image properties

func (*Media) ShouldCacheImage

func (i *Media) ShouldCacheImage() bool

ShouldCacheImage takes a http.Header and returns whether the image should be cached

func (*Media) ShouldRefreshImage

func (i *Media) ShouldRefreshImage() bool

ShouldRefreshImage takes a http.Header and returns whether the image should be refreshed

func (*Media) ShouldRevalidateImage

func (i *Media) ShouldRevalidateImage() bool

ShouldRevalidateImage takes a http.Header and returns whether the image should be revalidated

type Metadata

type Metadata struct {
	Width       int
	Height      int
	ContentSize int64
	ContentType string
}

func FetchMetadataFromHeader

func FetchMetadataFromHeader(imgURL string, maxBytes int) (Metadata, error)

FetchMetadataFromHeader returns the image metadata from the given URL. However, it does not download the image. It will attempt to get the ContentType, Width, and Height from downloading at most maxBytes. If the metadata cannot be extracted within the first maxBytes, an error is returned. This is useful for validating images without downloading the entire image.

type Type

type Type int
const (
	ImageType Type = iota
	VideoType
	AudioType
	VectorImageType // For SVGs, ICOs, etc.
)

Jump to

Keyboard shortcuts

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