oss

package
v0.0.0-...-a990035 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 36 Imported by: 2

README

Media Library OSS

Use OSS as backend to store medias

Usage

import (
	"github.com/ecletus/media/oss"
	"github.com/ecletus/oss/filesystem"
	"github.com/ecletus/oss/s3"
	awss3 "github.com/aws/aws-sdk-go/service/s3"
)

type Product struct {
	aorm.Model
	Image oss.OSS
}

func init() {
  // OSS's default storage is directory `public`, change it to S3
	oss.Storage = s3.New(&s3.Config{AccessID: "access_id", AccessKey: "access_key", Region: "region", Bucket: "bucket", Endpoint: "cdn.getqor.com", ACL: awss3.BucketCannedACLPublicRead})

  // or change directory to `download`
	oss.Storage = filesystem.New("download")
}

Advanced Usage

// change URL template
oss.URLTemplate = "/system/{{class}}/{{primary_key}}/{{column}}/{{filename_with_hash}}"

// change default URL handler
oss.DefaultURLTemplateHandler = func(option *media_library.Option) (url string) {
  // ...
}

// change default save handler
oss.DefaultStoreHandler = func(path string, option *media_library.Option, reader io.Reader) error {
  // ...
}

// change default retrieve handler
oss.DefaultRetrieveHandler = func(path string) (*os.File, error) {
	// ...
}

// By overwritting default store, retrieve handler, you could do some advanced tasks, like use private mode when store sensitive data to S3, public read mode for other files

License

Released under the MIT License.

Documentation

Index

Constants

View Source
const (
	IMAGE_STYLE_PREVIEW = "@qor_preview"
	IMAGE_STYLE_ORIGNAL = "original"
)

Variables

View Source
var (
	// Storage the storage used to save medias
	FileSystemStorage = filesystem.New(&filesystem.Config{RootDir: "./data"})
	PKG               = path_helpers.GetCalledDir()
)
View Source
var DB_CALLBACK_IGNORE = PKG + ".callback.ignore"
View Source
var DefaultRemoveHandler = func(storage oss.StorageInterface, path string, option *media.Option) error {
	return storage.Delete(path)
}

DefaultRemoveHandler used to store reader with default Storage

View Source
var DefaultRetrieveHandler = func(storage oss.StorageInterface, option *media.Option, path string) (*os.File, error) {
	return storage.Get(path)
}

DefaultRetrieveHandler used to retrieve file

View Source
var DefaultStoreHandler = func(storage oss.StorageInterface, path string, option *media.Option, reader io.Reader) error {
	_, err := storage.Put(path, reader)
	return err
}

DefaultStoreHandler used to store reader with default Storage

View Source
var E_DEFAULT_IMAGE_URL = PKG + ".defaultImageURL"
View Source
var E_SAVE_AND_CROP = PKG + ":save_and_crop"
View Source
var KEY_ICON_STYLE = PKG + ".iconStyle"
View Source
var META_PREVIEW = PKG + ".preview"

Functions

func GetImageStyle

func GetImageStyle(ctx *core.Context) string

func IgnoreCallback

func IgnoreCallback(db *aorm.DB) *aorm.DB

func ImageMetaGetDefaultURL

func ImageMetaGetDefaultURL(meta *admin.Meta, recorde interface{}, ctx *core.Context) (url string)

func ImageMetaOnDefaultValue

func ImageMetaOnDefaultValue(meta *admin.Meta, cb func(e *admin.MetaValuerEvent))

func ImageMetaURL

func ImageMetaURL(meta *admin.Meta, key, defaultStyle string) *admin.Meta

func IsIgnoreCallback

func IsIgnoreCallback(v interface{}) bool

func RegisterCallbacks

func RegisterCallbacks(db *aorm.DB)

RegisterCallbacks register callback into GORM DB

Types

type CropOption

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

CropOption includes crop options

func (CropOption) Rectangle

func (cropOption CropOption) Rectangle() *image.Rectangle

type CropperOption

type CropperOption struct {
	Size *Size
	Crop *CropOption
}

type Doc

type Doc struct {
	OSS
}

func (*Doc) ConfigureQorMetaBeforeInitialize

