add media service
This commit is contained in:
50
internal/infrastructure/images/processor.go
Normal file
50
internal/infrastructure/images/processor.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
type Processor interface {
|
||||
Process(input []byte, mode string) ([]byte, string, error)
|
||||
}
|
||||
|
||||
type ImageProcessor struct{}
|
||||
|
||||
func NewImageProcessor() *ImageProcessor {
|
||||
return &ImageProcessor{}
|
||||
}
|
||||
|
||||
func (p *ImageProcessor) Process(fileBytes []byte, mode string) ([]byte, string, error) {
|
||||
if mode == "raw" {
|
||||
return fileBytes, "application/octet-stream", nil
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(bytes.NewReader(fileBytes))
|
||||
if err != nil {
|
||||
// Если это не картинка (например, документ или видео), просто возвращаем исходные байты
|
||||
return fileBytes, "application/octet-stream", nil
|
||||
}
|
||||
|
||||
var processedImg image.Image
|
||||
switch mode {
|
||||
case "avatar":
|
||||
processedImg = imaging.Fill(img, 500, 500, imaging.Center, imaging.Lanczos)
|
||||
case "chat":
|
||||
processedImg = imaging.Fit(img, 1280, 1280, imaging.Lanczos)
|
||||
default:
|
||||
processedImg = img
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user