53 lines
884 B
Go
53 lines
884 B
Go
package storagehandlers
|
|
|
|
import (
|
|
"errors"
|
|
"mime/multipart"
|
|
)
|
|
|
|
var (
|
|
Storagepath string
|
|
)
|
|
|
|
type StorageHandler interface {
|
|
Init() error
|
|
Add(file *multipart.FileHeader, fileId string) error
|
|
Delete(filename string) error
|
|
Get(filename string) (string, error)
|
|
List() ([]string, error)
|
|
}
|
|
|
|
type StorageStrategy string
|
|
|
|
const (
|
|
FS StorageStrategy = "fs"
|
|
S3 StorageStrategy = "s3"
|
|
)
|
|
|
|
func NewStorageHandler(strategy StorageStrategy) (StorageHandler, error) {
|
|
if Storagepath == "" {
|
|
return nil, errors.New("storage path not set")
|
|
}
|
|
switch strategy {
|
|
case FS:
|
|
h := NewFsHandler()
|
|
err := h.Init()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return h, nil
|
|
|
|
case S3:
|
|
h, err := NewS3Handler()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = h.Init()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return h, nil
|
|
default:
|
|
return nil, errors.New("invalid storage strategy")
|
|
}
|
|
}
|