forked from mtusi2026/mtusi2026lab4
fix(attachments): enforce upload body read deadline
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
@@ -24,6 +25,7 @@ var (
|
||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
ErrInvalidImage = errors.New("invalid image")
|
||||
ErrInvalidSize = errors.New("invalid size")
|
||||
ErrUploadReadTimeout = errors.New("upload body read timed out")
|
||||
)
|
||||
|
||||
type attachmentQueries interface {
|
||||
@@ -80,6 +82,10 @@ func (s *AttachmentService) Upload(
|
||||
if errors.As(err, &maxBytesErr) {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
var networkErr net.Error
|
||||
if errors.As(err, &networkErr) && networkErr.Timeout() {
|
||||
return model.Attachment{}, ErrUploadReadTimeout
|
||||
}
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if size > int64(s.maxBytes) {
|
||||
|
||||
Reference in New Issue
Block a user