Files

85 lines
1.4 KiB
Go

package file_manager
import (
"time"
)
type Option func(*option)
type option struct {
dir *string
timeout time.Duration
expire time.Duration
verifyHash bool
s3 *s3Config
}
type s3Config struct {
endpoint string
accessKey string
secretKey string
bucket string
region string
usePathStyle bool
}
func WithDir(dir string) Option {
return func(o *option) {
if dir != "" {
o.dir = &dir
}
}
}
func WithTimeout(timeout time.Duration) Option {
return func(o *option) {
if timeout > 0 {
o.timeout = timeout
}
}
}
func WithExpire(expire time.Duration) Option {
return func(o *option) {
if expire > 0 {
o.expire = expire
}
}
}
func WithS3(endpoint, accessKey, secretKey, bucket, region string) Option {
return func(o *option) {
o.s3 = &s3Config{
endpoint: endpoint,
accessKey: accessKey,
secretKey: secretKey,
bucket: bucket,
region: region,
}
}
}
func WithS3PathStyle(usePathStyle bool) Option {
return func(o *option) {
if o.s3 != nil {
o.s3.usePathStyle = usePathStyle
}
}
}
// New 创建文件管理器
func New(opts ...Option) FileManager {
o := &option{
timeout: 1 * time.Minute,
expire: 8 * time.Hour,
verifyHash: true,
}
for _, opt := range opts {
opt(o)
}
if o.s3 != nil {
return NewS3FileManager(o)
}
return NewLocalFileManager(o)
}