upload

package
v5.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: AGPL-3.0 Imports: 13 Imported by: 5

Documentation

Index

Constants

View Source
const Name = `upload`

Variables

View Source
var (
	// UploadURLPath 上传文件网址访问路径
	UploadURLPath = defaultUploadURLPath

	// UploadDir 定义上传目录(首尾必须带“/”)
	UploadDir = defaultUploadDir

	// AllowedExtensions 被允许上传的文件的扩展名
	AllowedExtensions = []string{
		`.jpeg`, `.jpg`, `.gif`, `.png`, `.mp4`,
	}
)
View Source
var (
	ErrUnsupportedExtension = errors.New("不支持的文件扩展名")
	ErrIncorrectPath        = errors.New("路径不合法")
)
View Source
var IsRightUploadFile = func(ctx echo.Context, src string) error {
	src = path.Clean(src)
	ext := strings.ToLower(path.Ext(src))
	cfg := Get()
	if !com.InSlice(ext, cfg.AllowedExtensions) {
		return errors.WithMessage(ErrIncorrectPath, ext)
	}
	if !strings.HasPrefix(src, UploadURLPath) {
		return ErrIncorrectPath
	}
	return nil
}

IsRightUploadFile 是否是正确的上传文件

View Source
var ReplacePlaceholder = func(s string, repl func(string) string) string {
	return placeholderRegexp.ReplaceAllStringFunc(s, func(find string) string {
		id := find[9 : len(find)-1]
		return repl(id)
	})
}

ReplacePlaceholder 从文本中替换占位符

Functions

func CleanDomain

func CleanDomain(fileURL string) string

func EmbeddedRes

func EmbeddedRes(v string, fn func(string, int64)) [][]string

EmbeddedRes 获取正文中的资源

func ExtensionRegexpEnd

func ExtensionRegexpEnd(noCaptures ...bool) string

func FileTypeByName

func FileTypeByName(filename string) string

FileTypeByName 根据文件名判断文件类型

func ParseDomain

func ParseDomain(fileURL string) (scheme string, domain string)

func ParseSubdir

func ParseSubdir(fileURL string) string

ParseSubdir 从文件网址中获取子文件夹名

func RelatedRes

func RelatedRes(v string, fn func(string, int64), seperator ...string)

RelatedRes 获取字段中关联的资源

func ReplaceEmbeddedRes

func ReplaceEmbeddedRes(v string, reses map[string]string) (r string)

ReplaceEmbeddedRes 替换正文中的资源网址

func ReplaceEmbeddedResID

func ReplaceEmbeddedResID(v string, reses map[uint64]string) (r string)

ReplaceEmbeddedResID 替换正文中的资源网址

func ReplaceRelatedRes

func ReplaceRelatedRes(v string, reses map[string]string, seperator ...string) (r string)

ReplaceRelatedRes 替换字段中的资源网址

func ReplaceRelatedResID

func ReplaceRelatedResID(v string, reses map[uint64]string, seperator ...string) (r string)

ReplaceRelatedResID 替换字段中的资源网址

func URLToFile

func URLToFile(fileURL string) string

URLToFile 文件网址转为存储路径

Types

type Config

type Config struct {
	FileTypes         map[string]*FileType `json:"fileTypes"`
	MaxSize           string               `json:"maxSize"`
	Icon              string               `json:"icon"`
	AllowedExtensions []string             `json:"allowedExtensions"`
	// contains filtered or unexported fields
}

func Get

func Get() *Config

func NewConfig

func NewConfig() *Config

func (*Config) CheckTypeExtension

func (c *Config) CheckTypeExtension(typ string, extension string) bool

CheckTypeExtension 检查类型扩展名

func (*Config) DetectType

func (c *Config) DetectType(extension string) string

DetectType 根据扩展名判断类型

func (*Config) Extensions

func (c *Config) Extensions(typ string) (r []string)

Extensions 文件类型文件扩展名

func (*Config) FileIcon

func (c *Config) FileIcon(typ string) string

func (*Config) Init

func (c *Config) Init()

func (*Config) MaxSizeBytes

func (c *Config) MaxSizeBytes(typ string) int

func (*Config) Reload

func (c *Config) Reload() error

func (*Config) SetDefaults

func (c *Config) SetDefaults()

type FileType

type FileType struct {
	Icon         string   `json:"icon"`
	Extensions   []string `json:"extensions"`
	MIMEs        []string `json:"mimes"`
	MIMEKeywords []string `json:"mimeKeywords"`
	MaxSize      string   `json:"maxSize"`
	Description  string   `json:"description"`
	Disabled     bool     `json:"disabled"`
	// contains filtered or unexported fields
}

func (*FileType) Init

func (c *FileType) Init()

func (*FileType) MaxSizeBytes

func (c *FileType) MaxSizeBytes() int

Jump to

Keyboard shortcuts

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