service

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package service define many components for implementing system. Every service completes some specify function.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPathExisted represent the path has already existed
	ErrPathExisted = errors.New("the path has already existed")
	// ErrOnlyOneRenameAppendOverWrite represent uncertain operation
	ErrOnlyOneRenameAppendOverWrite = errors.New("only one of rename, append and overwrite is allowed")

	// ErrFileHasBeenDeleted represent that the file has been deleted
	ErrFileHasBeenDeleted = errors.New("the file has been deleted")
)
View Source
var (
	// Validate represent a global validator
	Validate = validator.New()

	// ErrInvalidApplication represent some apps are invalid
	ErrInvalidApplication = errors.New("invalid application")

	// ErrInvalidToken represent invalid tokens
	ErrInvalidToken = errors.New("invalid token")

	// ErrTokenIP represent that token forbid some ips to access
	ErrTokenIP = errors.New("token can't be used by this ip")

	// ErrTokenAvailableTimesExhausted represent that the available
	// times of token has been exhausted
	ErrTokenAvailableTimesExhausted = errors.New("the available times of token has already exhausted")

	// ErrTokenReadOnly represent that token is read only
	ErrTokenReadOnly = errors.New("this token is read only")

	// ErrTokenExpired represent token is expired
	ErrTokenExpired = errors.New("token is expired")

	// ErrInvalidFile represent the file is invalid
	ErrInvalidFile = errors.New("invalid file")
)
View Source
var (
	// ErrInvalidPath represent that path is not a legal unix path
	ErrInvalidPath = errors.New("path is not a legal unix path")
)
View Source
var ErrListFile = errors.New("can't list the content of a file")

ErrListFile represent that you are listing a directory

View Source
var ErrReadHiddenFile = errors.New("try to read the hidden file")

ErrReadHiddenFile represent that the hidden file is being read

