package service import ( "bytes" "context" "crypto/sha256" "encoding/hex" "errors" "image" _ "image/jpeg" _ "image/png" "io" "github.com/google/uuid" "mtusi2026-messenger/internal/db" "mtusi2026-messenger/internal/model" "mtusi2026-messenger/internal/postgres" s3store "mtusi2026-messenger/internal/s3" ) var ( ErrAttachmentNotFound = errors.New("attachment not found") ErrInvalidImage = errors.New("invalid image") ErrInvalidSize = errors.New("invalid size") ) type attachmentQueries interface { CreateAttachment(ctx context.Context, arg db.CreateAttachmentParams) (db.Attachment, error) GetAttachmentByID(ctx context.Context, id uuid.UUID) (db.Attachment, error) IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error) } type AttachmentService struct { q attachmentQueries s3 *s3store.Client maxBytes int maxPixels int } func NewAttachmentService( q attachmentQueries, s3 *s3store.Client, maxBytes, maxPixels int, ) *AttachmentService { return &AttachmentService{ q: q, s3: s3, maxBytes: maxBytes, maxPixels: maxPixels, } } func (s *AttachmentService) Upload( ctx context.Context, userID, chatID uuid.UUID, filename string, body []byte, contentType string, ) (model.Attachment, error) { if len(body) > s.maxBytes { return model.Attachment{}, ErrInvalidSize } if err := s.ensureMember(ctx, userID, chatID); err != nil { return model.Attachment{}, err } detectedType, err := s.checkImage(body, contentType) if err != nil { return model.Attachment{}, err } id, err := uuid.NewV7() if err != nil { return model.Attachment{}, err } sum := sha256.Sum256(body) hash := hex.EncodeToString(sum[:]) key := "chats/" + chatID.String() + "/attachments/" + id.String() if err := s.s3.Put(ctx, key, bytes.NewReader(body), int64(len(body)), string(detectedType)); err != nil { return model.Attachment{}, err } row, err := s.q.CreateAttachment(ctx, db.CreateAttachmentParams{ ID: id, ChatID: chatID, OwnerID: userID, S3Key: key, Filename: filename, ContentType: db.AttachmentContentType(detectedType), SizeBytes: len(body), Sha256: hash, Status: db.AttachmentStatusReady, ErrorCode: "", }) if err != nil { return model.Attachment{}, err } return model.Attachment{ ID: row.ID, ChatID: row.ChatID, OwnerID: row.OwnerID, Filename: row.Filename, ContentType: model.ContentType(row.ContentType), SizeBytes: row.SizeBytes, Sha256: row.Sha256, Status: model.AttachmentStatus(row.Status), ErrorCode: row.ErrorCode, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt, }, nil } func (s *AttachmentService) Get( ctx context.Context, userID, attachmentID uuid.UUID, ) (model.Attachment, error) { row, err := s.q.GetAttachmentByID(ctx, attachmentID) if postgres.IsNotFound(err) { return model.Attachment{}, ErrAttachmentNotFound } if err != nil { return model.Attachment{}, err } if err := s.ensureMember(ctx, userID, row.ChatID); err != nil { return model.Attachment{}, err } return model.Attachment{ ID: row.ID, ChatID: row.ChatID, OwnerID: row.OwnerID, Filename: row.Filename, ContentType: model.ContentType(row.ContentType), SizeBytes: row.SizeBytes, Sha256: row.Sha256, Status: model.AttachmentStatus(row.Status), ErrorCode: row.ErrorCode, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt, }, nil } func (s *AttachmentService) GetContent( ctx context.Context, userID, attachmentID uuid.UUID, ) ([]byte, model.ContentType, error) { row, err := s.q.GetAttachmentByID(ctx, attachmentID) if postgres.IsNotFound(err) { return nil, "", ErrAttachmentNotFound } if err != nil { return nil, "", err } if err := s.ensureMember(ctx, userID, row.ChatID); err != nil { return nil, "", err } out, err := s.s3.Get(ctx, row.S3Key) if err != nil { return nil, "", err } defer out.Body.Close() data, err := io.ReadAll(out.Body) if err != nil { return nil, "", err } return data, model.ContentType(row.ContentType), nil } func (s *AttachmentService) checkImage(data []byte, contentType string) (model.ContentType, error) { cfg, format, err := image.DecodeConfig(bytes.NewReader(data)) if err != nil { return "", ErrInvalidImage } if cfg.Width*cfg.Height > s.maxPixels { return "", ErrInvalidSize } switch format { case "png": if contentType != string(model.ContentTypePNG) { return "", ErrInvalidImage } return model.ContentTypePNG, nil case "jpeg": if contentType != string(model.ContentTypeJPEG) { return "", ErrInvalidImage } return model.ContentTypeJPEG, nil default: return "", ErrInvalidImage } } func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID uuid.UUID) error { member, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{ ChatID: chatID, UserID: userID, }) if err != nil { return err } if !member { return ErrAttachmentNotFound } return nil }