-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimageProcessing.go
161 lines (131 loc) · 3.28 KB
/
imageProcessing.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"golang.org/x/net/html"
)
func getImageFromPost(postJSON string) ([]string, error) {
var post map[string]interface{}
if err := json.Unmarshal([]byte(postJSON), &post); err != nil {
log.Error("Error during JSON unmarshaling:", err)
}
htmlContent := ""
if cards, ok := post["cards"].([]interface{}); ok {
if len(cards) > 0 {
if cardData, ok := cards[0].([]interface{}); ok && len(cardData) > 1 {
if cardHtml, ok := cardData[1].(map[string]interface{}); ok {
if htmlStr, ok := cardHtml["html"].(string); ok {
htmlContent = htmlStr
}
}
}
}
}
imageList := []string{}
z := html.NewTokenizer(strings.NewReader(htmlContent))
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
if tt == html.StartTagToken {
t := z.Token()
if t.Data == "img" {
for _, attr := range t.Attr {
if attr.Key == "src" {
imageList = append(imageList, attr.Val)
break
}
}
}
}
}
return imageList, nil
}
func imageToHash(image string) (string, string, error) {
var tp string // Temporary file path
var err error
var fileExtension string
var hashValue string
if strings.HasPrefix(image, "http://") || strings.HasPrefix(image, "https://") {
iext := filepath.Ext(image)
// fmt.Println("iext", iext)
tempDir := getTempDir()
tp = filepath.Join(tempDir, "img"+iext)
resp, err := http.Get(image)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
outFile, err := os.Create(tp)
if err != nil {
return "", "", err
}
defer outFile.Close()
_, err = io.Copy(outFile, resp.Body)
if err != nil {
return "", "", err
}
// Create the "images" directory if it doesn't exist
if opts.DownloadImage {
if !strings.Contains(image, "https://karma-src-x02msdf8-23.s3.ap-south-1.amazonaws.com/product-menu-logo") {
err = os.MkdirAll("images", os.ModePerm)
if err != nil {
return "", "", err
}
// Copy the temporary file into the "images" directory
fileName := filepath.Base(image)
destination := filepath.Join("images", fileName)
if err := copyFile(image,tp, destination); err != nil {
return "", "", err
}
}
}
fileExtension = iext
hashValue, err = sha256Sum(tp)
} else {
// fmt.Println("Downloading image:", image)
hashValue, err = sha256Sum(image)
fileExtension = filepath.Ext(image)
}
if err != nil {
return "", "", err
}
imageName := hashValue + fileExtension
return imageName, tp, nil
}
func getTempDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("TEMP")
}
return "/tmp/"
}
func copyFile(image,src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
newContent := strings.Replace(string(content),image, dst, -1)
err = ioutil.WriteFile(filePath, []byte(newContent), 0644)
if err != nil {
return err
}
return err
}