feat(service):lab4 add AttachmentService scaffold with helpers
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user