-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (155 loc) · 3.84 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
package main
import (
"os"
"syscall"
"sort"
"path"
"fmt"
"flag"
"http"
"template"
"log"
"regexp"
sqlite "gosqlite.googlecode.com/hg/sqlite"
)
const maxDirDepth = 24
const thumbsDir = ".thumbs"
const picpattern = "/pic/"
const tagpattern = "/tag/"
const tagspattern = "/tags"
var (
rootdir, _ = os.Getwd();
rootdirlen = len(rootdir)
// command flags
dbfile = flag.String("dbfile", "./gallery.db", "File to store the db")
host = flag.String("host", "localhost:8080", "hostname and port for this server")
initdb = flag.Bool("init", false, "clean out the db file and start from scratch")
picsdir = flag.String("picsdir", "pics/", "Root dir for all the pics")
thumbsize = flag.String("thumbsize", "200x300", "size of the thumbnails")
tmpldir = flag.String("tmpldir", "tmpl/", "dir for the templates")
)
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)
// used syscall because can't figure out how to check EEXIST with os
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:]
insert(maxId+1, path, "all")
}
}
}
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] = *thumbsize
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 miscInit() {
// fullpath for picsdir. must be within document root
*picsdir = path.Clean(*picsdir)
if (*picsdir)[0] != '/' {
cwd, _ := os.Getwd()
*picsdir = path.Join(cwd, *picsdir)
}
pathValidator := regexp.MustCompile(rootdir + ".*")
if !pathValidator.MatchString(*picsdir) {
log.Exit("picsdir has to be a subdir of rootdir. (symlink ok)")
}
// same drill for templates.
*tmpldir = path.Clean(*tmpldir)
if (*tmpldir)[0] != '/' {
cwd, _ := os.Getwd()
*tmpldir = path.Join(cwd, *tmpldir)
}
if !pathValidator.MatchString(*tmpldir) {
log.Exit("tmpldir has to be a subdir of rootdir. (symlink ok)")
}
for _, tmpl := range []string{"tag", "pic", "tags"} {
templates[tmpl] = template.MustParseFile(path.Join(*tmpldir, tmpl+".html"), nil)
}
if *initdb {
initDb()
} else {
var err os.Error
db, err = sqlite.Open(*dbfile)
errchk(err)
setMaxId()
}
}
func errchk(err os.Error) {
if err != nil {
log.Exit(err)
}
}
func usage() {
fmt.Fprintf(os.Stderr,
"usage: gogallery \n");
flag.PrintDefaults();
os.Exit(2);
}
func main() {
flag.Usage = usage
flag.Parse()
miscInit()
http.HandleFunc(tagpattern, makeHandler(tagHandler))
http.HandleFunc(picpattern, makeHandler(picHandler))
http.HandleFunc(tagspattern, makeHandler(tagsHandler))
http.HandleFunc("/random", makeHandler(randomHandler))
http.HandleFunc("/next", makeHandler(nextHandler))
http.HandleFunc("/prev", makeHandler(prevHandler))
http.HandleFunc("/", http.HandlerFunc(serveFile))
http.ListenAndServe(":8080", nil)
}