github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/fs/fs.go (about) 1 package fs 2 3 import ( 4 "context" 5 "io" 6 "mime/multipart" 7 ) 8 9 var defaultStorageDisk FSDisk = nil 10 11 type FSDisk interface { 12 Name() string 13 Url(filepath string) string 14 Delete(ctx context.Context, filepath string) error 15 Put(ctx context.Context, in io.Reader, size int64, mime, dst string) (*FileInfo, error) 16 PutMultipart(ctx context.Context, m *multipart.FileHeader, dsts ...string) (*FileInfo, error) 17 } 18 19 type FileInfo struct { 20 Disk string `json:"disk,omitempty"` 21 Path string `json:"path,omitempty"` 22 Type string `json:"type,omitempty"` 23 Size int `json:"size,omitempty"` 24 } 25 26 type DiskConfig struct { 27 Name string `json:"name"` 28 Driver string `json:"driver"` 29 Root string `json:"root"` 30 BaseUrl string `json:"base_url"` 31 BaseUrlFn func() string `json:"-"` 32 Provider string `json:"provider"` 33 Endpoint string `json:"endpoint"` 34 Region string `json:"region"` 35 Bucket string `json:"bucket"` 36 AccessKeyID string `json:"access_key_id"` 37 SecretAccessKey string `json:"secret_access_key"` 38 ACL string `json:"acl"` 39 } 40 41 type StorageConfig struct { 42 DefaultDisk string `json:"default_disk"` 43 DiskConfigs []*DiskConfig `json:"disks"` 44 } 45 46 var fsDisks []FSDisk 47 48 func New(defaultDisk string, disks []FSDisk) { 49 fsDisks = append(fsDisks, disks...) 50 51 if len(fsDisks) == 0 { 52 panic("No disk found") 53 } 54 55 for _, disk := range fsDisks { 56 if disk.Name() == defaultDisk { 57 defaultStorageDisk = disk 58 break 59 } 60 } 61 62 if defaultStorageDisk == nil { 63 panic("No default disk found") 64 } 65 } 66 67 func Disk(names ...string) FSDisk { 68 if len(names) == 0 { 69 return defaultStorageDisk 70 } 71 72 for _, disk := range fsDisks { 73 if disk.Name() == names[0] { 74 return disk 75 } 76 } 77 78 return nil 79 }