View Source
var (
	// PreDefinedValidateErrors map service field to specific error
	PreDefinedValidateErrors = map[string]*ValidateError{

		"TokenCreate.App": {
			Code:  10002,
			Field: "TokenCreate.App",
			Msg:   "can't find specific application by input params",
		},
		"TokenCreate.Path": {
			Code:  10003,
			Field: "TokenCreate.Path",
			Msg:   "path of token can't be empty, max of length is 1000, and must be a legal unix path",
		},
		"TokenCreate.IP": {
			Code:  10004,
			Field: "TokenCreate.Ip",
			Msg:   "max length of ip is 1500",
		},
		"TokenCreate.Secret": {
			Code:  10005,
			Field: "TokenCreate.Secret",
			Msg:   "secret of token is 32",
		},
		"TokenCreate.AvailableTimes": {
			Code:  10006,
			Field: "TokenCreate.AvailableTimes",
			Msg:   "availableTimes of token is greater than -1",
		},
		"TokenCreate.ReadOnly": {
			Code:  10007,
			Field: "TokenCreate.ReadOnly",
			Msg:   "readOnly of token is 0 or 1",
		},

		"TokenUpdate.Token": {
			Code:  10008,
			Field: "TokenUpdate.Token",
			Msg:   "token is required",
		},
		"TokenUpdate.IP": {
			Code:  10009,
			Field: "TokenUpdate.IP",
			Msg:   "max length of ip is 1500, it's optional",
		},
		"TokenUpdate.Path": {
			Code:  10010,
			Field: "TokenUpdate.Path",
			Msg:   "max length of ip is 1000, and must be a legal unix path, is's optional",
		},
		"TokenUpdate.Secret": {
			Code:  10011,
			Field: "TokenUpdate.Secret",
			Msg:   "the length of secret is 32, it's optional",
		},
		"TokenUpdate.ReadOnly": {
			Code:  10012,
			Field: "TokenUpdate.ReadOnly",
			Msg:   "readOnly is 1 or 0, it's optional",
		},
		"TokenUpdate.ExpiredAt": {
			Code:  10013,
			Field: "TokenUpdate.ExpiredAt",
			Msg:   "expiredAt must be greater than now, it's optional",
		},
		"TokenUpdate.AvailableTimes": {
			Code:  10014,
			Field: "TokenUpdate.AvailableTimes",
			Msg:   "availableTimes must be a integer, and must be greater than -1, it's optional",
		},

		"FileCreate.App": {
			Code:  10015,
			Field: "FileCreate.App",
			Msg:   "can't find specific application by input params",
		},
		"FileCreate.Token": {
			Code:  10016,
			Field: "FileCreate.Token",
			Msg:   "can't find specific token by input params",
		},
		"FileCreate.Path": {
			Code:  10017,
			Field: "FileCreate.Path",
			Msg:   "path of file or directory can't be empty, max of length is 1000, and must be a legal unix path",
		},
		"FileCreate.Hidden": {
			Code:  10018,
			Field: "FileCreate.Hidden",
			Msg:   "hidden must be 0 or 1",
		},
		"FileCreate.Overwrite": {
			Code:  10019,
			Field: "FileCreate.Overwrite",
			Msg:   "overwrite must be 0 or 1",
		},
		"FileCreate.Rename": {
			Code:  10020,
			Field: "FileCreate.Rename",
			Msg:   "rename must be 0 or 1",
		},
		"FileCreate.Append": {
			Code:  10021,
			Field: "FileCreate.Append",
			Msg:   "append must be 0 or 1",
		},
		"FileCreate.Operate": {
			Code:  10022,
			Field: "FileCreate.Operate",
			Msg:   ErrOnlyOneRenameAppendOverWrite.Error(),
		},

		"FileRead.Token": {
			Code:  10023,
			Field: "FileRead.Token",
			Msg:   "token is required",
		},
		"FileRead.File": {
			Code:  10024,
			Field: "FileRead.File",
			Msg:   "file is required",
		},

		"FileUpdate.Token": {
			Code:  10025,
			Field: "FileUpdate.Token",
			Msg:   "token is required",
		},
		"FileUpdate.File": {
			Code:  10026,
			Field: "FileUpdate.File",
			Msg:   "file is required",
		},
		"FileUpdate.Hidden": {
			Code:  10027,
			Field: "FileUpdate.Hidden",
			Msg:   "file is required",
		},
		"FileUpdate.Path": {
			Code:  10028,
			Field: "FileUpdate.Path",
			Msg:   "file is required",
		},

		"FileDelete.Token": {
			Code:  10029,
			Field: "FileDelete.Token",
			Msg:   "token is required",
		},
		"FileDelete.File": {
			Code:  10030,
			Field: "FileDelete.File",
			Msg:   "file is required",
		},

		"DirectoryList.Token": {
			Code:  10031,
			Field: "DirectoryList.Token",
			Msg:   "token is required",
		},
		"DirectoryList.Sort": {
			Code:  10032,
			Field: "DirectoryList.Sort",
			Msg:   "sort is only allowed to be one of type -type name -name time -time",
		},
		"DirectoryList.SubDir": {
			Code:  10033,
			Field: "DirectoryList.SubDir",
			Msg:   "subDir must be a legal unix path",
		},
		"DirectoryList.Offset": {
			Code:  10034,
			Field: "DirectoryList.Offset",
			Msg:   "the min value of offset is 0",
		},
		"DirectoryList.Limit": {
			Code:  10035,
			Field: "DirectoryList.Limit",
			Msg:   "the min value of limit is 10, and max of limit 20",
		},
	}
)

Functions

func ValidateApp

func ValidateApp(db *gorm.DB, app *models.App) error

ValidateApp is used to validate whether app is valid

func ValidateFile

func ValidateFile(db *gorm.DB, file *models.File) error

ValidateFile is used to validate whether a file is valid

func ValidatePath

func ValidatePath(path string) bool

ValidatePath is used to validate whether the given path is legal

func ValidateToken

