This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convserver.go
293 lines (269 loc) · 6.41 KB
/
convserver.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
bugsnag "github.com/bugsnag/bugsnag-go"
)
var pdfInfoRegexp = regexp.MustCompile("Page size:\\s+(\\d+) x (\\d+) pts")
type requestPayload struct {
Bucket string `json:"bucket"`
Key string `json:"key"`
CallbackURL string `json:"callback_url"`
CallbackHTTPMethod string `json:"callback_method,omitempty"`
}
type responsePayload struct {
Status string `json:"status"`
Thumbnails thumbnailsResponsePayload `json:"thumbnails"`
}
type thumbnailsResponsePayload struct {
Preview fileResponsePayload `json:"preview"`
}
type fileResponsePayload struct {
ContentHash string `json:"content_hash"`
ContentType string `json:"content_type"`
ContentSize int `json:"content_size"`
Width int `json:"width"`
Height int `json:"height"`
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
releaseStage := os.Getenv("ENV")
if releaseStage == "" {
releaseStage = "development"
}
if bugsnagKey := os.Getenv("BUGSNAG_API_KEY"); bugsnagKey != "" {
bugsnag.Configure(bugsnag.Configuration{
APIKey: bugsnagKey,
ReleaseStage: releaseStage,
})
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "We don't accept "+r.Method+" requests", http.StatusBadRequest)
return
}
var req requestPayload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer r.Body.Close()
go runCommand(req)
fmt.Fprintf(w, "OK")
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
err := http.ListenAndServe(":"+port, nil)
bugsnag.Notify(err)
log.Fatal(err)
}
func convertPreiviewKey(orgKey string) string {
ext := filepath.Ext(orgKey)
suffix := ext
if ext != "" {
suffix = ".pdf"
}
return strings.TrimSuffix(orgKey, ext) + "-preview" + suffix
}
// http://dev.pawelsz.eu/2014/11/google-golang-compute-md5-of-file.html
func computeMd5(filePath string) ([]byte, error) {
var result []byte
file, err := os.Open(filePath)
if err != nil {
return result, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return result, err
}
return hash.Sum(result), nil
}
func responseJSONFromFile(file *os.File) ([]byte, error) {
hashBytes, err := computeMd5(file.Name())
if err != nil {
return []byte{}, err
}
hash := hex.EncodeToString(hashBytes)
if hash == "" {
hash = "0"
}
fi, err := file.Stat()
if err != nil {
return []byte{}, err
}
size := int(fi.Size())
w, h, err := pdfSize(file.Name())
if err != nil {
return []byte{}, err
}
payload := responsePayload{
Status: "completed",
Thumbnails: thumbnailsResponsePayload{
Preview: fileResponsePayload{
ContentType: "application/pdf",
ContentHash: hash,
ContentSize: size,
Width: w,
Height: h,
},
},
}
b, err := json.Marshal(&payload)
if err != nil {
return []byte{}, err
}
return b, nil
}
func runWriter(filename string) error {
cmd := exec.Command("lowriter",
"--invisible",
"--convert-to",
"pdf:writer_pdf_Export",
"--outdir",
filepath.Dir(filename),
filename)
err := cmd.Start()
if err != nil {
return err
}
timeoutSeconds, err := strconv.Atoi(os.Getenv("CMD_TIMEOUT_SECONDS"))
if err != nil {
timeoutSeconds = 60
}
timer := time.AfterFunc(time.Second*time.Duration(timeoutSeconds), func() {
cmd.Process.Kill()
})
err = cmd.Wait()
timer.Stop()
return err
}
func pdfSize(filename string) (int, int, error) {
bin := os.Getenv("PDF_INFO_PATH")
if bin == "" {
bin = "pdfinfo"
}
cmd := exec.Command(bin, filename)
out, err := cmd.Output()
if err != nil {
return 0, 0, err
}
m := pdfInfoRegexp.FindAllStringSubmatch(string(out), 1)
if len(m) == 0 {
return 0, 0, errors.New("Invalid pdfinfo output")
}
line := m[0]
w, _ := strconv.Atoi(line[1])
h, _ := strconv.Atoi(line[2])
return w, h, nil
}
func sendCallback(method string, url string, json []byte) error {
if method == "" {
method = "POST"
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(json))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
status := res.StatusCode
if !(status >= 200 && status < 300) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return fmt.Errorf("Error sending callback: %v %v", status, string(body))
}
return err
}
func runCommand(req requestPayload) error {
defer bugsnag.Recover()
bugsnagMetadata := bugsnag.MetaData{
"req": {
"Bucket": req.Bucket,
"Key": req.Key,
"CallbackURL": req.CallbackURL,
"CallbackHTTPMethod": req.CallbackHTTPMethod,
},
}
tmpfile, err := ioutil.TempFile("", strings.Replace(req.Key, "/", "_", -1))
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
sess := session.New()
dl := s3manager.NewDownloader(sess)
fs, err := os.Create(tmpfile.Name())
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
_, err = dl.Download(fs, &s3.GetObjectInput{
Bucket: &req.Bucket,
Key: &req.Key,
})
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
defer os.Remove(tmpfile.Name())
err = runWriter(tmpfile.Name())
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
pdf, err := os.Open(strings.TrimSuffix(tmpfile.Name(), filepath.Ext(tmpfile.Name())) + ".pdf")
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
defer pdf.Close()
destKey := convertPreiviewKey(req.Key)
contentType := "application/pdf"
ul := s3manager.NewUploader(sess)
_, err = ul.Upload(&s3manager.UploadInput{
Bucket: &req.Bucket,
Key: &destKey,
Body: pdf,
ContentType: &contentType,
})
json, err := responseJSONFromFile(pdf)
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
err = sendCallback(req.CallbackHTTPMethod, req.CallbackURL, json)
if err != nil {
bugsnag.Notify(err, bugsnagMetadata)
return err
}
return nil
}