oss

package
v0.0.0-...-359ef4e Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 8 Imported by: 64

README

Media Library OSS

Use OSS as backend to store medias

Usage

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

type Product struct {
	gorm.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

This section is empty.

Variables

View Source
var (
	// URLTemplate default URL template
	URLTemplate = "/system/{{class}}/{{primary_key}}/{{column}}/{{filename_with_hash}}"
	// Storage the storage used to save medias
	Storage oss.StorageInterface = filesystem.New("public")
)
View Source
var DefaultRetrieveHandler = func(oss OSS, path string) (media.FileInterface, error) {
	result, err := Storage.GetStream(path)
	if f, ok := result.(media.FileInterface); ok {
		return f, err
	}

	if err == nil {
		buf := []byte{}
		if buf, err = ioutil.ReadAll(result); err == nil {
			result := utils.ClosingReadSeeker{bytes.NewReader(buf)}
			result.Seek(0, 0)
			return result, err
		}
	}
	return nil, err
}

DefaultRetrieveHandler used to retrieve file

View Source
var DefaultStoreHandler = func(oss OSS, 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 DefaultURLTemplateHandler = func(oss OSS, option *media.Option) (url string) {
	if url = option.Get("URL"); url == "" {
		url = URLTemplate
	}

	url = strings.Join([]string{strings.TrimSuffix(Storage.GetEndpoint(), "/"), strings.TrimPrefix(url, "/")}, "/")
	if strings.HasPrefix(url, "/") {
		return url
	}

	for _, prefix := range []string{"https://", "http://"} {
		url = strings.TrimPrefix(url, prefix)
	}

	return "//" + url
}

DefaultURLTemplateHandler used to generate URL and save into database

Functions

This section is empty.

Types

type OSS

type OSS struct {
	media.Base
}

OSS common storage interface

func (OSS) GetURLTemplate

func (o OSS) GetURLTemplate(option *media.Option) (url string)

GetURLTemplate URL's template

func (OSS) Retrieve

func (o OSS) Retrieve(path string) (media.FileInterface, error)

Retrieve retrieve file content with url

func (OSS) Store

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

Store save reader's content with path

func (OSS) String

func (o OSS) String() string

func (OSS) URL

func (o OSS) URL(styles ...string) string

URL return file's url with given style

Jump to

Keyboard shortcuts

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