func ValidateToken(db *gorm.DB, ip *string, canReadOnly bool, token *models.Token) error

ValidateToken is used to validate whether the token is valid

Types

type AfterHandler

type AfterHandler = func(ctx context.Context, service Service) error

AfterHandler is used to add handle function that is executed after Execute.

type BaseService

type BaseService struct {

	// Before includes many BeforeHandler
	Before []BeforeHandler

	// After is consists of many AfterHandler
	After []AfterHandler

	// DB represent a database connection
	DB *gorm.DB

	// Value is designed to provide value container for user
	Value map[string]interface{}

	// rootPath is used to set chunk storage path in test environment
	RootPath *string
}

BaseService only includes two fields: Before and After, the handler in Before will be executed in front of Execute and in After will be executed in back of Execute

func (*BaseService) CallAfter

func (b *BaseService) CallAfter(ctx context.Context, service Service) error

CallAfter will call AfterHandler in turn, return err if something goes wrong

func (*BaseService) CallBefore

func (b *BaseService) CallBefore(ctx context.Context, service Service) error

CallBefore will call BeforeHandler in turn, return err if something goes wrong

func (*BaseService) Execute

func (b *BaseService) Execute(ctx context.Context) (interface{}, error)

Execute is only for implementing Service

func (*BaseService) Validate

func (b *BaseService) Validate() ValidateErrors

Validate is only for implementing Service

type BeforeHandler

type BeforeHandler = func(ctx context.Context, service Service) error

BeforeHandler is used to add handle function that is executed before Execute.

type DirectoryList

type DirectoryList struct {
	BaseService

	Token  *models.Token `validate:"required"`
	IP     *string       `validate:"omitempty"`
	SubDir string        `validate:"omitempty"`
	Sort   string        `validate:"required,oneof=type -type name -name time -time"`
	Offset int           `validate:"omitempty,min=0"`
	Limit  int           `validate:"required,min=10,max=20"`
}

DirectoryList is used to list all files and sub directories

func (*DirectoryList) Execute

func (dl *DirectoryList) Execute(ctx context.Context) (interface{}, error)

Execute is used to list a directory

func (*DirectoryList) Validate

func (dl *DirectoryList) Validate() ValidateErrors

Validate is used to validate params

type DirectoryListResponse

type DirectoryListResponse struct {
	Total int
	Pages int
	Files []models.File
}

DirectoryListResponse represent the response value of DirectoryList service

type FileCreate

type FileCreate struct {
	BaseService

	Token     *models.Token `validate:"required"`
	Path      string        `validate:"required,max=1000"`
	Hidden    int8          `validate:"oneof=0 1"`
	IP        *string       `validate:"omitempty"`
	Reader    io.Reader     `validate:"omitempty"`
	Overwrite int8          `validate:"oneof=0 1"`
	Rename    int8          `validate:"oneof=0 1"`
	Append    int8          `validate:"oneof=0 1"`
}

FileCreate is used to upload file or create directory

func (*FileCreate) Execute

func (fc *FileCreate) Execute(ctx context.Context) (interface{}, error)

Execute is used to upload file or create directory

func (*FileCreate) Validate

func (fc *FileCreate) Validate() ValidateErrors

Validate is used to validate params

type FileDelete

type FileDelete struct {
	BaseService

	Token *models.Token `validate:"required"`
	File  *models.File  `validate:"required"`
	Force *bool         `validate:"omitempty"`
	IP    *string       `validate:"omitempty"`
}

FileDelete is used to provide service for deleting file

func (*FileDelete) Execute

func (fd *FileDelete) Execute(ctx context.Context) (interface{}, error)

Execute is used to provide file delete service

func (*FileDelete) Validate

func (fd *FileDelete) Validate() ValidateErrors

Validate is used to validate service params

type FileRead

type FileRead struct {
	BaseService

	Token *models.Token `validate:"required"`
	File  *models.File  `validate:"required"`
	IP    *string       `validate:"omitempty"`
}

