forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_http_multipart_handler.go
118 lines (87 loc) · 2.58 KB
/
ocr_http_multipart_handler.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
package ocrworker
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"strings"
"github.com/couchbaselabs/logg"
)
type OcrHttpMultipartHandler struct {
RabbitConfig RabbitConfig
}
func NewOcrHttpMultipartHandler(r RabbitConfig) *OcrHttpMultipartHandler {
return &OcrHttpMultipartHandler{
RabbitConfig: r,
}
}
func (s *OcrHttpMultipartHandler) extractParts(req *http.Request) (OcrRequest, error) {
logg.LogTo("OCR_HTTP", "request to ocr-file-upload")
ocrReq := OcrRequest{}
switch req.Method {
case "POST":
h := req.Header.Get("Content-Type")
logg.LogTo("OCR_HTTP", "content type: %v", h)
contentType, attrs, _ := mime.ParseMediaType(req.Header.Get("Content-Type"))
logg.LogTo("OCR_HTTP", "content type: %v", contentType)
if !strings.HasPrefix(h, "multipart/related") {
return ocrReq, fmt.Errorf("Expected multipart related")
}
reader := multipart.NewReader(req.Body, attrs["boundary"])
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
contentTypeOuter := part.Header["Content-Type"][0]
contentType, attrs, _ := mime.ParseMediaType(contentTypeOuter)
logg.LogTo("OCR_HTTP", "attrs: %v", attrs)
switch contentType {
case "application/json":
decoder := json.NewDecoder(part)
err := decoder.Decode(&ocrReq)
if err != nil {
return ocrReq, fmt.Errorf("Unable to unmarshal json: %s", err)
}
part.Close()
default:
if !strings.HasPrefix(contentType, "image") {
return ocrReq, fmt.Errorf("Expected content-type: image/*")
}
partContents, err := ioutil.ReadAll(part)
if err != nil {
return ocrReq, fmt.Errorf("Failed to read mime part: %v", err)
}
ocrReq.ImgBytes = partContents
return ocrReq, nil
}
}
return ocrReq, fmt.Errorf("Didn't expect to get this far")
default:
return ocrReq, fmt.Errorf("This endpoint only accepts POST requests")
}
}
func (s *OcrHttpMultipartHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
ocrRequest, err := s.extractParts(req)
if err != nil {
logg.LogError(err)
errStr := fmt.Sprintf("Error extracting multipart/related parts: %v", err)
http.Error(w, errStr, 500)
return
}
logg.LogTo("OCR_HTTP", "ocrRequest: %v", ocrRequest)
ocrResult, err := HandleOcrRequest(ocrRequest, s.RabbitConfig)
if err != nil {
msg := "Unable to perform OCR decode. Error: %v"
errMsg := fmt.Sprintf(msg, err)
logg.LogError(fmt.Errorf(errMsg))
http.Error(w, errMsg, 500)
return
}
logg.LogTo("OCR_HTTP", "ocrResult: %v", ocrResult)
fmt.Fprintf(w, ocrResult.Text)
}