-
Notifications
You must be signed in to change notification settings - Fork 3
/
uploadBlogImages.go
64 lines (54 loc) · 1.61 KB
/
uploadBlogImages.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
package main
import (
"regexp"
"strings"
)
func uploadImages(token, htmlData string) (map[string]string, error) {
uploadedImages := make(map[string]string)
imageBackend := config.GetDefault("image-configuration.IMAGE_BACKEND", "").(string)
blogImageList := []string{}
pattern := `<img[^>]+src="([^"]+)"[^>]*>`
re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(buf.String(), -1)
if imageBackend == "ghost" {
var err error
blogImageList, err = getImageFromPost(htmlData)
if err != nil {
log.Error("Error getting images from post:", err)
}
}
var mdlibImages []string
for _, m := range matches {
if len(m) > 1 {
mdlibImages = append(mdlibImages, m[1])
}
}
for _, image := range mdlibImages {
hashValue, imageData, err := imageToHash(image) // Implement imageToHash function
if err != nil {
log.Error("Error calculating hash:", err)
return nil, err
}
var imageLink string
if strings.HasPrefix(image, "http://") || strings.HasPrefix(image, "https://") {
if imageBackend == "s3" {
imageLink, err = uploadToS3(imageData, hashValue)
} else if imageBackend == "ghost" {
imageLink, err = uploadToGhost(token, imageData, hashValue, blogImageList)
}
} else {
if imageBackend == "s3" {
log.Debug("Uploading image to AWS S3")
imageLink, err = uploadToS3(image, hashValue)
} else if imageBackend == "ghost" {
imageLink, err = uploadToGhost(token, image, hashValue, blogImageList)
log.Debug("Uploading image to Ghost Database")
}
}
if err != nil {
return nil, err
}
uploadedImages[image] = imageLink
}
return uploadedImages, nil
}