Documentation
¶
Index ¶
- Constants
- Variables
- func ByteCountSI(b int) string
- func DeniedExtensions() []string
- func FileExtension(name string) string
- func GetImageFormat(url string) (*imaging.Format, error)
- func HasKnownMIMEType(ext string) bool
- func IsDeniedExtension(ext string) bool
- func IsImageContent(head []byte) bool
- func IsImageFormat(name string) bool
- func IsMarkupContent(head []byte) bool
- func IsSVGFormat(name string) bool
- func IsSafeUploadFileName(name string) bool
- func IsVideoContent(head []byte) bool
- func IsVideoFormat(name string) bool
- func NormalizeExtension(ext string) string
- func ReadHead(r io.Reader) ([]byte, error)
- func RegisterMediaHandler(name string, handler MediaHandler)
- func SaleUpDown(width, height int, size *Size)
- func SaveUploadAndCropImage(db *gorm.DB, obj interface{}, _ string, _ *web.EventContext) (err error)
- func SetFileSizes(media Media, fileSizes map[string]int)
- func SetWeightHeight(media Media, width, height int)
- func StartsWithMarkup(head []byte) bool
- func URLDirUUID(url string) string
- type Base
- func (b *Base) Cropped(values ...bool) (result bool)
- func (b *Base) Ext() string
- func (b *Base) GetCropOption(name string) *image.Rectangle
- func (b *Base) GetFileHeader() FileHeader
- func (b *Base) GetFileName() string
- func (b *Base) GetFileSizes() map[string]int
- func (*Base) GetSizes() map[string]*Size
- func (b *Base) GetURL(option *Option, db *gorm.DB, field *schema.Field, templater URLTemplater) string
- func (*Base) GetURLTemplate(option *Option) (path string)
- func (b *Base) IsImage() bool
- func (b *Base) IsSVG() bool
- func (b *Base) IsVideo() bool
- func (b *Base) NeedCrop() bool
- func (*Base) Retrieve(url string) (*os.File, error)
- func (b *Base) Scan(data interface{}) (err error)
- func (b *Base) String() string
- func (b *Base) URL(styles ...string) string
- func (b *Base) URLNoCached(styles ...string) string
- func (b Base) Value() (driver.Value, error)
- type CropOption
- type FileHeader
- type FileInterface
- type Media
- type MediaHandler
- type MemoryFile
- type Option
- type Size
- type URLTemplater
Constants ¶
const SniffLen = 512
SniffLen is the number of leading bytes ReadHead returns, the most http.DetectContentType considers.
Variables ¶
var ( DefaultSizeKey = "default" OriginalSizeKey = "original" )
var MediaLibraryURL = ""
set MediaLibraryURL to change the default url /system/{{class}}/{{primary_key}}/{{column}}.{{extension}}
Functions ¶
func ByteCountSI ¶
func DeniedExtensions ¶ added in v3.4.1
func DeniedExtensions() []string
DeniedExtensions returns the static list of extensions rejected on upload by default. IsDeniedExtension additionally denies any extension whose MIME type (mime.TypeByExtension) is HTML, XML, SVG or JavaScript.
func FileExtension ¶ added in v3.4.1
FileExtension returns the normalized extension of a file name. Trailing dots and spaces are ignored, so "a.svg." and "a.svg " both yield ".svg" instead of slipping past an extension check (uploads with such names are rejected anyway, see IsSafeUploadFileName).
func HasKnownMIMEType ¶ added in v3.4.1
HasKnownMIMEType reports whether a server would serve ext with an explicit MIME type (mime.TypeByExtension), rather than sniffing the content.
func IsDeniedExtension ¶ added in v3.4.1
IsDeniedExtension reports whether files with ext are rejected on upload by default: ext is in the static denylist, or the MIME type a server would serve it with is HTML, XML, SVG or JavaScript.
func IsImageContent ¶ added in v3.4.1
IsImageContent reports whether head is the start of a raster image of a format the media library treats as an image (see IsImageFormat).
func IsImageFormat ¶
IsImageFormat check filename is image or not
func IsMarkupContent ¶ added in v3.4.1
IsMarkupContent reports whether head (the leading bytes of a file) looks like HTML, XML or SVG — content a browser may render as a script-capable document.
func IsSVGFormat ¶
func IsSafeUploadFileName ¶ added in v3.4.1
IsSafeUploadFileName reports whether name is acceptable as an upload file name: non-empty, without control characters, and not ending in a dot or whitespace — trailing characters that some storages and servers strip, which would make the served extension differ from the checked one.
func IsVideoContent ¶ added in v3.4.1
IsVideoContent reports whether head is plausibly the start of a video file. http.DetectContentType recognizes only a few containers (mp4, webm, avi, ogg, mpeg); others such as QuickTime .mov come back as application/octet-stream, which is accepted so real videos are not rejected. Text content — including markup — is never accepted.
func IsVideoFormat ¶
IsVideoFormat check filename is video or not
func NormalizeExtension ¶ added in v3.4.1
NormalizeExtension lowercases ext and makes sure it has a leading dot, so "SVG", "svg" and ".svg" all compare equal. Blank input yields "".
func ReadHead ¶ added in v3.4.1
ReadHead reads up to SniffLen leading bytes from r for content sniffing.
func RegisterMediaHandler ¶
func RegisterMediaHandler(name string, handler MediaHandler)
RegisterMediaHandler register Media library handler
func SaleUpDown ¶ added in v3.3.0
func SaveUploadAndCropImage ¶
func SetFileSizes ¶
func SetWeightHeight ¶
func StartsWithMarkup ¶ added in v3.4.1
StartsWithMarkup reports whether the first significant byte of head — after any byte order mark, whitespace and NUL padding (UTF-16) — is '<'. That is the structural signature of HTML/XML/SVG, whatever the elements are named or namespaced, and it is what browsers key on when they sniff an untyped response.
func URLDirUUID ¶ added in v3.4.1
URLDirUUID returns the first directory segment of url that is a UUID (as rendered by the {{uuid}} template placeholder), or "".
Types ¶
type Base ¶
type Base struct {
FileName string
Url string
CropOptions map[string]*CropOption `json:"-"`
Delete bool `json:"-"`
Crop bool `json:"-"`
FileHeader FileHeader `json:"-"`
Reader io.Reader `json:"-"`
Options map[string]string `json:",omitempty"`
Width int `json:",omitempty"`
Height int `json:",omitempty"`
FileSizes map[string]int `json:",omitempty"`
// contains filtered or unexported fields
}
Base defined a base struct for storages
func (*Base) GetCropOption ¶
GetCropOption get crop options
func (*Base) GetFileHeader ¶
func (b *Base) GetFileHeader() FileHeader
GetFileHeader get file's header, this value only exists when saving files
func (*Base) GetFileSizes ¶
GetFileSizes get file sizes
func (*Base) GetURL ¶
func (b *Base) GetURL(option *Option, db *gorm.DB, field *schema.Field, templater URLTemplater) string
GetURL get default URL for a model based on its options
func (*Base) GetURLTemplate ¶
GetURLTemplate get url template
func (*Base) URLNoCached ¶ added in v3.0.4
type CropOption ¶
type CropOption struct {
X, Y, Width, Height int
}
CropOption includes crop options
type FileHeader ¶
FileHeader is an interface, for matched values, when call its `Open` method will return `multipart.File`
type FileInterface ¶
type FileInterface interface {
io.ReadSeeker
io.Closer
}
FileInterface media file interface
type Media ¶
type Media interface {
Scan(value interface{}) error
Value() (driver.Value, error)
GetURLTemplate(*Option) string
GetURL(option *Option, db *gorm.DB, field *schema.Field, templater URLTemplater) string
GetFileHeader() FileHeader
GetFileName() string
GetSizes() map[string]*Size
NeedCrop() bool
Cropped(values ...bool) bool
GetCropOption(name string) *image.Rectangle
GetFileSizes() map[string]int
Store(url string, option *Option, reader io.Reader) error
Retrieve(url string) (FileInterface, error)
IsImage() bool
URL(style ...string) string
Ext() string
String() string
}
Media is an interface including methods that needs for a media library storage
type MediaHandler ¶
type MediaHandler interface {
CouldHandle(media Media) bool
Handle(media Media, file FileInterface, option *Option) error
}
MediaHandler media library handler interface, defined which files could be handled, and the handler
type MemoryFile ¶ added in v3.0.3
type MemoryFile struct {
// contains filtered or unexported fields
}
func NewMemoryFile ¶ added in v3.0.3
func NewMemoryFile(filename string, data []byte) *MemoryFile
func (*MemoryFile) Close ¶ added in v3.0.3
func (*MemoryFile) Close() error
type Size ¶
Size is a struct, used for `GetSizes` method, it will return a slice of Size, media library will crop images automatically based on it
type URLTemplater ¶
URLTemplater is a interface to return url template