-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogallery.go
332 lines (295 loc) · 7.01 KB
/
gogallery.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"log"
"os"
"syscall"
"sort"
"path"
"fmt"
"flag"
"http"
"regexp"
"template"
"rand"
// "strings"
sqlite "gosqlite.googlecode.com/hg/sqlite"
)
const maxDirDepth = 24
const templDir = "tmpl/"
const thumbsDir = ".thumbs"
const picpattern = "/pic/"
const tagpattern = "/tag/"
const tagspattern = "/tags"
const randompattern = "/random"
var (
rootDir, _ = os.Getwd();
fileServer = http.FileServer(rootDir, "")
rootDirLen = len(rootDir)
db *sqlite.Conn
titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
templates = make(map[string]*template.Template)
picValidator = regexp.MustCompile(".*(jpg|JPG|jpeg|JPEG|png)$")
maxId = 1
// command flags
snapsize = flag.Int("snapsize", 0, "height of the thumbnail pictures")
picsdir = flag.String("picsdir", "pics/", "Root dir for all the pics (defaults to ./pics/)")
dbfile = flag.String("dbfile", "./gallery.db", "File to store the db (defaults to ./gallery.db)")
initdb = flag.Bool("init", false, "clean out the db file and start from scratch")
)
type lines []string
func (p *lines) Write(line string) (n int, err os.Error) {
slice := *p
l := len(slice)
if l == cap(slice) { // reallocate
// Allocate one more line
newSlice := make([]string, l+1, l+1)
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
l++;
slice = slice[0:l]
slice[l-1] = line
*p = slice
return len(line), nil
}
type page struct {
title string
pics lines
thumbs lines
}
// http
func renderTemplate(c *http.Conn, tmpl string, p *page) {
err := templates[tmpl].Execute(p, c)
if err != nil {
http.Error(c, err.String(), http.StatusInternalServerError)
}
}
func tagPage(tag string) (*page, os.Error) {
stmt, err := db.Prepare(
"select file from tags where tag = '" + tag + "'")
errchk(err)
var t string
var pics lines
var thumbs lines
errchk(stmt.Exec())
for stmt.Next() {
errchk(stmt.Scan(&t))
pics.Write(t)
dir, file := path.Split(t)
thumb := path.Join(dir, thumbsDir, file)
thumbs.Write(thumb)
}
title := tag
stmt.Finalize()
return &page{title: title, pics: pics, thumbs: thumbs}, nil
}
//TODO: tags size
func tagsPage() (*page, os.Error) {
stmt, err := db.Prepare(
"select tag, count(tag) from tags group by tag")
errchk(err)
var s string
var i int
var pics lines
errchk(stmt.Exec())
for stmt.Next() {
errchk(stmt.Scan(&s, &i))
pics.Write(s)
}
title := "All tags"
stmt.Finalize()
return &page{title: title, pics: pics, thumbs: nil}, nil
}
func tagHandler(c *http.Conn, r *http.Request, urlpath string) {
tag := urlpath[len(tagpattern):]
if !titleValidator.MatchString(tag) {
http.NotFound(c, r)
return
}
p, err := tagPage(tag)
if err != nil {
http.Error(c, err.String(), http.StatusInternalServerError)
return
}
renderTemplate(c, "tag", p)
}
func picHandler(c *http.Conn, r *http.Request, urlpath string) {
var p page
p.title = urlpath[len(picpattern):]
p.pics = nil
p.thumbs = nil
err := r.ParseForm()
if err != nil {
http.Error(c, err.String(), http.StatusInternalServerError)
return
}
// get newtag from POST
for k, v := range (*r).Form {
if k == "newtag" {
newtag := v[0]
// only allow single alphanumeric word tag
if titleValidator.MatchString(newtag) {
errchk(db.Exec(
"insert into tags values (" +
fmt.Sprint(maxId) + ", '" + p.title + "', '" + newtag + "')"))
maxId++;
}
break
}
}
renderTemplate(c, "pic", &p)
}
func tagsHandler(c *http.Conn, r *http.Request, urlpath string) {
p, err := tagsPage()
if err != nil {
http.Error(c, err.String(), http.StatusInternalServerError)
return
}
renderTemplate(c, "tags", p)
}
//TODO: set maxId when initDb has not been run
func randomHandler(c *http.Conn, r *http.Request, urlpath string) {
randId := rand.Intn(maxId) + 1
stmt, err := db.Prepare(
"select file from tags where id = " + fmt.Sprint(randId))
errchk(err)
var s string
errchk(stmt.Exec())
if stmt.Next() {
errchk(stmt.Scan(&s))
}
stmt.Finalize()
s = picpattern + s
http.Redirect(c, s, http.StatusFound)
}
func serveFile(c *http.Conn, r *http.Request) {
fileServer.ServeHTTP(c, r);
}
func makeHandler(fn func(*http.Conn, *http.Request, string)) http.HandlerFunc {
return func(c *http.Conn, r *http.Request) {
title := r.URL.Path
fn(c, r, title)
}
}
// sqlite
func initDb() {
var err os.Error
db, err = sqlite.Open(*dbfile)
errchk(err)
db.Exec("drop table tags")
errchk(db.Exec("create table tags (id int, file text, tag text)"))
err = scanDir(*picsdir)
if err != nil {
log.Exit(err)
}
db.Close();
}
func errchk(err os.Error) {
if err != nil {
log.Exit(err)
}
}
func scanDir(dirpath string) os.Error {
currentDir, err := os.Open(dirpath, os.O_RDONLY, 0644)
if err != nil {
return err
}
names, err := currentDir.Readdirnames(-1)
if err != nil {
return err
}
currentDir.Close()
sort.SortStrings(names)
e := 0
e = syscall.Mkdir(path.Join(dirpath, thumbsDir), 0755)
if e != 0 && e != syscall.EEXIST {
return os.Errno(e)
}
for _,v := range names {
childpath := path.Join(dirpath, v)
fi, err := os.Lstat(childpath)
if err != nil {
return err
}
if fi.IsDirectory() && v != thumbsDir {
err = scanDir(childpath)
if err != nil {
return err
}
} else {
if picValidator.MatchString(childpath) {
err = mkThumb(childpath)
if err != nil {
return err
}
path := childpath[rootDirLen+1:]
errchk(db.Exec(
"insert into tags values (" +
fmt.Sprint(maxId) + ", '" + path + "', 'all')"))
maxId++;
}
}
}
return err
}
func mkThumb(filepath string) os.Error {
dir, file := path.Split(filepath)
thumb := path.Join(dir, thumbsDir, file)
fd, err := os.Open(thumb, os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
if err != os.EEXIST {
return err
}
return nil
}
fd.Close()
var args []string = make([]string, 5)
args[0] = "/usr/bin/convert"
args[1] = filepath
args[2] = "-thumbnail"
args[3] = "200x300"
args[4] = thumb
fds := []*os.File{os.Stdin, os.Stdout, os.Stderr}
pid, err := os.ForkExec(args[0], args, os.Environ(), "", fds)
if err != nil {
return err
}
_, err = os.Wait(pid, os.WNOHANG)
if err != nil {
return err
}
return nil
}
func init() {
for _, tmpl := range []string{"tag", "pic", "tags"} {
templates[tmpl] = template.MustParseFile(templDir + tmpl+".html", nil)
}
}
func usage() {
fmt.Fprintf(os.Stderr,
"usage: gogallery \n");
flag.PrintDefaults();
os.Exit(2);
}
func main() {
flag.Usage = usage
flag.Parse()
*picsdir = path.Clean(*picsdir)
if (*picsdir)[0] != '/' {
cwd, _ := os.Getwd()
*picsdir = path.Join(cwd, *picsdir)
}
if *initdb {
initDb()
}
var err os.Error
db, err = sqlite.Open(*dbfile)
errchk(err)
http.HandleFunc(tagpattern, makeHandler(tagHandler))
http.HandleFunc(picpattern, makeHandler(picHandler))
http.HandleFunc(tagspattern, makeHandler(tagsHandler))
http.HandleFunc(randompattern, makeHandler(randomHandler))
http.HandleFunc("/", http.HandlerFunc(serveFile))
http.ListenAndServe(":8080", nil)
}