forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_util.go
61 lines (45 loc) · 1.02 KB
/
ocr_util.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
package ocrworker
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/nu7hatch/gouuid"
)
func saveUrlContentToFileName(url, tmpFileName string) error {
// TODO: current impl uses more memory than it needs to
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return ioutil.WriteFile(tmpFileName, bodyBytes, 0600)
}
func saveBytesToFileName(bytes []byte, tmpFileName string) error {
return ioutil.WriteFile(tmpFileName, bytes, 0600)
}
func url2bytes(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bodyBytes, nil
}
func createTempFileName() (string, error) {
tempDir := os.TempDir()
uuidRaw, err := uuid.NewV4()
if err != nil {
return "", err
}
uuidStr := uuidRaw.String()
return filepath.Join(tempDir, uuidStr), nil
}