Skip to content

Commit

Permalink
fix(minio): resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joremysh committed Oct 25, 2024
1 parent 20cdb1a commit 913bdb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 67 deletions.
100 changes: 34 additions & 66 deletions minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,25 @@ type MinioI interface {
GetFilesByPaths(ctx context.Context, logger *zap.Logger, filePaths []string) ([]FileContent, error)
}

type ExpiryGroup string

func (g ExpiryGroup) String() string {
return string(g)
type ExpiryRule struct {
Tag string
ExpirationDays int
}

const (
Location = "us-east-1"

StatusEnabled = "Enabled"
ExpiryTag = "expiry-group"

FreePlanExpiry ExpiryGroup = "free-plan-expiry"
ProPlanExpiry ExpiryGroup = "pro-plan-expiry"
TeamPlanExpiry ExpiryGroup = "team-plan-expiry"
EnterprisePlanExpiry ExpiryGroup = "enterprise-plan-expiry"
)

var expiryTimeConfig = map[ExpiryGroup]int{
FreePlanExpiry: 3,
ProPlanExpiry: 7,
TeamPlanExpiry: 30,
}

type minio struct {
client *miniogo.Client
bucket string
client *miniogo.Client
bucket string
expiryRuleConfig map[string]int
}

func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.Logger) (MinioI, error) {
func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.Logger, expiryRules ...ExpiryRule) (MinioI, error) {
logger.Info("Initializing Minio client and bucket...")

endpoint := net.JoinHostPort(cfg.Host, cfg.Port)
Expand Down Expand Up @@ -98,45 +87,24 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.L
Days: lifecycle.ExpirationDays(30),
},
},
{
ID: "free-plan-rule",
Status: StatusEnabled,
Expiration: lifecycle.Expiration{
Days: lifecycle.ExpirationDays(expiryTimeConfig[FreePlanExpiry]),
},
RuleFilter: lifecycle.Filter{
Tag: lifecycle.Tag{
Key: ExpiryTag,
Value: FreePlanExpiry.String(),
},
},
},
{
ID: "pro-plan-rule",
Status: StatusEnabled,
Expiration: lifecycle.Expiration{
Days: lifecycle.ExpirationDays(expiryTimeConfig[ProPlanExpiry]),
},
RuleFilter: lifecycle.Filter{
Tag: lifecycle.Tag{
Key: ExpiryTag,
Value: ProPlanExpiry.String(),
},
},
},
{
ID: "team-plan-rule",
}

expiryRuleConfig := make(map[string]int)
for _, expiryRule := range expiryRules {
expiryRuleConfig[expiryRule.Tag] = expiryRule.ExpirationDays
lccfg.Rules = append(lccfg.Rules, lifecycle.Rule{
ID: expiryRule.Tag,
Status: StatusEnabled,
Expiration: lifecycle.Expiration{
Days: lifecycle.ExpirationDays(expiryTimeConfig[TeamPlanExpiry]),
Days: lifecycle.ExpirationDays(expiryRule.ExpirationDays),
},
RuleFilter: lifecycle.Filter{
Tag: lifecycle.Tag{
Key: ExpiryTag,
Value: TeamPlanExpiry.String(),
Value: expiryRule.Tag,
},
},
},
})
}

err = client.SetBucketLifecycle(ctx, cfg.BucketName, lccfg)
Expand All @@ -145,30 +113,30 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *Config, logger *zap.L
return nil, err
}

return &minio{client: client, bucket: cfg.BucketName}, nil
return &minio{client: client, bucket: cfg.BucketName, expiryRuleConfig: expiryRuleConfig}, nil
}

type UploadFileParam struct {
FilePath string
FileContent any
FileMimeType string
ExpiryGroup ExpiryGroup
FilePath string
FileContent any
FileMimeType string
ExpiryRuleTag string
}

type UploadFileBytesParam struct {
FilePath string
FileBytes []byte
FileMimeType string
ExpiryGroup ExpiryGroup
FilePath string
FileBytes []byte
FileMimeType string
ExpiryRuleTag string
}

func (m *minio) UploadFile(ctx context.Context, logger *zap.Logger, param *UploadFileParam) (url string, objectInfo *miniogo.ObjectInfo, err error) {
jsonData, _ := json.Marshal(param.FileContent)
return m.UploadFileBytes(ctx, logger, &UploadFileBytesParam{
FilePath: param.FilePath,
FileBytes: jsonData,
FileMimeType: param.FileMimeType,
ExpiryGroup: param.ExpiryGroup,
FilePath: param.FilePath,
FileBytes: jsonData,
FileMimeType: param.FileMimeType,
ExpiryRuleTag: param.ExpiryRuleTag,
})
}

Expand All @@ -179,7 +147,7 @@ func (m *minio) UploadFileBytes(ctx context.Context, logger *zap.Logger, param *
// Create the file path with folder structure
_, err = m.client.PutObject(ctx, m.bucket, param.FilePath, reader, int64(len(param.FileBytes)), miniogo.PutObjectOptions{
ContentType: param.FileMimeType,
UserTags: map[string]string{ExpiryTag: param.ExpiryGroup.String()},
UserTags: map[string]string{ExpiryTag: param.ExpiryRuleTag},
})
if err != nil {
logger.Error("Failed to upload file to MinIO", zap.Error(err))
Expand All @@ -193,9 +161,9 @@ func (m *minio) UploadFileBytes(ctx context.Context, logger *zap.Logger, param *
}

// Generate the presigned URL
expiryDays, ok := expiryTimeConfig[param.ExpiryGroup]
if !ok {
expiryDays = 30
expiryDays, ok := m.expiryRuleConfig[param.ExpiryRuleTag]
if !ok || expiryDays > 7 { // presignedURL Expires cannot be greater than 7 days.
expiryDays = 7
}
expiryDuration := time.Hour * 24 * time.Duration(expiryDays)
presignedURL, err := m.client.PresignedGetObject(ctx, m.bucket, param.FilePath, expiryDuration, nil)
Expand Down
7 changes: 6 additions & 1 deletion minio/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestMinio(t *testing.T) {
FilePath: fileName.String(),
FileContent: data,
FileMimeType: "application/json",
ExpiryGroup: miniox.FreePlanExpiry,
})
require.NoError(t, err)
t.Log("url:", url)
Expand All @@ -50,4 +49,10 @@ func TestMinio(t *testing.T) {
fileBytes, err := mc.GetFile(ctx, log, fileName.String())
require.NoError(t, err)
require.Equal(t, jsonBytes, fileBytes)

err = mc.DeleteFile(ctx, log, fileName.String())
require.NoError(t, err)

_, err = mc.GetFile(ctx, log, fileName.String())
require.Error(t, err)
}

0 comments on commit 913bdb5

Please sign in to comment.