-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (157 loc) · 4.61 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
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/jpeg"
"image/png"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/chai2010/webp"
)
const VERSION = "0.0.1"
type Config struct {
Quality int `json:"quality"`
CacheFolder string `json:"cacheFolder"`
CacheClearKey string `json:"cacheClearKey"`
Port string `json:"port"`
Debug bool `json:"debug"`
}
var (
configFile = flag.String("config", "config.json", "Path to configuration file")
config Config
debugLog *log.Logger
)
func loadConfig() error {
file, err := os.Open(*configFile)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
return decoder.Decode(&config)
}
func setupLogging() {
if config.Debug {
debugLog = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
}
}
func debugLogf(format string, v ...interface{}) {
if debugLog != nil {
debugLog.Printf(format, v...)
}
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
filePath := r.URL.Path
cachePath := config.CacheFolder + filePath + ".webp"
debugLogf("Handling request for: %s", filePath)
if _, err := os.Stat(cachePath); err == nil {
debugLogf("Serving cached file: %s", cachePath)
http.ServeFile(w, r, cachePath)
return
}
file, err := os.Open(filePath)
if err != nil {
debugLogf("File not found: %s", filePath)
http.Error(w, "File not found", http.StatusNotFound)
return
}
defer file.Close()
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
debugLogf("Error reading file: %v", err)
http.Error(w, "Error reading file", http.StatusInternalServerError)
return
}
contentType := http.DetectContentType(buffer)
debugLogf("Detected content type: %s", contentType)
_, err = file.Seek(0, 0)
if err != nil {
debugLogf("Error seeking file: %v", err)
http.Error(w, "Error reading file", http.StatusInternalServerError)
return
}
var img image.Image
switch {
case strings.Contains(contentType, "jpeg"):
img, err = jpeg.Decode(file)
case strings.Contains(contentType, "png"):
img, err = png.Decode(file)
case strings.HasPrefix(contentType, "image/webp"):
debugLogf("Serving original WebP file: %s", filePath)
http.ServeFile(w, r, filePath)
return
default:
debugLogf("Unsupported file type: %s", contentType)
http.Error(w, "Unsupported file type", http.StatusBadRequest)
return
}
if err != nil {
debugLogf("Error processing image: %v", err)
http.Error(w, "Error processing image", http.StatusInternalServerError)
return
}
if err := os.MkdirAll(filepath.Dir(cachePath), 0755); err != nil {
debugLogf("Error creating cache directory: %v", err)
http.Error(w, "Error creating cache directory", http.StatusInternalServerError)
return
}
cacheFile, err := os.Create(cachePath)
if err != nil {
debugLogf("Error creating cache file: %v", err)
http.Error(w, "Error creating cache file", http.StatusInternalServerError)
return
}
defer cacheFile.Close()
w.Header().Set("Content-Type", "image/webp")
err = webp.Encode(cacheFile, img, &webp.Options{Quality: float32(config.Quality)})
if err != nil {
debugLogf("Error encoding image: %v", err)
http.Error(w, "Error encoding image", http.StatusInternalServerError)
return
}
debugLogf("Successfully converted and cached image: %s", cachePath)
http.ServeFile(w, r, cachePath)
}
func cacheClearHandler(w http.ResponseWriter, r *http.Request) {
debugLogf("Cache clear request received")
if r.URL.Query().Get("key") != config.CacheClearKey {
debugLogf("Invalid cache clear key provided")
http.Error(w, "Invalid cache clear key", http.StatusForbidden)
return
}
if err := os.RemoveAll(config.CacheFolder); err != nil {
debugLogf("Error clearing cache: %v", err)
http.Error(w, "Error clearing cache", http.StatusInternalServerError)
return
}
debugLogf("Cache cleared successfully")
if _, err := w.Write([]byte("Cache cleared successfully")); err != nil {
debugLogf("Error writing response: %v", err)
http.Error(w, "Error writing response", http.StatusInternalServerError)
return
}
}
func main() {
fmt.Println("Nimage v" + VERSION)
flag.Parse()
if err := loadConfig(); err != nil {
log.Fatal("Error loading configuration:", err)
}
setupLogging()
debugLogf("Configuration loaded: %+v", config)
if err := os.MkdirAll(config.CacheFolder, 0755); err != nil {
log.Fatal("Error creating cache folder:", err)
}
http.HandleFunc("/", imageHandler)
http.HandleFunc("/clearcache", cacheClearHandler)
log.Printf("Starting server on :%s", config.Port)
if err := http.ListenAndServe(":"+config.Port, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}