-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp_gallery.go
76 lines (68 loc) · 1.64 KB
/
http_gallery.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
package main
import (
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/ncruces/jason"
"github.com/ncruces/rethinkraw/pkg/osutil"
)
func galleryHandler(w http.ResponseWriter, r *http.Request) httpResult {
if r := sendAllowed(w, r, "GET", "HEAD"); r.Done() {
return r
}
prefix := getPathPrefix(r)
path := fromURLPath(r.URL.Path, prefix)
w.Header().Set("Cache-Control", "max-age=10")
if r := sendCached(w, r, path); r.Done() {
return r
}
if files, err := os.ReadDir(path); err != nil {
return httpResult{Error: err}
} else {
data := struct {
Upload bool
Title, Path string
Dirs, Photos []struct{ Name, Path string }
JSON jason.Object
}{
!isLocalhost(r),
toUsrPath(path, prefix),
toURLPath(path, prefix),
nil, nil, jason.Object{},
}
if data.Upload {
data.JSON["uploadPath"] = data.Path
data.JSON["uploadExts"] = extensions
}
for _, entry := range files {
name := entry.Name()
path := filepath.Join(path, name)
item := struct{ Name, Path string }{name, toURLPath(path, prefix)}
if osutil.HiddenFile(entry) {
continue
}
if entry.Type().IsRegular() {
if _, ok := extensions[strings.ToUpper(filepath.Ext(name))]; ok {
data.Photos = append(data.Photos, item)
}
continue
}
if entry.Type()&os.ModeSymlink != 0 {
fi, err := os.Stat(path)
if err != nil {
continue
}
entry = fs.FileInfoToDirEntry(fi)
}
if entry.IsDir() {
data.Dirs = append(data.Dirs, item)
}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
return httpResult{
Error: templates.ExecuteTemplate(w, "gallery.gohtml", data),
}
}
}