forked from GoesToEleven/GolangTraining
/
main.go
129 lines (112 loc) · 2.89 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
package photo
import (
"crypto/md5"
"encoding/hex"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
type fileTimes []time.Time
func (ft *fileTimes) Len() int {
return len(*ft)
}
func (ft *fileTimes) Less(i, j int) bool {
return (*ft)[i].After((*ft)[j])
}
func (ft *fileTimes) Swap(i, j int) {
(*ft)[i], (*ft)[j] = (*ft)[j], (*ft)[i]
}
func mainSite(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
tpl, err := template.ParseFiles("mainSite.gohtml")
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
times := fileTimes{}
images := map[time.Time]string{}
filepath.Walk("images", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
fileTime := info.ModTime()
images[fileTime] = filepath.ToSlash("/" + path)
times = append(times, fileTime)
}
return nil
})
sort.Sort(×)
sortedImages := make([]string, len(times))
for i, v := range times {
sortedImages[i] = images[v]
}
err = tpl.Execute(res, sortedImages)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
}
func adminSite(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
gotFile := false
if req.Method == "POST" {
file, _, err := req.FormFile("image")
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
defer file.Close()
ckSum := md5.New()
io.Copy(ckSum, file)
filename := "images/" + hex.EncodeToString(ckSum.Sum(nil))
wtr, err := os.Create(filename)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
defer wtr.Close()
_, err = file.Seek(0, 0)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
_, err = io.Copy(wtr, file)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
gotFile = true
}
tpl, err := template.ParseFiles("adminSite.gohtml")
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
err = tpl.Execute(res, gotFile)
if err != nil {
http.Error(res, "Server Error", http.StatusInternalServerError)
log.Errorf(ctx, err.Error())
return
}
}
func getCSS(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "style.css")
}
func init() {
imagesHandler := http.StripPrefix("/images/", http.FileServer(http.Dir("images/")))
http.HandleFunc("/", mainSite)
http.HandleFunc("/admin", adminSite)
http.Handle("/images/", imagesHandler)
http.HandleFunc("/style.css", getCSS)
}