-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
161 lines (136 loc) · 3.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"fmt"
"io"
"mime"
"net/http"
"os"
"os/exec"
"pdf-api/logs"
"time"
"github.com/gofiber/fiber/v2"
)
var httpClient = http.Client{
Timeout: 600 * time.Second,
}
func main() {
appBasePath := os.Getenv("API_ROOT")
app := fiber.New(fiber.Config{
BodyLimit: 250 * 1024 * 1024,
})
router := app.Group(appBasePath)
router.Get("/health", func(c *fiber.Ctx) error {
return c.JSON(map[string]interface{}{
"staus": "ok",
})
})
router.Post("/*", func(c *fiber.Ctx) error {
/*
Match all route and send to gotenburg
*/
reqBody := c.Body()
reqContentType := string(c.Request().Header.ContentType())
reqURI := string(c.Request().RequestURI())
//Send request to gotenberg
gotenbergUrl := "http://gotenberg:3000" + reqURI
response, responseHeaders, err := proxyToGotenberg(gotenbergUrl, reqContentType, reqBody)
if err != nil {
return c.JSON(map[string]interface{}{
"error": err,
})
}
defer response.Close()
//Set with header from gotenberg response
for k, v := range responseHeaders {
c.Set(k, v)
}
pdfFileName := ""
originalPdfFilePath := ""
//Get current pdf file name
for k, v := range responseHeaders {
if k == "Content-Disposition" {
//Get pdf filename for create tmp file
_, params, _ := mime.ParseMediaType(v)
pdfFileName = params["filename"]
}
}
if pdfFileName != "" {
_, err := os.Stat("tmp")
if os.IsNotExist(err) {
//Create tmp folder if not exist
err := os.Mkdir("tmp", 0700)
if err != nil {
logs.Error(fmt.Sprintf("create tmp folder error, pdf_name=%s, error=%s", pdfFileName, err))
}
}
originalPdfFilePath = "tmp/" + pdfFileName
}
if originalPdfFilePath != "" {
//Write file for return or compress it before return
//open a file for writing
file, err := os.Create(originalPdfFilePath)
if err != nil {
logs.Error(fmt.Sprintf("create empty tmp_file err, pdf_name=%s, error=%s", pdfFileName, err))
return c.JSON(map[string]interface{}{
"error": err,
})
}
defer file.Close()
_, err = io.Copy(file, response)
if err != nil {
logs.Error(fmt.Sprintf("save tmp_file err, pdf_name=%s, error=%s", pdfFileName, err))
return c.JSON(map[string]interface{}{
"error": err,
})
}
defer func() {
//Delete original file
err = os.Remove(originalPdfFilePath)
if err != nil {
logs.Error(fmt.Sprintf("cannot remove tmp_file err, pdf_name=%s, error=%s", pdfFileName, err))
}
}()
}
if pdfFileName != "" {
//Check compressedFlag
if c.FormValue("compressPdf") == "1" {
//Compress pdf with ghostscript
compressPdfFilePath := "tmp/compress_" + pdfFileName
pdfDpi := 350
out, err := exec.Command(`/bin/sh`, `-c`, fmt.Sprintf(`sh ./scripts/shrinkpdf.sh %s %s %d`, originalPdfFilePath, compressPdfFilePath, pdfDpi)).Output()
if err != nil {
logs.Error(fmt.Sprintf("compress pdf err, pdf_name=%s, out=%s, error=%s", pdfFileName, out, err))
return c.JSON(map[string]interface{}{
"error": err,
})
}
defer func() {
//Delete compress file
err = os.Remove(compressPdfFilePath)
if err != nil {
logs.Error(fmt.Sprintf("cannot remove tmp_file err, pdf_name=%s, error=%s", compressPdfFilePath, err))
}
}()
//Read compress pdf byte
return c.SendFile(compressPdfFilePath)
} else {
return c.SendFile(originalPdfFilePath)
}
}
return c.SendStream(response)
})
app.Listen(":3000")
}
func proxyToGotenberg(gotenbergUrl string, requestContentType string, requestBody []byte) (io.ReadCloser, map[string]string, error) {
resp, err := httpClient.Post(gotenbergUrl, requestContentType, bytes.NewReader(requestBody))
if err != nil {
// handle error
logs.Error(fmt.Sprintf("send to gotenburg error=%s", err))
}
responseHeaders := map[string]string{}
for k, v := range resp.Header {
responseHeaders[k] = v[0]
}
return resp.Body, responseHeaders, err
}