mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
feat(chromium): deprecate --chromium-user-agent property, add userAgent form field
This commit is contained in:
@@ -61,6 +61,10 @@ type Chromium struct {
|
||||
|
||||
// Options are the available options for converting HTML document to PDF.
|
||||
type Options struct {
|
||||
// UserAgent overrides the default User-Agent header.
|
||||
// Optional.
|
||||
UserAgent string
|
||||
|
||||
// WaitDelay is the duration to wait when loading an HTML document before
|
||||
// converting it to PDF.
|
||||
// Optional.
|
||||
@@ -143,6 +147,7 @@ type Options struct {
|
||||
// DefaultOptions returns the default values for Options.
|
||||
func DefaultOptions() Options {
|
||||
return Options{
|
||||
UserAgent: "",
|
||||
WaitDelay: 0,
|
||||
WaitWindowStatus: "",
|
||||
ExtraHTTPHeaders: nil,
|
||||
@@ -194,6 +199,11 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor {
|
||||
fs.String("chromium-deny-list", "^file:///[^tmp].*", "Set the denied URLs for Chromium using a regular expression")
|
||||
fs.Bool("chromium-disable-routes", false, "Disable the routes")
|
||||
|
||||
err := fs.MarkDeprecated("chromium-user-agent", "use the userAgent form field instead")
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("create deprecated flags for chromium module: %v", err))
|
||||
}
|
||||
|
||||
return fs
|
||||
}(),
|
||||
New: func() gotenberg.Module { return new(Chromium) },
|
||||
@@ -302,7 +312,8 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath
|
||||
chromedp.UserDataDir(userProfileDirPath),
|
||||
)
|
||||
|
||||
if mod.userAgent != "" {
|
||||
if mod.userAgent != "" && options.UserAgent == "" {
|
||||
// Deprecated.
|
||||
args = append(args, chromedp.UserAgent(mod.userAgent))
|
||||
}
|
||||
|
||||
@@ -328,6 +339,10 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath
|
||||
args = append(args, chromedp.ProxyServer(mod.proxyServer))
|
||||
}
|
||||
|
||||
if options.UserAgent != "" {
|
||||
args = append(args, chromedp.UserAgent(options.UserAgent))
|
||||
}
|
||||
|
||||
allocatorCtx, cancel := chromedp.NewExecAllocator(ctx, args...)
|
||||
defer cancel()
|
||||
|
||||
|
||||
@@ -250,6 +250,12 @@ func TestChromium_PDF(t *testing.T) {
|
||||
denyList: regexp.MustCompile("file:///tests/*"),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
URL: "file:///tests/test/testdata/chromium/html/sample4/index.html",
|
||||
options: Options{
|
||||
UserAgent: "foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
URL: "file:///tests/test/testdata/chromium/html/sample4/index.html",
|
||||
options: Options{
|
||||
|
||||
@@ -26,6 +26,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
||||
defaultOptions := DefaultOptions()
|
||||
|
||||
var (
|
||||
userAgent string
|
||||
waitDelay time.Duration
|
||||
waitWindowStatus string
|
||||
extraHTTPHeaders map[string]string
|
||||
@@ -38,6 +39,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
||||
)
|
||||
|
||||
form := ctx.FormData().
|
||||
String("userAgent", &userAgent, defaultOptions.UserAgent).
|
||||
Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay).
|
||||
String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus).
|
||||
Custom("extraHttpHeaders", func(value string) error {
|
||||
@@ -69,6 +71,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
||||
Bool("preferCssPageSize", &preferCSSPageSize, defaultOptions.PreferCSSPageSize)
|
||||
|
||||
options := Options{
|
||||
UserAgent: userAgent,
|
||||
WaitDelay: waitDelay,
|
||||
WaitWindowStatus: waitWindowStatus,
|
||||
ExtraHTTPHeaders: extraHTTPHeaders,
|
||||
|
||||
Reference in New Issue
Block a user