MCPcopy Create free account
hub / github.com/gotenberg/gotenberg / webhookMiddleware

Function webhookMiddleware

pkg/modules/webhook/middleware.go:33–396  ·  view source on GitHub ↗
(w *Webhook)

Source from the content-addressed store, hash-verified

31}
32
33func webhookMiddleware(w *Webhook) api.Middleware {
34 return api.Middleware{
35 Stack: api.MultipartStack,
36 Handler: func() echo.MiddlewareFunc {
37 return func(next echo.HandlerFunc) echo.HandlerFunc {
38 sendOutputFile := func(params sendOutputFileParams) {
39 outputFile, err := os.Open(params.outputPath)
40 if err != nil {
41 params.ctx.Log().Error(fmt.Sprintf("open output file: %s", err))
42 params.handleError(err)
43 return
44 }
45 defer func() {
46 err := outputFile.Close()
47 if err != nil {
48 params.ctx.Log().Error(fmt.Sprintf("close output file: %s", err))
49 }
50 }()
51
52 fileHeader := make([]byte, 512)
53 _, err = outputFile.Read(fileHeader)
54 if err != nil {
55 params.ctx.Log().Error(fmt.Sprintf("read header of output file: %s", err))
56 params.handleError(err)
57 return
58 }
59
60 fileStat, err := outputFile.Stat()
61 if err != nil {
62 params.ctx.Log().Error(fmt.Sprintf("get stat from output file: %s", err))
63 params.handleError(err)
64 return
65 }
66
67 _, err = outputFile.Seek(0, 0)
68 if err != nil {
69 params.ctx.Log().Error(fmt.Sprintf("reset output file reader: %s", err))
70 params.handleError(err)
71 return
72 }
73
74 headers := map[string]string{
75 echo.HeaderContentType: http.DetectContentType(fileHeader),
76 echo.HeaderContentLength: strconv.FormatInt(fileStat.Size(), 10),
77 params.correlationIdHeader: params.correlationId,
78 }
79 _, ok := params.extraHttpHeaders[echo.HeaderContentDisposition]
80 if !ok {
81 headers[echo.HeaderContentDisposition] = fmt.Sprintf("attachment; filename=%q", params.ctx.OutputFilename(params.outputPath))
82 }
83
84 err = params.client.send(params.ctx, bufio.NewReader(outputFile), headers, false)
85 if err != nil {
86 params.ctx.Log().Error(fmt.Sprintf("send output file to webhook: %s", err))
87 params.handleError(err)
88 return
89 }
90

Callers 1

MiddlewaresMethod · 0.85

Calls 15

sendMethod · 0.95
sendEventMethod · 0.95
WrapErrorFunction · 0.92
NewSentinelHttpErrorFunction · 0.92
WithDenyPrivateIPsFunction · 0.92
WithDenyPublicIPsFunction · 0.92
FilterOutboundURLFunction · 0.92
NewOutboundHttpClientFunction · 0.92
NewLeveledLoggerFunction · 0.92
ParseErrorFunction · 0.92
detachAsyncContextFunction · 0.85
newPoolSafeContextFunction · 0.85

Tested by

no test coverage detected