store

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: MIT Imports: 10 Imported by: 1

README

Store 文件储存接口

定义了用于网站上传下载的文件传输接口,以及本地仓库的实现

可用驱动

  • assest 本地文件仓库

配置说明

#TOML版本,其他版本可以根据对应格式配置
#驱动名,具体值取决于需要使用的驱动
Driver="assest"
#驱动配置部分
[Config]

本地文件仓库驱动配置

#TOML版本,其他版本可以根据对应格式配置
#驱动名
Driver="assest"
#驱动配置部分
[Config
#前台路径访问域名部分
URLHost="http://www.test.com"
#前台路径目录部分
URLPrefix="/upload"
#本地文件根目录
Root="/tmp"
#路径为绝对路径还是相对路径
Absolute=true
#本地文件子目录
Location="/savedfiles"

使用方法

创建Store
s:=store.New()
config=&store.ConfigMap{}
err=toml.Unmarshal(data,config)
err=config.ApplyTo(store)
上传和载入文件
//上传,传入文件名和reader
//reader会被驱动关闭
//返回文件id,长度和错误

id, length, err := s.Save("filename",reader)

//下载,传入文件id,返回reader和错误
//用户需要自行关闭reader
reader, err := s.Load(id)
defer reader.Close()

//删除文件
err = s.Remove(id)
//通过id换取文件url
url, err = s.URL(f.ID)
创建和使用File对象
//根据id创建file对象
f:=s.File(id)

//获取file对象的url
url, err =f.URL()
错误列表

文件错误(*store.ErrorType)是一类文件相关的错误

可以通过把错误换转为文件错误来获取响应的文件信息

if storeerr=err.(*store.ErrorType){
    fmt.Println("文件名",storeerr.File)
    fmt.Println("错误类型",storeerr.Type)
    
}

文件错误类型列表为

  • store.ErrorTypeNotExists 文件不存在
  • store.ErrorTypeUnavailableID 文件ID无效

Documentation

Index

Constants

View Source
const ErrorTypeNotExists = "file-not-exists"

ErrorTypeNotExists error type file not exists.

View Source
const ErrorTypeUnavailableID = "unavailable-id"

ErrorTypeUnavailableID error type id unavailable

Variables

This section is empty.

Functions

func Factories

func Factories() []string

Factories returns a sorted list of the names of the registered factories.

func Register

func Register(name string, f Factory)

Register makes a driver creator available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

func RegisterAssets

func RegisterAssets()

RegisterAssets register assets driver .

func UnregisterAll

func UnregisterAll()

UnregisterAll unregister all driver

Types

type Assets

type Assets struct {
	URLPrefix string
	Location  string
}

Assets local file store.

func (*Assets) Load

func (f *Assets) Load(id string) (io.ReadCloser, error)

Load load file with given id. Return file reader any error if raised.

func (*Assets) Remove

func (f *Assets) Remove(id string) error

Remove remove file by id. Return any error if raised.

func (*Assets) Save

func (f *Assets) Save(filename string, reader io.Reader) (string, int64, error)

Save save data form reader to named file. Return file id ,file size and any error if raised.

func (*Assets) URL

func (f *Assets) URL(id string) (string, error)

URL convert file id to file url. Return file url and any error if raised.

type AssetsStoreConfig

type AssetsStoreConfig struct {
	//URLHost file url host.
	URLHost string
	//URLPrefix file url path prefix
	URLPrefix string
	//Absolute if filepath is Absolute.
	Absolute bool
	//Root file root path.
	//if Absolute is true,file root is based from root path.
	//Otherwie file root is based from current working direction.
	Root string
	//Location file sub  folder which stored in.
	Location string
}

AssetsStoreConfig local file store config

func (*AssetsStoreConfig) Create

func (c *AssetsStoreConfig) Create() (Driver, error)

Create create new local file driver. Return created driver and any error if raised.

type Config

type Config interface {
	//Get get value form given key.
	//Return any error if raised.
	Get(key string, v interface{}) error
}

Config confit interface

type ConfigJSON

type ConfigJSON map[string]string

ConfigJSON JSON format config

func (*ConfigJSON) Get

func (c *ConfigJSON) Get(key string, v interface{}) error

Get get value form given key. Return any error if raised.

func (*ConfigJSON) Set

func (c *ConfigJSON) Set(key string, v interface{}) error

Set set value to given key. Return any error if raised.

type ConfigMap

type ConfigMap map[string]interface{}

ConfigMap Map format config

func (*ConfigMap) Get

func (c *ConfigMap) Get(key string, v interface{}) error

Get get value form given key. Return any error if raised.

func (*ConfigMap) Set

func (c *ConfigMap) Set(key string, v interface{}) error

Set set value to given key. Return any error if raised.

type Driver

type Driver interface {
	//Save save data form reader to named file.
	//Return file id ,file size and any error if raised.
	Save(filename string, reader io.Reader) (id string, length int64, err error)
	//Load load file with given id.
	//Return file reader any error if raised.
	Load(id string) (io.ReadCloser, error)
	//Remove remove file by id.
	//Return any error if raised.
	Remove(id string) error
	//URL convert file id to file url.
	//Return file url and any error if raised.
	URL(id string) (string, error)
}

Driver store driver interface.

func NewDriver

func NewDriver(name string, conf Config, prefix string) (Driver, error)

NewDriver create new driver with given name,config and prefix. Reutrn driver created and any error if raised.

type Error

type Error struct {
	File string
	Err  error
	Type ErrorType
}

Error file store error.

func NewError

func NewError(file string, errorType ErrorType, err error) *Error

NewError create new error with given file,type and error.

func NewNotExistsError

func NewNotExistsError(file string) *Error

NewNotExistsError create new file not exists error.

func NewUnavailableIDError

func NewUnavailableIDError(file string) *Error

NewUnavailableIDError create new id not unavailablel error.

func (*Error) Error

func (e *Error) Error() string

Error return error msg.

type ErrorType

type ErrorType string

ErrorType error type

type Factory

type Factory func(conf Config, prefix string) (Driver, error)

Factory store driver create factory.

type File

type File struct {
	//Store store which field stored in
	Store *Store
	//ID file id
	ID string
	// contains filtered or unexported fields
}

File file stored.

func NewFile

func NewFile(store *Store, id string) *File

NewFile create new file

func (*File) SetURL

func (f *File) SetURL(url string)

SetURL set file url.

func (*File) URL

func (f *File) URL() (url string, err error)

URL return file url and any error if raised.

type Option

type Option interface {
	ApplyTo(*Store) error
}

Option store option interface.

type OptionConfigJSON

type OptionConfigJSON struct {
	Driver string
	Config ConfigJSON
}

OptionConfigJSON option config in json format.

func NewOptionConfigJSON

func NewOptionConfigJSON() *OptionConfigJSON

NewOptionConfigJSON create new option config.

func (*OptionConfigJSON) ApplyTo

func (o *OptionConfigJSON) ApplyTo(store *Store) error

ApplyTo apply option to file store.

type OptionConfigMap

type OptionConfigMap struct {
	Driver string
	Config ConfigMap
}

OptionConfigMap option config in map format.

func NewOptionConfigMap

func NewOptionConfigMap() *OptionConfigMap

NewOptionConfigMap create new option config.

func (*OptionConfigMap) ApplyTo

func (o *OptionConfigMap) ApplyTo(store *Store) error

ApplyTo apply option to file store.

type Store

type Store struct {
	Driver
}

Store file store.

func New

func New() *Store

New create new file store.

func (*Store) File

func (s *Store) File(id string) *File

File create new store file by given id

func (*Store) Init

func (s *Store) Init(option Option) error

Init applu option to store.

Jump to

Keyboard shortcuts

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