add media service
This commit is contained in:
48
internal/processor/image.go
Normal file
48
internal/processor/image.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
_ "image/png" // Для поддержки декодирования PNG
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
// ProcessImage обрабатывает картинку в зависимости от типа загрузки
|
||||
// mode может быть: "avatar", "chat", "raw"
|
||||
func ProcessImage(fileBytes []byte, mode string) ([]byte, string, error) {
|
||||
// Если пользователь отправил "как файл" (без сжатия)
|
||||
if mode == "raw" {
|
||||
return fileBytes, "image/jpeg", nil // В идеале тут нужно определять mime-type по байтам
|
||||
}
|
||||
|
||||
// Декодируем исходную картинку
|
||||
img, _, err := image.Decode(bytes.NewReader(fileBytes))
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
var processedImg image.Image
|
||||
|
||||
switch mode {
|
||||
case "avatar":
|
||||
// Telegram делает аватарки квадратными (например, 500x500)
|
||||
processedImg = imaging.Fill(img, 500, 500, imaging.Center, imaging.Lanczos)
|
||||
case "chat":
|
||||
// Ограничиваем максимальный размер для чата (например, 1280px по большей стороне),
|
||||
// сохраняя пропорции
|
||||
processedImg = imaging.Fit(img, 1280, 1280, imaging.Lanczos)
|
||||
default:
|
||||
processedImg = img
|
||||
}
|
||||
|
||||
// Кодируем результат в сжатый JPEG (качество 80 - отличный баланс размера и качества)
|
||||
buf := new(bytes.Buffer)
|
||||
err = jpeg.Encode(buf, processedImg, &jpeg.Options{Quality: 80})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return buf.Bytes(), "image/jpeg", nil
|
||||
}
|
||||
67
internal/storage/minio.go
Normal file
67
internal/storage/minio.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type MinioStorage struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewMinioStorage(endpoint, accessKey, secretKey, bucket string) (*MinioStorage, error) {
|
||||
client, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Secure: false, // Для локальной разработки (без HTTPS)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
exists, err := client.BucketExists(ctx, bucket)
|
||||
if err == nil && !exists {
|
||||
err = client.MakeBucket(ctx, bucket, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
slog.Info("Бакет создан", "bucket", bucket)
|
||||
// Бакет остается ПРИВАТНЫМ (нет публичного BucketPolicy).
|
||||
}
|
||||
|
||||
return &MinioStorage{
|
||||
client: client,
|
||||
bucket: bucket,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Upload(ctx context.Context, objectName string, reader io.Reader, size int64, contentType string) (string, error) {
|
||||
info, err := s.client.PutObject(ctx, s.bucket, objectName, reader, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Возвращаем ТОЛЬКО имя файла (info.Key).
|
||||
// Полный путь клиенту не нужен, он сам не сможет по нему перейти.
|
||||
return info.Key, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) GeneratePresignedURL(ctx context.Context, objectName string, expiry time.Duration) (string, error) {
|
||||
reqParams := make(url.Values)
|
||||
|
||||
presignedURL, err := s.client.PresignedGetObject(ctx, s.bucket, objectName, expiry, reqParams)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user