fix(api): outpout filename as path - fixes #1227

This commit is contained in:
Julien Neuhart
2025-06-06 15:14:06 +00:00
parent b0a84bcbf7
commit aa58615650
4 changed files with 55 additions and 1 deletions

View File

@@ -462,6 +462,7 @@ func (a *Api) Start() error {
latencyMiddleware(),
rootPathMiddleware(a.rootPath),
traceMiddleware(a.traceHeader),
outputFilenameMiddleware(),
loggerMiddleware(a.logger, disableLoggingForPaths),
)

View File

@@ -513,7 +513,7 @@ func (ctx *Context) BuildOutputFile() (string, error) {
// OutputFilename returns the filename based on the given output path or the
// "Gotenberg-Output-Filename" header's value.
func (ctx *Context) OutputFilename(outputPath string) string {
filename := ctx.echoCtx.Request().Header.Get("Gotenberg-Output-Filename")
filename := ctx.echoCtx.Get("outputFilename").(string)
if filename == "" {
return filepath.Base(outputPath)

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"
"strings"
"time"
@@ -150,6 +151,25 @@ func traceMiddleware(header string) echo.MiddlewareFunc {
}
}
// outputFilenameMiddleware sets the output filename in the [echo.Context]
// under "outputFilename".
//
// outputFilename := c.Get("outputFilename").(string)
func outputFilenameMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
filename := c.Request().Header.Get("Gotenberg-Output-Filename")
// See https://github.com/gotenberg/gotenberg/issues/1227.
if filename != "" {
filename = filepath.Base(filename)
}
c.Set("outputFilename", filename)
// Call the next middleware in the chain.
return next(c)
}
}
}
// loggerMiddleware sets the logger in the [echo.Context] under "logger" and
// logs a synchronous request result.
//