forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocr_http_multipart_handler.go
116 lines (93 loc) · 2.82 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
package ocrworker
import (
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"strings"
"github.com/rs/zerolog/log"
)
type OcrHttpMultipartHandler struct {
RabbitConfig RabbitConfig
}
func NewOcrHttpMultipartHandler(r *RabbitConfig) *OcrHttpMultipartHandler {
return &OcrHttpMultipartHandler{
RabbitConfig: *r,
}
}
func (*OcrHttpMultipartHandler) extractParts(req *http.Request) (OcrRequest, error) {
log.Info().Str("component", "OCR_HTTP").Msg("request to ocr-file-upload")
ocrReq := OcrRequest{}
switch req.Method {
case "POST":
h := req.Header.Get("Content-Type")
log.Info().Str("component", "OCR_HTTP").
Str("content_type", h).
Msg("content type")
contentType, attrs, _ := mime.ParseMediaType(req.Header.Get("Content-Type"))
log.Info().Str("component", "OCR_HTTP").
Str("content_type", contentType).
Msg("content type")
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)
log.Info().Str("component", "OCR_HTTP").Interface("attrs", 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 := io.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 func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Warn().Err(err).Caller().Str("component", "OCR_HTTP").Msg(req.RequestURI + " request Body could not be removed")
}
}(req.Body)
httpStatus := 200
ocrRequest, err := s.extractParts(req)
if err != nil {
log.Error().Err(err).Str("component", "OCR_HTTP")
errStr := fmt.Sprintf("Error extracting multipart/related parts: %v", err)
http.Error(w, errStr, 500)
return
}
ocrResult, httpStatus, err := HandleOcrRequest(&ocrRequest, &s.RabbitConfig)
if err != nil {
msg := "Unable to perform OCR decode."
log.Error().Err(err).Str("component", "OCR_HTTP").Msg(msg)
http.Error(w, msg, httpStatus)
return
}
_, _ = fmt.Fprintf(w, ocrResult.Text)
}