-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
135 lines (118 loc) · 3.22 KB
/
handlers.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
package main
import (
"fmt"
"log"
"net/http"
"path"
"strconv"
"github.com/gin-gonic/gin"
"github.com/miaomiaotech/photobox-lite/imageupload"
"github.com/miaomiaotech/photobox-lite/storage"
)
// APIUpload and generate thumbnail
func APIUpload(c *gin.Context) {
pw := c.Request.FormValue("password")
if pw != PASSWORD {
c.JSON(http.StatusUnauthorized, gin.H{"error": "password incorrect"})
return
}
img, err := imageupload.Process(c.Request, "file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// check cache
cacheRes := UploadResponse{}
err = CacheGet(c.Request.Context(), img.Md5, &cacheRes)
if err == nil {
if storage.Exist(cacheRes.Image.Path) && storage.Exist(cacheRes.Thumb.Path) {
log.Printf("hit cache %v", img.Md5)
c.JSON(http.StatusOK, cacheRes)
return
}
}
// limit the origin size
if img.ContentType == "image/jpeg" {
log.Printf("compress origin %s", img.Filename)
img, err = imageupload.Thumbnail(img, maxWidthOrHeight, maxWidthOrHeight, maxQuality)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}
width, height, quality := getThumbParams(c)
thumbnailImage, err := imageupload.Thumbnail(img, width, height, quality)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
pathAndUrl := genImgPathUrl(img.Md5, img.Format)
thumbnailImage.Filename = path.Base(pathAndUrl.ThumbPath)
// save origin image
err = saveImage(DataDir, pathAndUrl.OriginPath, img)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// save thumbnail image
err = saveImage(DataDir, pathAndUrl.ThumbPath, thumbnailImage)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// save redis cache
res := UploadResponse{
Image: img,
Thumb: thumbnailImage,
Data: &pathAndUrl,
}
err = CacheSet(img.Md5, &res)
if err != nil {
log.Println(err)
}
if uploadCallback != "" {
go func() {
code, stdout, stderr := RunSimpleCommand(fmt.Sprintf("bash -c '%s'", uploadCallback))
log.Printf("exit: %d", code)
log.Printf("stdout: %s", stdout)
log.Printf("stderr: %s", stderr)
}()
}
c.JSON(http.StatusOK, res)
}
func APIThumbnail(c *gin.Context) {
img, err := imageupload.Process(c.Request, "file")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
width, height, quality := getThumbParams(c)
t, err := imageupload.Thumbnail(img, width, height, quality)
t.WriteResponse(c.Writer)
}
func saveImage(dir, keyPath string, img *imageupload.Image) error {
fp := path.Join(dir, keyPath)
local := storage.LocalStorage{Img: img}
return storage.SaveTo(&local, fp)
}
func getThumbParams(c *gin.Context) (int, int, int) {
width := defaultThumbMaxWidth
height := defaultThumbMaxHeight
quality := defaultQuality
if str, ok := c.GetQuery("width"); ok {
if v, e := strconv.Atoi(str); e == nil {
width = v
}
}
if str, ok := c.GetQuery("height"); ok {
if v, e := strconv.Atoi(str); e == nil {
height = v
}
}
if str, ok := c.GetQuery("quality"); ok {
if v, e := strconv.Atoi(str); e == nil {
quality = v
}
}
return width, height, quality
}