feat (patient): upload done
This commit is contained in:
@@ -27,17 +27,35 @@ func (repo *minioRepository) createBucket(bucketName string, region string) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
return nil
|
||||
}
|
||||
if err := repo.client.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{Region: region, ObjectLocking: true}); err != nil {
|
||||
return err
|
||||
if !exist {
|
||||
// create bucket
|
||||
if err := repo.client.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{Region: region}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set bucket policy to public read
|
||||
policy := fmt.Sprintf(`{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Principal": {"AWS": ["*"]},
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
}
|
||||
]
|
||||
}`, bucketName)
|
||||
|
||||
if err := repo.client.SetBucketPolicy(context.Background(), bucketName, policy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Upload file reader to MinIO
|
||||
func (repo *minioRepository) PutObject(input m.UploadReaderInput) (*minio.UploadInfo, error) {
|
||||
func (repo *minioRepository) PutObject(input UploadReaderInput) (*minio.UploadInfo, error) {
|
||||
if err := repo.createBucket(input.BucketName, m.O.GetRegion()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -55,7 +73,7 @@ func (repo *minioRepository) PutObject(input m.UploadReaderInput) (*minio.Upload
|
||||
}
|
||||
|
||||
// Upload file path to MinIO
|
||||
func (repo *minioRepository) FPutObject(input m.UploadPathInput) (*minio.UploadInfo, error) {
|
||||
func (repo *minioRepository) FPutObject(input UploadPathInput) (*minio.UploadInfo, error) {
|
||||
if err := repo.createBucket(input.BucketName, m.O.GetRegion()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,7 +138,7 @@ func (repo *minioRepository) GeneratePresignedPost(policy *minio.PostPolicy) (*u
|
||||
}
|
||||
|
||||
// create presigned url to get object
|
||||
func (repo *minioRepository) GeneratePresignedGetObject(input m.PresignedGetInput) (*url.URL, error) {
|
||||
func (repo *minioRepository) GeneratePresignedGetObject(input PresignedGetInput) (*url.URL, error) {
|
||||
presignedUrl, err := repo.client.PresignedGetObject(context.Background(), input.Bucket, input.Object, input.Expiry, input.ReqParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package miniohelper
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UploadReaderInput struct {
|
||||
File io.Reader
|
||||
Name string
|
||||
Size int64
|
||||
ContentType string
|
||||
BucketName string
|
||||
}
|
||||
|
||||
type UploadPathInput struct {
|
||||
BucketName string
|
||||
ContentType string
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
type PresignedGetInput struct {
|
||||
Bucket string
|
||||
Object string
|
||||
Expiry time.Duration
|
||||
ReqParams url.Values
|
||||
}
|
||||
@@ -38,9 +38,8 @@ func getValidFileTypesForBucket(bucketName string) []string {
|
||||
}
|
||||
|
||||
// isValidFileType checks if the uploaded file type is allowed for the specific bucket
|
||||
func isValidFileType(filename, bucketName string) bool {
|
||||
func IsValidFileType(ext, bucketName string) bool {
|
||||
allowedTypes := getValidFileTypesForBucket(bucketName)
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
|
||||
for _, allowedExt := range allowedTypes {
|
||||
if ext == allowedExt {
|
||||
|
||||
Reference in New Issue
Block a user