FileRead is used to provide file read service

func (*FileRead) Execute

func (fr *FileRead) Execute(ctx context.Context) (interface{}, error)

Execute is used to read file

func (*FileRead) Validate

func (fr *FileRead) Validate() ValidateErrors

Validate is used to validate service params

type FileUpdate

type FileUpdate struct {
	BaseService

	Token  *models.Token `validate:"required"`
	File   *models.File  `validate:"required"`
	IP     *string       `validate:"omitempty"`
	Hidden *int8         `validate:"omitempty,oneof=0 1"`
	Path   *string       `validate:"omitempty,max=1000"`
}

FileUpdate is used uo update a file, such as move file to another path, or rename file, hide file.

func (*FileUpdate) Execute

func (fu *FileUpdate) Execute(ctx context.Context) (interface{}, error)

Execute is used to update file

func (*FileUpdate) Validate

func (fu *FileUpdate) Validate() ValidateErrors

Validate is used to validate service params

type Service

type Service interface {

	// Execute is designed to implement specific function
	Execute(ctx context.Context) (interface{}, error)

	// Validate is designed to validate input params
	Validate() ValidateErrors
}

Service interface conventions all subtypes must implement the Execute method.

type TokenCreate

type TokenCreate struct {
	BaseService

	IP             *string     `validate:"omitempty,max=1500"`
	App            *models.App `validate:"required"`
	Path           string      `validate:"required,max=1000"`
	Secret         *string     `validate:"omitempty,min=12,max=32"`
	ReadOnly       int8        `validate:"oneof=0 1"`
	ExpiredAt      *time.Time  `validate:"omitempty,gt"`
	AvailableTimes int         `validate:"omitempty,gte=-1,max=2147483647"`
	// contains filtered or unexported fields
}

TokenCreate provide service for create token

func (*TokenCreate) Execute

func (t *TokenCreate) Execute(ctx context.Context) (interface{}, error)

Execute is used to implement token create

func (*TokenCreate) Validate

func (t *TokenCreate) Validate() ValidateErrors

Validate is used to validate input params

type TokenUpdate

type TokenUpdate struct {
	BaseService

	Token          string     `validate:"required"`
	IP             *string    `validate:"omitempty,max=1500"`
	Path           *string    `validate:"omitempty,max=1000"`
	Secret         *string    `validate:"omitempty,min=12,max=32"`
	ReadOnly       *int8      `validate:"omitempty,oneof=0 1"`
	ExpiredAt      *time.Time `validate:"omitempty,gt"`
	AvailableTimes *int       `validate:"omitempty,gte=-1,max=2147483647"`
}

TokenUpdate provide service for updating token

func (*TokenUpdate) Execute

func (t *TokenUpdate) Execute(ctx context.Context) (result interface{}, err error)

Execute is used to update token

func (*TokenUpdate) Validate

func (t *TokenUpdate) Validate() ValidateErrors

Validate is used to validate input params

type ValidateError

type ValidateError struct {
	Msg       string `json:"msg"`
	Field     string `json:"field"`
	Code      int    `json:"code"`
	Exception error
}

ValidateError is defined validate error information

func (*ValidateError) Error

func (v *ValidateError) Error() string

Error implement error interface

type ValidateErrors

type ValidateErrors []*ValidateError

ValidateErrors is an array of ValidateError

func (ValidateErrors) ContainsErrCode

func (v ValidateErrors) ContainsErrCode(code int) bool

ContainsErrCode will check whether ValidateErrors contains err by code

func (ValidateErrors) Error

func (v ValidateErrors) Error() string

Error implement error interface

func (ValidateErrors) Map

func (v ValidateErrors) Map() map[int]string

Map will transform error to map[code] = errMsg form

func (ValidateErrors) MapFieldErrors

func (v ValidateErrors) MapFieldErrors() map[string][]string

MapFieldErrors is used to represent error in other way. It's mainly used to represent http response errors

Jump to

Keyboard shortcuts

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