diff --git a/internal/util/posts.go b/internal/util/posts.go index 9c3e8d1..8951368 100644 --- a/internal/util/posts.go +++ b/internal/util/posts.go @@ -261,17 +261,3 @@ func FormatFileInfo(fileInfo PostFileInfo) string { } return fmt.Sprintf("(%s, %dx%d)", humanSize, fileInfo.Width, fileInfo.Height) } - -func IsMediaTooLarge(file multipart.File) (bool, error) { - limitedReader := io.LimitReader(file, FILE_MEM_LIMIT+1) - fileBytes, err := io.ReadAll(limitedReader) - if err != nil { - return true, err - } - - if int64(len(fileBytes)) > FILE_MEM_LIMIT { - return true, nil - } - - return false, nil -} diff --git a/web/main.go b/web/main.go index 2ed93bd..1317693 100644 --- a/web/main.go +++ b/web/main.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "log" + "mime/multipart" "net/http" "path/filepath" "strconv" @@ -142,6 +143,16 @@ func main() { // parse form r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES) if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil { + var maxErr *http.MaxBytesError + if errors.As(err, &maxErr) { + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) + return + } + + if errors.Is(err, multipart.ErrMessageTooLarge) { + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) + return + } http.Error(w, "Failed to parse form", http.StatusBadRequest) log.Printf("ParseMultipartForm: %v", err) return @@ -175,19 +186,7 @@ func main() { defer file.Close() if header.Size > util.FILE_MEM_LIMIT { - http.Error(w, "File too large (max 10 MB)", http.StatusRequestEntityTooLarge) - return - } - - isMediaTooLarge, err := util.IsMediaTooLarge(file) - if err != nil { - http.Error(w, "Failed to detect if file is too large", http.StatusInternalServerError) - return - } - file.Seek(0, io.SeekStart) - - if isMediaTooLarge { - http.Error(w, "File too large (max 10MB)", http.StatusRequestEntityTooLarge) + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) return } @@ -237,6 +236,16 @@ func main() { // parse form r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES) if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil { + var maxErr *http.MaxBytesError + if errors.As(err, &maxErr) { + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) + return + } + + if errors.Is(err, multipart.ErrMessageTooLarge) { + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) + return + } http.Error(w, "Failed to parse form", http.StatusBadRequest) log.Printf("ParseMultipartForm: %v", err) return @@ -268,19 +277,7 @@ func main() { defer file.Close() if header.Size > util.FILE_MEM_LIMIT { - http.Error(w, "File too large (max 10 MB)", http.StatusRequestEntityTooLarge) - return - } - - isMediaTooLarge, err := util.IsMediaTooLarge(file) - if err != nil { - http.Error(w, "Failed to detect if file is too large", http.StatusInternalServerError) - return - } - file.Seek(0, io.SeekStart) - - if isMediaTooLarge { - http.Error(w, "File too large (max 5MB)", http.StatusRequestEntityTooLarge) + http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge) return }