-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
photos.go
134 lines (108 loc) · 2.92 KB
/
photos.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
package photos
import (
"fmt"
"github.com/gorilla/sessions"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/mknote"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
var tpl *template.Template
var store = sessions.NewCookieStore([]byte("something-very-secret"))
func init() {
var err error
tpl, err = template.ParseFiles("assets/tpl/index.gohtml", "assets/tpl/admin_login.gohtml", "assets/tpl/admin_upload.gohtml")
if err != nil {
log.Fatalln("couldn't parse", err)
}
http.HandleFunc("/", home)
http.HandleFunc("/admin", admin)
http.HandleFunc("/admin/upload", upload)
http.HandleFunc("/admin/logout", logout)
http.Handle("/assets/imgs/", http.StripPrefix("/assets/imgs/", http.FileServer(http.Dir("assets/imgs/"))))
}
func home(res http.ResponseWriter, req *http.Request) {
type Photo struct {
PhotoPath string
Lat float64
Long float64
}
var model struct {
Photos []Photo
LoggedIn bool
}
session, _ := store.Get(req, "session-name")
_, model.LoggedIn = session.Values["loggedin"]
filepath.Walk("assets/imgs", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
fmt.Println("WALKING", path)
var currentPhoto Photo
currentPhoto.PhotoPath = path
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
exif.RegisterParsers(mknote.All...)
x, err := exif.Decode(f)
if err != nil {
log.Println("no info")
return nil
}
currentPhoto.Lat, currentPhoto.Long, _ = x.LatLong()
fmt.Println("lat, long: ", currentPhoto.Lat, ", ", currentPhoto.Long)
model.Photos = append(model.Photos, currentPhoto)
}
return nil
})
err := tpl.ExecuteTemplate(res, "index.gohtml", model)
if err != nil {
http.Error(res, err.Error(), 500)
}
}
func admin(res http.ResponseWriter, req *http.Request) {
userName := req.FormValue("userName")
password := req.FormValue("password")
if userName == "You" && password == "Me" {
session, _ := store.Get(req, "session-name")
session.Values["loggedin"] = "true"
session.Save(req, res)
http.Redirect(res, req, "/admin/upload", 302)
return
}
tpl.ExecuteTemplate(res, "admin_login.gohtml", nil)
}
func upload(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session-name")
_, ok := session.Values["loggedin"]
if !ok {
http.Redirect(res, req, "/admin", 302)
return
}
if req.Method == "POST" {
src, hdr, err := req.FormFile("file")
if err != nil {
panic(err)
}
defer src.Close()
fileName := hdr.Filename
dst, err := os.Create("assets/imgs/" + fileName)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer dst.Close()
io.Copy(dst, src)
}
tpl.ExecuteTemplate(res, "admin_upload.gohtml", ok)
}
func logout(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session-name")
delete(session.Values, "loggedin")
session.Save(req, res)
http.Redirect(res, req, "/admin", 302)
return
}