-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (130 loc) · 3.22 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/disintegration/imaging"
"github.com/gorilla/mux"
"github.com/rwcarlsen/goexif/exif"
)
const storage = "/media/sf_Photos"
const cacheDir = "/media/sf_Photos/tmp"
type FileInfo struct {
Name string `json:"name,omitempty"`
Size int64 `json:"size"`
Mode os.FileMode `json:"mode"`
ModTime time.Time `json:"modTime"`
IsDir bool `json:"isDir"`
IsPhoto bool `json:"isPhoto"`
Exif exif.Exif `json:"exif,omitempty"`
Url string `json:"img"`
ThumbUrl string `json:"thumb"`
}
func readRotation(path string) int {
ef, err := os.Open(path)
if err != nil {
log.Print(err)
return 1
}
e, err := exif.Decode(ef)
if err != nil {
log.Print(err)
return 1
}
o, err := e.Get(exif.Orientation)
if err != nil {
log.Print(err)
return 1
}
i, err := o.Int(0)
if err != nil {
log.Print(err)
return 1
}
return i
}
func thumbnailHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
height, err := strconv.Atoi(query.Get("height"))
if err != nil {
height = 50
}
path := filepath.Join(storage, r.URL.Path)
photo, err := imaging.Open(path)
if err != nil {
fmt.Fprint(w, err)
return
}
switch readRotation(path) {
case 8:
photo = imaging.Rotate90(photo)
case 2:
photo = imaging.Rotate180(photo)
case 6:
photo = imaging.Rotate270(photo)
}
thumb := imaging.Resize(photo, 0, height, imaging.Box)
imaging.Encode(w, thumb, imaging.JPEG)
}
func withJsonDirectoryListing(h http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "" || strings.HasSuffix(r.URL.Path, "/") {
path := filepath.Join(storage, r.URL.Path)
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
list := []FileInfo{}
for _, file := range files {
f := FileInfo{
Name: file.Name(),
Size: file.Size(),
ModTime: file.ModTime(),
IsDir: file.IsDir(),
IsPhoto: !file.IsDir() && strings.HasSuffix(strings.ToLower(file.Name()), ".jpg"),
Url: (&url.URL{Path: "/photos/" + r.URL.Path + file.Name()}).String(),
ThumbUrl: (&url.URL{Path: "/thumbnail/" + r.URL.Path + file.Name()}).String(),
}
if f.IsPhoto { // read exif
ef, err := os.Open(filepath.Join(path, file.Name()))
if err != nil {
log.Print(err)
continue
}
e, err := exif.Decode(ef)
if err != nil {
log.Print(err)
continue
}
f.Exif = *e
}
list = append(list, f)
}
result, err := json.Marshal(list)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(result)
return
}
h.ServeHTTP(w, r)
})
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("static")))
r.Handle("/thumbnail/", http.StripPrefix("/thumbnail/", http.HandlerFunc(thumbnailHandler)))
r.Handle("/photos/", http.StripPrefix("/photos/", withJsonDirectoryListing(http.FileServer(http.Dir(storage)))))
http.Handle("/", r)
http.ListenAndServe("0.0.0.0:8000", nil)
}