func (d *Doc) ConfigureQorMetaBeforeInitialize(metaor resource.Metaor)

func (*Doc) ContextScan

func (d *Doc) ContextScan(ctx *core.Context, data interface{}) (err error)

func (*Doc) FileExts

func (*Doc) FileExts() []string

type Image

type Image struct {
	OSS
	CropOptions map[string]*CropOption `json:",omitempty"`
	Crop        bool                   `json:"-"`

	Sizes        map[string]*Size `json:",omitempty"`
	OriginalSize Size
	// contains filtered or unexported fields
}

func (*Image) ConfigureQorMetaBeforeInitialize

func (img *Image) ConfigureQorMetaBeforeInitialize(meta resource.Metaor)

func (*Image) ContextScan

func (img *Image) ContextScan(ctx *core.Context, data interface{}) (err error)

func (Image) Cropable

func (img Image) Cropable() bool

func (*Image) Cropped

func (img *Image) Cropped(values ...bool) (result bool)

Cropped mark the image to be cropped

func (Image) Export

func (img Image) Export(ctx *core.Context) (string, error)

func (Image) FileExts

func (Image) FileExts() []string

func (Image) FileTypes

func (Image) FileTypes() []string

func (*Image) GetCropOption

func (img *Image) GetCropOption(name string) *CropOption

GetCropOption get crop options

func (*Image) GetCropOptions

func (img *Image) GetCropOptions() map[string]*CropOption

func (*Image) GetOriginalSize

func (img *Image) GetOriginalSize() *Size

func (Image) GetSizes

func (img Image) GetSizes() map[string]*Size

func (Image) HasImage

func (img Image) HasImage() bool

func (*Image) Init

func (img *Image) Init(site *core.Site, field *aorm.Field)

func (Image) MaxSize

func (Image) MaxSize() uint64

func (*Image) MediaScan

func (img *Image) MediaScan(ctx *media.Context, data interface{}) (err error)

func (*Image) NeedCrop

func (img *Image) NeedCrop() bool

NeedCrop return the file needs to be cropped or not

func (Image) OriginalSizeDefined

func (img Image) OriginalSizeDefined() bool

func (*Image) Scan

func (img *Image) Scan(data interface{}) (err error)

func (*Image) ScanBytes

func (img *Image) ScanBytes(ctx *media.Context, data []byte) (err error)

func (*Image) Set

func (img *Image) Set(ctx *media.Context, data interface{}) (err error)

func (Image) SystemNames

func (img Image) SystemNames() []string

func (Image) Value

func (img Image) Value() (driver.Value, error)

type ImageCropper

type ImageCropper struct {
	Image  ImageInterface
	Img    image.Image
	Format imaging.Format
	// contains filtered or unexported fields
}

func NewImageCropper

func NewImageCropper(img ImageInterface, file io.ReadSeeker) (cropper *ImageCropper, err error)

func (*ImageCropper) Crop

func (cropper *ImageCropper) Crop(options map[string]*CropperOption, cb func(key string, f *bytes.Buffer) error) (err error)

func (*ImageCropper) CropNames

func (cropper *ImageCropper) CropNames(cb func(key string, f *bytes.Buffer) error, names ...string) (err error)

func (*ImageCropper) Height

func (cropper *ImageCropper) Height() (w int)

func (*ImageCropper) Size

func (cropper *ImageCropper) Size() (w int, h int)

func (*ImageCropper) Width

func (cropper *ImageCropper) Width() (w int)

type ImageInterface

type ImageInterface interface {
	OSSInterface
	GetSizes() map[string]*Size

	NeedCrop() bool
	Cropped(values ...bool) bool
	GetCropOption(name string) *CropOption
	GetCropOptions() map[string]*CropOption
	GetOriginalSize() *Size
	OriginalSizeDefined() bool
	Cropable() bool
}

type ImageMeta

type ImageMeta struct {
	FormattedStyle string
	URLFunc        func(meta *admin.Meta, ctx *core.Context, record interface{}, value *Image) string
	SvgDisabled    bool
	// contains filtered or unexported fields
}

