media

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 28 Imported by: 2

README

Media Library

Media is a Golang library that supports the upload of files/images/videos to a filesystem or cloud storage as well as linked videos (i.e. YouTube, Vimeo, etc.). The plugin includes:

  • cropping and resizing features for images.
  • optional multiple sizes for each media resource.
  • Accessibility helpers.
File Types

Media accepts any and every file type, yet it associates certain file types as images or videos so as to provide helpers supporting those media's specific needs.

Images: .jpg, .jpeg, .png, .tif, .tiff, .bmp, .gif

Videos: .mp4, .m4p, .m4v, .m4v, .mov, .mpeg, .webm, .avi, .ogg, .ogv

Usage

Media depends on GORM models as it is using GORM's callbacks to handle file processing, so you will need to register callbacks first:


db, err := gorm.Open(postgres.Open(os.Getenv("DB_PARAMS")), &gorm.Config{})
media.RegisterCallbacks(db)

sess := session.Must(session.NewSession())
oss.Storage = s3.New(&s3.Config{
    Bucket:  os.Getenv("S3_Bucket"),
    Region:  os.Getenv("S3_Region"),
    Session: sess,
})


Use media box in a model

type Product struct {
	gorm.Model
	HeroImage     media_library.MediaBox `sql:"type:text;"`
}

Configure media box


import (
    media_view "github.com/qor/qor5/media/views"
)
b := presets.New()
media_view.Configure(b, db)

p := b.Model(&Product{})
ed := p.Editing( "HeroImage")
ed.Field("HeroImage").WithContextValue(
    media_view.MediaBoxConfig,
    &media_library.MediaBoxConfig{
        AllowType: "image",
        Sizes: map[string]*media.Size{
            "thumb": {
                Width:  400,
                Height: 300,
            },
        },
    })

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSizeKey = "default"
View Source
var (
	// set MediaLibraryURL to change the default url /system/{{class}}/{{primary_key}}/{{column}}.{{extension}}
	MediaLibraryURL = ""
)

Functions

func ByteCountSI

func ByteCountSI(b int) string

func GetImageFormat

func GetImageFormat(url string) (*imaging.Format, error)

func IsImageFormat

func IsImageFormat(name string) bool

IsImageFormat check filename is image or not

func IsSVGFormat

func IsSVGFormat(name string) bool

func IsVideoFormat

func IsVideoFormat(name string) bool

IsVideoFormat check filename is video or not

func RegisterMediaHandler

func RegisterMediaHandler(name string, handler MediaHandler)

RegisterMediaHandler register Media library handler

func SaveUploadAndCropImage

func SaveUploadAndCropImage(db *gorm.DB, obj interface{}) (err error)

func SetFileSizes

func SetFileSizes(media Media, fileSizes map[string]int)

func SetWeightHeight

func SetWeightHeight(media Media, width, height int)

Types

type Base

type Base struct {
	FileName    string
	Url         string
	CropOptions map[string]*CropOption `json:",omitempty"`
	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) Cropped

func (b *Base) Cropped(values ...bool) (result bool)

Cropped mark the image to be cropped

func (Base) Ext

func (b Base) Ext() string

func (*Base) GetCropOption

func (b *Base) GetCropOption(name string) *image.Rectangle

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) GetFileName

func (b Base) GetFileName() string

GetFileName get file's name

func (*Base) GetFileSizes

func (b *Base) GetFileSizes() map[string]int

GetFileSizes get file sizes

func (Base) GetSizes

func (b Base) GetSizes() map[string]*Size

GetSizes get configured sizes, it will be used to crop images accordingly

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

func (b Base) GetURLTemplate(option *Option) (path string)

GetURLTemplate get url template

func (Base) IsImage

func (b Base) IsImage() bool

IsImage return if it is an image

func (Base) IsSVG

func (b Base) IsSVG() bool

func (Base) IsVideo

func (b Base) IsVideo() bool

func (*Base) NeedCrop

func (b *Base) NeedCrop() bool

NeedCrop return the file needs to be cropped or not

func (Base) Retrieve

func (b Base) Retrieve(url string) (*os.File, error)

Retrieve retrieve file content with url

func (*Base) Scan

func (b *Base) Scan(data interface{}) (err error)

Scan scan files, crop options, db values into struct

func (Base) String

func (b Base) String() string

String return file's url

func (Base) URL

func (b Base) URL(styles ...string) string

URL return file's url with given style

func (Base) Value

func (b Base) Value() (driver.Value, error)

Value return struct's Value

type CropOption

type CropOption struct {
	X, Y, Width, Height int
}

CropOption includes crop options

type FileHeader

type FileHeader interface {
	Open() (multipart.File, error)
}

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 Option

type Option map[string]string

Option media library option

func (Option) Get

func (option Option) Get(key string) string

get option with name

func (Option) Set

func (option Option) Set(key string, val string)

set option

type Size

type Size struct {
	Width   int
	Height  int
	Padding bool
}

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

type URLTemplater interface {
	GetURLTemplate(*Option) string
}

URLTemplater is a interface to return url template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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