-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (193 loc) · 5.7 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"net/url"
"github.com/chai2010/webp"
"github.com/gorilla/mux"
"github.com/nfnt/resize"
)
var (
outputDirectory string
port int
allowedDomains string
contentTypeMap = map[string]string{
"jpeg": "image/jpeg",
"png": "image/png",
"webp": "image/webp",
}
initOnce sync.Once
)
func initConfig() {
initOnce.Do(func() {
flag.StringVar(&outputDirectory, "o", ".", "Output directory for compressed images")
flag.IntVar(&port, "p", 8080, "Port for the server to listen on")
flag.StringVar(&allowedDomains, "s", "*", "Allowed domains separated by comma (,)")
flag.Parse()
})
}
func isDomainAllowed(urlString string) bool {
if allowedDomains == "*" {
return true
}
allowedDomainList := strings.Split(allowedDomains, ",")
parsedURL, err := url.Parse(urlString)
if err != nil {
return false
}
for _, domain := range allowedDomainList {
if strings.HasSuffix(parsedURL.Hostname(), domain) {
return true
}
}
return false
}
func downloadImage(urlString string) (image.Image, string, error) {
resp, err := http.Get(urlString)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
var img image.Image
var format string
switch {
case strings.Contains(contentType, "jpeg"):
img, format, err = image.Decode(resp.Body)
format = "jpeg"
case strings.Contains(contentType, "png"):
img, format, err = image.Decode(resp.Body)
format = "png"
case strings.Contains(contentType, "webp"):
img, err = webp.Decode(resp.Body)
format = "webp"
default:
return nil, "", fmt.Errorf("unsupported image format")
}
if err != nil {
return nil, "", err
}
return img, format, nil
}
func compressImage(img image.Image, format, output string, quality int, resolution string) error {
if resolution != "" {
size := strings.Split(resolution, "x")
width, height := parseResolution(size[0], size[1], img.Bounds().Dx(), img.Bounds().Dy())
img = resize.Resize(width, height, img, resize.Lanczos3)
}
out, err := os.Create(filepath.Join(outputDirectory, output))
if err != nil {
return err
}
defer out.Close()
switch format {
case "jpeg":
err = jpeg.Encode(out, img, &jpeg.Options{Quality: quality})
case "png":
err = (&png.Encoder{CompressionLevel: png.BestCompression}).Encode(out, img)
case "webp":
err = webp.Encode(out, img, &webp.Options{Lossless: true})
default:
return fmt.Errorf("unsupported output format")
}
return err
}
func generateMD5Hash(input string) string {
hasher := md5.New()
hasher.Write([]byte(input))
return hex.EncodeToString(hasher.Sum(nil))
}
func parseResolution(width, height string, originalWidth, originalHeight int) (uint, uint) {
if width == "auto" && height == "auto" {
return uint(originalWidth), uint(originalHeight)
} else if width == "auto" {
newHeight, _ := strconv.Atoi(height)
return uint(float64(newHeight) * float64(originalWidth) / float64(originalHeight)), uint(newHeight)
} else if height == "auto" {
newWidth, _ := strconv.Atoi(width)
return uint(newWidth), uint(float64(newWidth) * float64(originalHeight) / float64(originalWidth))
}
newWidth, _ := strconv.Atoi(width)
newHeight, _ := strconv.Atoi(height)
return uint(newWidth), uint(newHeight)
}
func compressHandler(w http.ResponseWriter, r *http.Request) {
urlString := r.URL.Query().Get("url")
format := r.URL.Query().Get("output")
qualityStr := r.URL.Query().Get("quality")
resolution := r.URL.Query().Get("resolution")
version := r.URL.Query().Get("v")
if !isDomainAllowed(urlString) {
http.Error(w, "URL domain not allowed", http.StatusForbidden)
return
}
paramsString := fmt.Sprintf("%s-%s-%s-%s-%s", urlString, format, qualityStr, resolution, version)
hash := generateMD5Hash(paramsString)
output := fmt.Sprintf("%s.%s", hash, format)
filePath := filepath.Join(outputDirectory, output)
if _, err := os.Stat(filePath); err == nil {
sendExistingFile(w, filePath, format)
return
}
img, imgFormat, err := downloadImage(urlString)
if err != nil {
http.Error(w, fmt.Sprintf("Error downloading image: %s", err), http.StatusInternalServerError)
return
}
if format == "" {
format = imgFormat
}
quality, _ := strconv.Atoi(qualityStr)
err = compressImage(img, format, output, quality, resolution)
if err != nil {
http.Error(w, fmt.Sprintf("Error compressing image: %s", err), http.StatusInternalServerError)
return
}
sendExistingFile(w, filePath, format)
}
func sendExistingFile(w http.ResponseWriter, filePath, format string) {
compressedFile, err := os.Open(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Error opening compressed image file: %s", err), http.StatusInternalServerError)
return
}
defer compressedFile.Close()
w.Header().Set("Content-Type", contentTypeMap[format])
io.Copy(w, compressedFile)
}
func printBanner() {
banner := `
------------------------------------
Image Optimizer Service
Author: https://github.com/daniwebdev`
fmt.Println(banner)
fmt.Printf("Server is running on port: %d\n", port)
fmt.Printf("Allowed Domains: %s\n", allowedDomains)
fmt.Printf("Output Directory: %s\n", outputDirectory)
fmt.Println("------------------------------------")
}
func main() {
initConfig()
r := mux.NewRouter()
r.HandleFunc("/optimize", compressHandler).Methods("GET")
r.HandleFunc("/optimize/{filename}", compressHandler).Methods("GET")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok!")
})
log.Printf("Server is listening on :%d. Output directory: %s\n", port, outputDirectory)
printBanner()
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), r))
}