func (*ImageMeta) ConfigureQorMeta

func (this *ImageMeta) ConfigureQorMeta(metaor resource.Metaor)

func (*ImageMeta) DefaultURL

func (this *ImageMeta) DefaultURL(ctx *core.Context, record interface{}, value *Image) string

func (*ImageMeta) GetURL

func (this *ImageMeta) GetURL(ctx *core.Context, record interface{}, value *Image) (url string)

func (*ImageMeta) StyleURL

func (this *ImageMeta) StyleURL(ctx *core.Context, record interface{}, style ...string) (url string)
type ImageOrLink struct {
	MediaType MediaType `sql:"type:varchar(24)"`
	ImageFile Image
	ImageWithLink
}

func (*ImageOrLink) HasImageFile

func (i *ImageOrLink) HasImageFile() bool

func (*ImageOrLink) ImageURLProvider

func (i *ImageOrLink) ImageURLProvider() reader_provider.MediaReaderProvider

func (*ImageOrLink) IsImageFile

func (i *ImageOrLink) IsImageFile(has ...bool) bool
func (i *ImageOrLink) IsImageLink(has ...bool) bool
type ImageOrLinkOrVideoLink struct {
	ImageOrLink
	VideoLink string
}
func (iv *ImageOrLinkOrVideoLink) HasVideoLink() bool
func (iv *ImageOrLinkOrVideoLink) IsVideoLink(has ...bool) bool

func (*ImageOrLinkOrVideoLink) VideoLinkProvider

type ImageOrVideoType

type ImageOrVideoType string
type ImageOrVideoWithLink struct {
	ImageOrLinkOrVideoLink
	VideoFile Video
}

func (*ImageOrVideoWithLink) HasVideoFile

func (iv *ImageOrVideoWithLink) HasVideoFile() bool

func (*ImageOrVideoWithLink) IsVideoFile

func (iv *ImageOrVideoWithLink) IsVideoFile() bool
type ImageWithLink struct {
	ImageLink string
}
func (i *ImageWithLink) HasImageLink() bool

type MediaType

type MediaType string
const (
	MEDIA_IMAGE_FILE MediaType = "image_file"
	MEDIA_IMAGE_LINK MediaType = "image_link"
)
const (
	MEDIA_VIDEO_FILE MediaType = "video_file"
	MEDIA_VIDEO_LINK MediaType = "video_link"
)

type OSS

type OSS struct {
	media.Base
	// contains filtered or unexported fields
}

OSS common storage interface

func (*OSS) ContextScan

func (o *OSS) ContextScan(ctx *core.Context, data interface{}) (err error)

func (*OSS) Init

func (o *OSS) Init(site *core.Site, field *aorm.Field)

func (OSS) IsNew

func (o OSS) IsNew() bool

func (*OSS) MediaScan

func (o *OSS) MediaScan(ctx *media.Context, data interface{}) (err error)

func (*OSS) Remove

func (o *OSS) Remove(path string) (found bool, err error)

Remove content by path

func (*OSS) Retrieve

func (o *OSS) Retrieve(path string) (*os.File, error)

Retrieve retrieve file content with url

func (*OSS) Scan

func (o *OSS) Scan(data interface{}) (err error)

func (*OSS) Set

func (o *OSS) Set(ctx *media.Context, data interface{}) (err error)

func (*OSS) Store

func (o *OSS) Store(path string, reader io.Reader) error

Store save reader's content with path

type OSSInterface

type OSSInterface interface {
	media.Media
	IsNew() bool
}

type Plugin

type Plugin struct {
	db.DBNames
	plug.EventDispatcher
	SetupConfigKey string
}

func (*Plugin) After

func (p *Plugin) After() []interface{}

func (*Plugin) Init

func (p *Plugin) Init(options *plug.Options) error

func (*Plugin) OnRegister

func (p *Plugin) OnRegister()

func (*Plugin) RequireOptions

func (p *Plugin) RequireOptions() []string

type Size

type Size struct {
	Width  int
	Height int
}

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 Video

type Video struct {
	OSS
}

func (Video) FileExts

func (Video) FileExts() []string

Jump to

Keyboard shortcuts

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