Documentation ¶
Index ¶
- Constants
- func ContentTypeByExtension(extension string) (string, error)
- func ContentTypeFromURL(url string) (string, error)
- func DataURIToBytes(dataURI string) ([]byte, error)
- func ExtensionByContentType(contentType string) (string, error)
- func GenerateCacheKey(url string) string
- func IsValidFavicon(url string) bool
- func IsValidFaviconContentType(contentType string) bool
- func IsValidImage(url string) bool
- func IsValidImageContentType(contentType string) bool
- type Cache
- type Media
- func NewAudioVideoFromHTTPResponse(resp *http.Response, mediaURL string, maxSize int) (*Media, error)
- func NewImageFromBytes(data []byte) (*Media, error)
- func NewImageFromDataURI(dataURI string) (*Media, error)
- func NewImageFromHTTPResponse(resp *http.Response, imgURL string, maxSize int) (*Media, error)
- func NewImageFromURL(imgURL string, maxSize int) (*Media, error)
- func NewMediaFromURL(mediaURL string, maxSize int) (*Media, error)
- func NewVectorImageFromHTTPResponse(resp *http.Response, imgURL string, maxSize int) (*Media, error)
- type Metadata
- type Type
Constants ¶
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 ContentTypeFromURL ¶
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 ¶
DataURIToBytes takes a Data URI and returns the byte data it contains.
func ExtensionByContentType ¶
func GenerateCacheKey ¶
GenerateCacheKey generates a cache key for the given URL
func IsValidFavicon ¶
IsValidFavicon checks if the given URL is a valid favicon.
func IsValidImage ¶
IsValidImage checks if the given URL is a valid image.
func IsValidImageContentType ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
GenerateUniqueFilename generates a unique filename based on the Image properties
func (*Media) ShouldCacheImage ¶
ShouldCacheImage takes a http.Header and returns whether the image should be cached
func (*Media) ShouldRefreshImage ¶
ShouldRefreshImage takes a http.Header and returns whether the image should be refreshed
func (*Media) ShouldRevalidateImage ¶
ShouldRevalidateImage takes a http.Header and returns whether the image should be revalidated
type Metadata ¶
func FetchMetadataFromHeader ¶
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.