forked from mtusi2026/mtusi2026lab5
feat(attachments): add pipeline and outbox schema and queries
This commit is contained in:
@@ -98,7 +98,7 @@ func (s *AttachmentService) Upload(
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
detectedType, err := s.checkImage(ctx, tmp, contentType)
|
||||
detectedType, width, height, err := s.checkImage(ctx, tmp, contentType)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
@@ -124,6 +124,8 @@ func (s *AttachmentService) Upload(
|
||||
ContentType: db.AttachmentContentType(detectedType),
|
||||
SizeBytes: int(size),
|
||||
Sha256: hash,
|
||||
Width: width,
|
||||
Height: height,
|
||||
ErrorCode: "",
|
||||
})
|
||||
if postgres.IsNotFound(err) {
|
||||
@@ -222,52 +224,52 @@ func (s *AttachmentService) GetContent(
|
||||
return data, model.ContentType(row.ContentType), nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, error) {
|
||||
func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, int, int, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 {
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
if cfg.Width > s.maxPixels/cfg.Height {
|
||||
return "", ErrInvalidSize
|
||||
return "", 0, 0, ErrInvalidSize
|
||||
}
|
||||
|
||||
var detectedType model.ContentType
|
||||
switch format {
|
||||
case "png":
|
||||
if contentType != string(model.ContentTypePNG) {
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
detectedType = model.ContentTypePNG
|
||||
case "jpeg":
|
||||
if contentType != string(model.ContentTypeJPEG) {
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
detectedType = model.ContentTypeJPEG
|
||||
default:
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
select {
|
||||
case s.decodeSlot <- struct{}{}:
|
||||
defer func() { <-s.decodeSlot }()
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
return "", 0, 0, ctx.Err()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
return "", 0, 0, err
|
||||
}
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return "", err
|
||||
return "", 0, 0, err
|
||||
}
|
||||
if _, _, err := image.Decode(r); err != nil {
|
||||
return "", ErrInvalidImage
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
return detectedType, nil
|
||||
return detectedType, cfg.Width, cfg.Height, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID uuid.UUID) error {
|
||||
|
||||
Reference in New Issue
Block a user