mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 21:22:15 +01:00
special case handling: if one file has been sent and it is empty, return a correct invalid HTTP code AND remove the resource folder
This commit is contained in:
@@ -34,9 +34,10 @@ func contextMiddleware(config conf.Config, processes ...pm2.Process) echo.Middle
|
|||||||
// if the endpoint is not for healthcheck, create a
|
// if the endpoint is not for healthcheck, create a
|
||||||
// Resource.
|
// Resource.
|
||||||
if err := ctx.WithResource(trace); err != nil {
|
if err := ctx.WithResource(trace); err != nil {
|
||||||
// required to have a correct status code.
|
err = doCleanup(ctx, err)
|
||||||
ctx.Error(err)
|
err = doErr(ctx, err)
|
||||||
return ctx.LogRequestResult(err, false)
|
return ctx.LogRequestResult(err, false)
|
||||||
|
|
||||||
}
|
}
|
||||||
return next(ctx)
|
return next(ctx)
|
||||||
}
|
}
|
||||||
@@ -62,9 +63,30 @@ func loggerMiddleware() echo.MiddlewareFunc {
|
|||||||
func cleanupMiddleware() echo.MiddlewareFunc {
|
func cleanupMiddleware() echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
const op string = "xhttp.cleanupMiddleware"
|
|
||||||
err := next(c)
|
err := next(c)
|
||||||
ctx := context.MustCastFromEchoContext(c)
|
ctx := context.MustCastFromEchoContext(c)
|
||||||
|
return doCleanup(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// errorMiddleware handles errors (if any).
|
||||||
|
func errorMiddleware() echo.MiddlewareFunc {
|
||||||
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
ctx := context.MustCastFromEchoContext(c)
|
||||||
|
err := next(ctx)
|
||||||
|
if err == nil {
|
||||||
|
// so far so good!
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return doErr(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doCleanup(ctx context.Context, err error) error {
|
||||||
|
const op string = "xhttp.cleanup"
|
||||||
if !ctx.HasResource() {
|
if !ctx.HasResource() {
|
||||||
// nothing to remove.
|
// nothing to remove.
|
||||||
return err
|
return err
|
||||||
@@ -83,20 +105,9 @@ func cleanupMiddleware() echo.MiddlewareFunc {
|
|||||||
ctx.XLogger().ErrorOp(xerror.Op(xerr), xerr)
|
ctx.XLogger().ErrorOp(xerror.Op(xerr), xerr)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// errorMiddleware handles errors (if any).
|
func doErr(ctx context.Context, err error) error {
|
||||||
func errorMiddleware() echo.MiddlewareFunc {
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
||||||
return func(c echo.Context) error {
|
|
||||||
ctx := context.MustCastFromEchoContext(c)
|
|
||||||
err := next(ctx)
|
|
||||||
if err == nil {
|
|
||||||
// so far so good!
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// if it's an error from echo
|
// if it's an error from echo
|
||||||
// like 404 not found and so on.
|
// like 404 not found and so on.
|
||||||
if echoHTTPErr, ok := err.(*echo.HTTPError); ok {
|
if echoHTTPErr, ok := err.(*echo.HTTPError); ok {
|
||||||
@@ -122,6 +133,4 @@ func errorMiddleware() echo.MiddlewareFunc {
|
|||||||
// required to have a correct status code.
|
// required to have a correct status code.
|
||||||
ctx.Error(httpErr)
|
ctx.Error(httpErr)
|
||||||
return httpErr
|
return httpErr
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package context
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@@ -102,6 +104,14 @@ func (ctx *Context) WithResource(directoryName string) error {
|
|||||||
// write form files from request.
|
// write form files from request.
|
||||||
form, err := ctx.MultipartForm()
|
form, err := ctx.MultipartForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
/*
|
||||||
|
(very) special case: one and
|
||||||
|
only one file has been sent
|
||||||
|
and it is empty.
|
||||||
|
*/
|
||||||
|
if strings.Contains(err.Error(), io.EOF.Error()) {
|
||||||
|
return r, xerror.Invalid(op, "one file has been sent but it is empty: does it exist?", err)
|
||||||
|
}
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
for _, files := range form.File {
|
for _, files := range form.File {
|
||||||
|
|||||||
Reference in New Issue
Block a user