2025-04-17 12:44:25 +00:00
|
|
|
package repository
|
2024-02-19 18:09:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2025-04-17 12:44:25 +00:00
|
|
|
bucket = "3c580be9-cf31f296-d055-49cf-b39e-30c7959dc17b"
|
|
|
|
folderImages = "squizimages"
|
2024-02-19 18:09:39 +00:00
|
|
|
bucketFonts = "squizfonts"
|
|
|
|
bucketScripts = "squizscript"
|
|
|
|
bucketStyle = "squizstyle"
|
|
|
|
)
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
type S3 struct {
|
2024-02-19 18:09:39 +00:00
|
|
|
client *minio.Client
|
|
|
|
}
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
func New(ctx context.Context, minioClient *minio.Client) (*S3, error) {
|
2024-02-19 18:09:39 +00:00
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
return &S3{
|
2024-02-19 18:09:39 +00:00
|
|
|
client: minioClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
func (s *S3) UploadImages(ctx context.Context, quid string, files map[string]io.Reader, sizes map[string]int64) []error {
|
2024-02-19 18:09:39 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
m sync.Mutex
|
|
|
|
errs []error
|
|
|
|
)
|
|
|
|
wg.Add(len(files))
|
|
|
|
for f, r := range files {
|
|
|
|
fname := f
|
|
|
|
reader := r
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, err := s.client.PutObject(ctx,
|
2024-05-28 13:54:30 +00:00
|
|
|
bucket,
|
|
|
|
folderImages+"/"+fmt.Sprintf("%s/%s", quid, fname),
|
2024-02-19 18:09:39 +00:00
|
|
|
reader,
|
|
|
|
sizes[fname],
|
|
|
|
minio.PutObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
func (s *S3) UploadFonts(ctx context.Context, quid string, files map[string]io.Reader, sizes map[string]int64) []error {
|
2024-02-19 18:09:39 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
m sync.Mutex
|
|
|
|
errs []error
|
|
|
|
)
|
|
|
|
wg.Add(len(files))
|
|
|
|
for f, r := range files {
|
|
|
|
fname := f
|
|
|
|
reader := r
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if _, err := s.client.PutObject(ctx,
|
2024-05-28 13:54:30 +00:00
|
|
|
bucket,
|
2025-04-17 12:44:25 +00:00
|
|
|
bucketFonts+"/"+fmt.Sprintf("%s/%s", quid, fname),
|
2024-02-19 18:09:39 +00:00
|
|
|
reader,
|
|
|
|
sizes[fname],
|
|
|
|
minio.PutObjectOptions{}); err != nil {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
func (s *S3) UploadScript(ctx context.Context, quid string, file io.Reader, size int64) error {
|
2024-02-19 18:09:39 +00:00
|
|
|
if _, err := s.client.PutObject(ctx,
|
2024-05-28 13:54:30 +00:00
|
|
|
bucket,
|
2025-04-17 12:44:25 +00:00
|
|
|
bucketScripts+"/"+fmt.Sprintf("%s.js", quid),
|
2024-02-19 18:09:39 +00:00
|
|
|
file,
|
|
|
|
size,
|
|
|
|
minio.PutObjectOptions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-17 12:44:25 +00:00
|
|
|
func (s *S3) UploadStyle(ctx context.Context, quid string, file io.Reader, size int64) error {
|
2024-02-19 18:09:39 +00:00
|
|
|
if _, err := s.client.PutObject(ctx,
|
2024-05-28 13:54:30 +00:00
|
|
|
bucket,
|
2025-04-17 12:44:25 +00:00
|
|
|
bucketStyle+"/"+fmt.Sprintf("%s.css", quid),
|
2024-02-19 18:09:39 +00:00
|
|
|
file,
|
|
|
|
size,
|
|
|
|
minio.PutObjectOptions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|