/
ocr.go
95 lines (79 loc) · 2.44 KB
/
ocr.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
package ocr
import (
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"strings"
"github.com/dudakovict/gocr/conf"
"github.com/otiai10/gosseract/v2"
)
// GosseractClient is an interface matching the methods used by gosseract.Client
type GosseractClient interface {
SetImageFromBytes(content []byte) error
Text() (string, error)
Close() error
}
// OCRProcessor holds the OCR client and other related functionality
type OCRProcessor struct {
gosseractClient GosseractClient
logger *log.Logger
}
// NewOCRProcessor initializes a new OCRProcessor
func NewOCRProcessor(logger *log.Logger) *OCRProcessor {
return &OCRProcessor{
gosseractClient: gosseract.NewClient(),
logger: logger,
}
}
// UploadHandler handles the file upload and text extraction
func (ocr *OCRProcessor) UploadHandler(w http.ResponseWriter, r *http.Request, cfg conf.Config) {
r.ParseMultipartForm(int64(cfg.MaxFileSizeMB) << 20)
file, handler, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
ocr.logger.Printf("Error retrieving the file: %s", err)
return
}
defer file.Close()
if !isImage(handler) {
http.Error(w, "Invalid file format. Only image files are allowed.", http.StatusBadRequest)
ocr.logger.Println("Invalid file format. Only image files are allowed.")
return
}
fileBytes, err := io.ReadAll(file)
if err != nil {
http.Error(w, "Error reading the file", http.StatusInternalServerError)
ocr.logger.Printf("Error reading the file: %s", err)
return
}
if err := ocr.gosseractClient.SetImageFromBytes(fileBytes); err != nil {
http.Error(w, "Error setting image from bytes", http.StatusInternalServerError)
ocr.logger.Printf("Error setting image from bytes: %s", err)
return
}
text, err := ocr.gosseractClient.Text()
if err != nil {
http.Error(w, fmt.Sprintf("Error extracting text: %s", err), http.StatusInternalServerError)
ocr.logger.Printf("Error extracting text: %s", err)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, text)
}
// Close closes any resources held by the OCRProcessor
func (ocr *OCRProcessor) Close() error {
return ocr.gosseractClient.Close()
}
// isImage checks if the given file is an image
func isImage(fileHeader *multipart.FileHeader) bool {
if fileHeader == nil {
return false
}
contentType := fileHeader.Header.Get("Content-Type")
if contentType == "" {
return false
}
return strings.HasPrefix(strings.ToLower(contentType), "image/")
}