-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubergallery.go
248 lines (214 loc) · 5.94 KB
/
ubergallery.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"io/ioutil"
"log"
"net/http"
"strconv"
"bitbucket.org/huperwebs/webutils/templates"
"fmt"
"github.com/go-ini/ini"
"github.com/julienschmidt/httprouter"
"github.com/nfnt/resize"
"image"
"image/jpeg"
"os"
"path/filepath"
)
const (
ConfigFilename = "galleryConfig.ini"
GalleryDirectory = "gallery-images"
ViewDirectory = "view"
Port = 8080
)
var (
router = httprouter.New()
config *Config
)
type Config struct {
CacheExpiration int
EnablePagination bool
PaginatorThreshold int
ImagesPerPage int
ThumbnailWidth uint
ThumbnailHeight uint
ThumbnailQuality int
ThemeName string
ImageSortBy string
ReverseSort bool
EnableDebugging bool
}
func ReadConfig(filename string) (*Config, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg, err := ini.Load(contents)
if err != nil {
return nil, err
}
config := &Config{}
for _, section := range cfg.Sections() {
switch section.Name() {
case "DEFAULT":
// Nothing to do here
case "basic_settings":
for _, key := range section.Keys() {
switch key.Name() {
case "cache_expiration":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'cache_expiration' unable to parse integer:", err)
}
config.PaginatorThreshold = i
case "enable_pagination":
config.EnablePagination = (key.Value() == "true")
case "paginator_threshold":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'paginator_threshold' unable to parse integer:", err)
}
config.PaginatorThreshold = i
case "thumbnail_width":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'thumbnail_width' unable to parse integer:", err)
}
config.ThumbnailWidth = uint(i)
case "thumbnail_height":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'thumbnail_height' unable to parse integer:", err)
}
config.ThumbnailHeight = uint(i)
case "thumbnail_quality":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'thumbnail_quality' unable to parse integer:", err)
}
config.ThumbnailQuality = i
if i < 0 {
log.Println("Warning: 'thumbnail_quality' must be >= 0; set to 0")
config.ThumbnailQuality = 0
}
if i > 100 {
log.Println("Warning: 'thumbnail_quality' must be <= 100; set to 100")
config.ThumbnailQuality = 100
}
case "theme_name":
config.ThemeName = key.Value()
default:
log.Println("Warning: unsupported key:", key.Name())
}
}
case "advanced_settings":
for _, key := range section.Keys() {
switch key.Name() {
case "images_per_page":
i, err := strconv.Atoi(key.Value())
if err != nil {
log.Println("Error: 'images_per_page' unable to parse integer:", err)
}
config.ImagesPerPage = i
case "images_sort_by":
case "reverse_sort":
config.ReverseSort = (key.Value() == "true")
case "enable_debugging":
config.EnableDebugging = (key.Value() == "true")
default:
log.Println("Warning: unsupported key:", key.Name())
}
}
default:
log.Println("Warning: unsupported section:", section.Name())
}
}
return config, nil
}
type Image struct {
Name string
Thumbnail string
URL string
}
func DefaultRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var images []Image
// Load images
files, err := ioutil.ReadDir(filepath.Join("public", GalleryDirectory))
if err != nil {
templates.WriteInternalError(w, err.Error())
return
}
for _, file := range files {
if !file.IsDir() {
// Generate thumbnail if needed
images = append(images, Image{
Name: file.Name(),
Thumbnail: GenerateThumbnail(file.Name()),
URL: "/" + filepath.Join("public", GalleryDirectory, file.Name()),
})
}
}
p := templates.NewPage("images", images)
templates.Execute(w, r, config.ThemeName+".html", p)
}
func GenerateThumbnail(filename string) string {
thumbName := fmt.Sprintf("%s/%dx%d-%s",
filepath.Join("public", "cache"),
config.ThumbnailWidth,
config.ThumbnailHeight,
filename,
)
// Generate it if needed
if _, err := os.Stat(thumbName); os.IsNotExist(err) {
file, err := os.Open(filepath.Join("public", GalleryDirectory, filename))
if err != nil {
log.Println("Warning: could not create thumbnail for", filename, err)
return filepath.Join("public", GalleryDirectory, filename)
}
// Load original file
img, _, err := image.Decode(file)
if err != nil {
log.Println("Warning: could not create thumbnail for", filename, err)
return filepath.Join("public", GalleryDirectory, filename)
}
// Resize and save
thumb := resize.Thumbnail(config.ThumbnailWidth, config.ThumbnailHeight, img, resize.Lanczos2)
thumbFile, err := os.Create(thumbName)
if err != nil {
log.Println("Warning: could not create thumbnail for", filename, err)
return filepath.Join("public", GalleryDirectory, filename)
}
jpeg.Encode(thumbFile, thumb, &jpeg.Options{config.ThumbnailQuality})
thumbFile.Close()
}
return thumbName
}
func LoadViews() {
templates.Init(&templates.Config{
ProjectView: ViewDirectory,
Handler: nil,
})
templates.PreloadTemplate(config.ThemeName + ".html")
templates.PreloadTemplates()
}
func main() {
var err error
// Read config
config, err = ReadConfig(ConfigFilename)
if err != nil {
log.Fatal(err)
}
// Load HTML views
LoadViews()
// TODO: listen for "reload" signal
// Register route
staticHandler := http.FileServer(http.Dir("./"))
router.GET("/public/*filepath", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
staticHandler.ServeHTTP(w, r)
})
router.GET("/", DefaultRoute)
log.Println("Notice: server started listening at port", Port, "...")
err = http.ListenAndServe(":"+strconv.Itoa(Port), router)
if err != nil {
log.Fatal("Error: server shut down:", err)
}
}