package storage
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/goodrain/rainbond/config/configs"
"github.com/goodrain/rainbond/event"
"github.com/sirupsen/logrus"
"mime/multipart"
"net/http"
)
type StorageComponent struct {
StorageCli InterfaceStorage
storageConfig *configs.StorageConfig
}
var defaultStorageComponent *StorageComponent
func New() *StorageComponent {
storageConfig := configs.Default().StorageConfig
defaultStorageComponent = &StorageComponent{
storageConfig: storageConfig,
}
return defaultStorageComponent
}
func (s *StorageComponent) Start(ctx context.Context) error {
var storageCli InterfaceStorage
logrus.Infof("create s3 client %v,----%v,----%v", s.storageConfig.StorageType, s.storageConfig.S3AccessKeyID, s.storageConfig.S3SecretAccessKey)
if s.storageConfig.StorageType == "s3" {
sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(s.storageConfig.S3Endpoint),
Region: aws.String("rainbond"),
Credentials: credentials.NewStaticCredentials(s.storageConfig.S3AccessKeyID, s.storageConfig.S3SecretAccessKey, ""),
S3ForcePathStyle: aws.Bool(true),
})
if err != nil {
logrus.Errorf("failed to create session: %v", err)
return err
}
s3Client := s3.New(sess)
s3Storage := &S3Storage{s3Client: s3Client}
logrus.Info("Initializing S3 bucket lifecycle policies on startup...")
if err := s3Storage.InitBucketLifecycle(); err != nil {
logrus.Warnf("Failed to initialize bucket lifecycle policies: %v (non-fatal, continuing startup)", err)
} else {
logrus.Info("Successfully initialized S3 bucket lifecycle policies")
}
storageCli = s3Storage
} else {
storageCli = &LocalStorage{}
}
s.StorageCli = storageCli
return nil
}
func (s *StorageComponent) CloseHandle() {
}
func Default() *StorageComponent {
return defaultStorageComponent
}
type InterfaceStorage interface {
MkdirAll(path string) error
Unzip(archive, target string, currentDirectory bool) error
ReadDir(dirName string) ([]string, error)
ServeFile(w http.ResponseWriter, r *http.Request, filePath string)
SaveFile(fileName string, reader multipart.File) error
UploadFileToFile(src string, dst string, logger event.Logger) error
DownloadDirToDir(srcDir, dstDir string) error
DownloadFileToDir(srcFile, dstDir string) error
ReadFile(filePath string) (ReadCloser, error)
SaveChunk(sessionID string, chunkIndex int, reader multipart.File) (string, error)
MergeChunks(sessionID string, outputPath string, totalChunks int) error
ChunkExists(sessionID string, chunkIndex int) bool
CleanupChunks(sessionID string) error
GetChunkDir(sessionID string) string
}
type ReadCloser interface {
Read(p []byte) (n int, err error)
Close() error
}
type SrcFile interface {
Read([]byte) (int, error)
}
type DstFile interface {
Write([]byte) (int, error)
}