github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/shared/filestore/filesstore.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package filestore 5 6 import ( 7 "io" 8 "time" 9 10 "github.com/pkg/errors" 11 ) 12 13 const ( 14 driverS3 = "amazons3" 15 driverLocal = "local" 16 ) 17 18 type ReadCloseSeeker interface { 19 io.ReadCloser 20 io.Seeker 21 } 22 23 type FileBackend interface { 24 TestConnection() error 25 26 Reader(path string) (ReadCloseSeeker, error) 27 ReadFile(path string) ([]byte, error) 28 FileExists(path string) (bool, error) 29 FileSize(path string) (int64, error) 30 CopyFile(oldPath, newPath string) error 31 MoveFile(oldPath, newPath string) error 32 WriteFile(fr io.Reader, path string) (int64, error) 33 AppendFile(fr io.Reader, path string) (int64, error) 34 RemoveFile(path string) error 35 FileModTime(path string) (time.Time, error) 36 37 ListDirectory(path string) ([]string, error) 38 RemoveDirectory(path string) error 39 } 40 41 type FileBackendSettings struct { 42 DriverName string 43 Directory string 44 AmazonS3AccessKeyId string 45 AmazonS3SecretAccessKey string 46 AmazonS3Bucket string 47 AmazonS3PathPrefix string 48 AmazonS3Region string 49 AmazonS3Endpoint string 50 AmazonS3SSL bool 51 AmazonS3SignV2 bool 52 AmazonS3SSE bool 53 AmazonS3Trace bool 54 } 55 56 func (settings *FileBackendSettings) CheckMandatoryS3Fields() error { 57 if settings.AmazonS3Bucket == "" { 58 return errors.New("missing s3 bucket settings") 59 } 60 61 // if S3 endpoint is not set call the set defaults to set that 62 if settings.AmazonS3Endpoint == "" { 63 settings.AmazonS3Endpoint = "s3.amazonaws.com" 64 } 65 66 return nil 67 } 68 69 func NewFileBackend(settings FileBackendSettings) (FileBackend, error) { 70 switch settings.DriverName { 71 case driverS3: 72 backend, err := NewS3FileBackend(settings) 73 if err != nil { 74 return nil, errors.Wrap(err, "unable to connect to the s3 backend") 75 } 76 return backend, nil 77 case driverLocal: 78 return &LocalFileBackend{ 79 directory: settings.Directory, 80 }, nil 81 } 82 return nil, errors.New("no valid filestorage driver found") 83 }