forked from smtalim/bwpwg
/
blobstrex.go
112 lines (94 loc) · 3.24 KB
/
blobstrex.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
package blobstrex
import (
"fmt"
"html/template"
"io"
"net/http"
"github.com/rwcarlsen/goexif/exif"
"strconv"
"appengine"
"appengine/blobstore"
)
func init() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/serve/", handleServe)
http.HandleFunc("/upload", handleUpload)
}
func serveError(c appengine.Context, w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "Internal Server Error")
c.Errorf("%v", err)
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
uploadURL, err := blobstore.UploadURL(c, "/upload", nil)
if err != nil {
serveError(c, w, err)
return
}
w.Header().Set("Content-Type", "text/html")
err = rootTemplate.Execute(w, uploadURL)
if err != nil {
c.Errorf("%v", err)
}
}
var rootTemplate = template.Must(template.New("root").Parse(rootTemplateHTML))
const rootTemplateHTML = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/upper.css">
<title>Upload your Photo</title>
</head>
<body>
<form action="{{.}}" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="file"><br />
<input type="submit" name="submit" value="Submit">
</form></body></html>
`
func handleServe(w http.ResponseWriter, r *http.Request) {
// Instantiate blobstore reader
reader := blobstore.NewReader(appengine.NewContext(r),
appengine.BlobKey(r.FormValue("blobKey")))
lat, lng, _ := getLatLng(reader)
blobstore.Delete(appengine.NewContext(r),
appengine.BlobKey(r.FormValue("blobKey")))
if lat == "" {
io.WriteString(w, "Sorry but your photo has no GeoTag information...")
return
}
s := "http://maps.googleapis.com/maps/api/staticmap?zoom=5&size=600x300&maptype=roadmap&center="
s = s + lat + "," + lng + "&markers=color:blue%7Clabel:I%7C" + lat + "," + lng
img := "<img src='" + s + "' alt='map' />"
fmt.Fprint(w, img)
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
blobs, _, err := blobstore.ParseUpload(r)
if err != nil {
serveError(c, w, err)
return
}
file := blobs["file"]
if len(file) == 0 {
c.Errorf("no file uploaded")
http.Redirect(w, r, "/", http.StatusFound)
return
}
http.Redirect(w, r, "/serve/?blobKey="+string(file[0].BlobKey),
http.StatusFound)
}
func getLatLng(f io.Reader) (string, string, error) {
// Decode parses EXIF-encoded data from f
// and returns a queryable Exif object.
x, err := exif.Decode(f)
if err != nil {
return "", "", err
}
lat, lng, _ := x.LatLong()
latstr := strconv.FormatFloat(lat, 'f', 15, 64)
lngstr := strconv.FormatFloat(lng, 'f', 15, 64)
return latstr, lngstr, nil
}