forked from mtusi2026/mtusi2026lab4
feat: restricted S3 credentials, orphan reconciler, upload limits (grade 4)
This commit is contained in:
@@ -31,10 +31,21 @@ type AttachmentService struct {
|
||||
store ObjectStore
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
uploads chan struct{}
|
||||
decodes chan struct{}
|
||||
}
|
||||
|
||||
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore, maxBytes, maxPixels int64) *AttachmentService {
|
||||
return &AttachmentService{chats: chats, repo: repo, store: store, maxBytes: maxBytes, maxPixels: maxPixels}
|
||||
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore,
|
||||
maxBytes, maxPixels int64, maxConcurrentUploads, maxConcurrentDecodes int) *AttachmentService {
|
||||
return &AttachmentService{
|
||||
chats: chats,
|
||||
repo: repo,
|
||||
store: store,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
uploads: make(chan struct{}, maxConcurrentUploads),
|
||||
decodes: make(chan struct{}, maxConcurrentDecodes),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatID uuid.UUID, filename, contentType string, body io.Reader) (domain.Attachment, error) {
|
||||
@@ -47,6 +58,10 @@ func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatI
|
||||
if contentType != domain.ContentPNG && contentType != domain.ContentJPEG {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: content type must be image/png or image/jpeg", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
if err := acquire(ctx, s.uploads); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
defer release(s.uploads)
|
||||
raw, err := readLimited(body, s.maxBytes)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
@@ -57,7 +72,7 @@ func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatI
|
||||
if err := checkPixels(raw, s.maxPixels); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := checkIntegrity(raw); err != nil {
|
||||
if err := s.decode(ctx, raw); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
@@ -124,6 +139,25 @@ func (s *AttachmentService) Open(ctx context.Context, actor domain.User, id uuid
|
||||
return attachment, content, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) decode(ctx context.Context, raw []byte) error {
|
||||
if err := acquire(ctx, s.decodes); err != nil {
|
||||
return err
|
||||
}
|
||||
defer release(s.decodes)
|
||||
return checkIntegrity(raw)
|
||||
}
|
||||
|
||||
func acquire(ctx context.Context, sem chan struct{}) error {
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("%w: request cancelled while waiting for capacity", domain.ErrUnavailable)
|
||||
}
|
||||
}
|
||||
|
||||
func release(sem chan struct{}) { <-sem }
|
||||
|
||||
func checkFilename(filename string) error {
|
||||
if n := utf8.RuneCountInString(filename); n < 1 || n > maxFilenameRunes {
|
||||
return fmt.Errorf("%w: filename must be 1 to 255 characters", domain.ErrInvalidInput)
|
||||
|
||||
Reference in New Issue
Block a user