-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo.go
125 lines (113 loc) · 2.17 KB
/
photo.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
package main
import (
_ "embed"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
)
var (
photoExt = []string{
".jpg",
".png",
".gif",
".jpeg",
".bmp",
}
//go:embed photo.html
photoTmpl string
)
type Image template.HTML
type Images struct {
Title string
Images []Image
}
func (images Images) List() template.JS {
var out string
for _, v_ := range images.Images {
v := string(v_)
s := (&url.URL{Path: v}).String()
out += fmt.Sprintf("{url:\"%s\"},", s)
}
if len(out) > 0 {
return template.JS(out[:len(out)-1])
}
return ""
}
func imagesList(images Images) template.JS {
return images.List()
}
func renderPhoto(w http.ResponseWriter, r *http.Request) {
// trim /photo/test -> test
path := r.URL.Path[len("/photo/"):]
dir := filepath.Join(rootDir, path)
var images Images
images.Title = filepath.Base(dir)
fs := readDir2(dir)
if len(fs) == 0 {
http.Redirect(w, r, "/"+path, 302)
return
}
// sort.Slice(fs, func(i, j int) bool {
// return fs[i].Name() < fs[j].Name()
// })
sort.Strings(fs)
for _, f := range fs {
// fp := filepath.Join("/statics", path, f.Name())
fp := filepath.Join("/statics", f)
log.Println(fp)
images.Images = append(images.Images, Image(fp))
}
f := template.FuncMap{
"imageslist": imagesList,
}
t, err := template.New("index").Funcs(f).Parse(photoTmpl)
if err != nil {
fmt.Fprintln(w, err.Error())
}
err = t.Execute(w, images)
if err != nil {
fmt.Fprintln(w, err.Error())
}
}
func readDir(path string) (fs []os.FileInfo) {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Println(err)
}
for _, f := range files {
for _, ext := range photoExt {
if strings.ToLower(filepath.Ext(f.Name())) == ext {
fs = append(fs, f)
break
}
}
}
return
}
func readDir2(path string) (fs []string) {
err := filepath.Walk(path, func(p string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
for _, ext := range photoExt {
if strings.ToLower(filepath.Ext(p)) == ext {
f := p[len(rootDir):]
log.Println(f)
fs = append(fs, f)
break
}
}
return err
})
if err != nil {
log.Println(err)
}
return
}