From 4a71b690c3b563ad446de68dc0aed90a5095b693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Wed, 21 Oct 2026 17:28:11 +0300 Subject: [PATCH] feat(service):lab4 add AttachmentService scaffold with helpers --- backend/internal/service/attachment.go | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 backend/internal/service/attachment.go diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go new file mode 100644 index 0000000..9f7ebba --- /dev/null +++ b/backend/internal/service/attachment.go @@ -0,0 +1,64 @@ +package service + +import ( + "context" + "strings" + "unicode" + "unicode/utf8" + + "high-loud-apps-labs/internal/domain" +) + +type AttachmentService struct { + attachments domain.AttachmentRepository + chats domain.ChatRepository + storage domain.ObjectStorage + maxBytes int64 + maxPixels int64 +} + +func NewAttachmentService( + attachments domain.AttachmentRepository, + chats domain.ChatRepository, + storage domain.ObjectStorage, + maxBytes, maxPixels int64, +) *AttachmentService { + return &AttachmentService{ + attachments: attachments, + chats: chats, + storage: storage, + maxBytes: maxBytes, + maxPixels: maxPixels, + } +} + +func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return err + } + for _, m := range c.MemberIDs { + if m == userID { + return nil + } + } + return &domain.NotFoundError{Entity: "chat", ID: chatID} +} + +func validateFilename(name string) error { + if name == "" { + return &domain.ValidationError{Field: "filename", Message: "не может быть пустым"} + } + if utf8.RuneCountInString(name) > 255 { + return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"} + } + if strings.ContainsAny(name, "/\\\x00") { + return &domain.ValidationError{Field: "filename", Message: "содержит недопустимые символы"} + } + for _, r := range name { + if unicode.IsControl(r) { + return &domain.ValidationError{Field: "filename", Message: "содержит управляющие символы"} + } + } + return nil +}