-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfile.go
154 lines (133 loc) · 3.31 KB
/
file.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gemini
import (
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
//--------------------------------------
type byDirs []os.FileInfo
func (s byDirs) Len() int {
return len(s)
}
func (s byDirs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byDirs) Less(i, j int) bool {
if s[i].IsDir() {
if s[j].IsDir() {
return s[i].Name() < s[j].Name()
}
return true
}
if s[j].IsDir() {
if s[i].IsDir() {
return s[i].Name() < s[j].Name()
}
return false
}
return s[i].Name() < s[j].Name()
}
//--------------------------------------
func brief(s string) (r string) {
if len(s) < 58 {
return s
} else {
return s[0:58] + " ..."
}
}
type fileHandler struct {
root string
}
func FileServer(root string) Handler {
return &fileHandler{root: filepath.Clean(root)}
}
func ServeFilePath(p string, w *Response, r *Request) {
s, err := os.Stat(p)
if err != nil {
w.SetStatus(StatusNotFound, "File Not Found!")
return
}
if !allowed(s) {
w.SetStatus(StatusGone, "Forbidden!")
return
}
if s.IsDir() {
files, err := ioutil.ReadDir(p)
if err != nil {
w.SetStatus(StatusTemporaryFailure, "Error reading directory!")
return
}
for _, f := range files {
if f.Name() == "index.gmi" {
p = path.Join(p, "index.gmi")
goto FILE
}
}
w.SetStatus(StatusSuccess, "text/gemini")
w.Write([]byte(fmt.Sprintf("# Listing %s\n\n", p)))
sort.Sort(byDirs(files))
//sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() })
rpath := r.URL.String()
w.Write([]byte(fmt.Sprintf("=> %s ..\n", rpath[0:strings.LastIndex(rpath,"/")])))
for _, file := range files {
if strings.HasPrefix(file.Name(), ".") {
continue
}
if !allowed(file) {
continue
}
if file.IsDir() {
w.Write([]byte(fmt.Sprintf("=> %s % 80s %*v\n", filepath.Clean(path.Join(r.URL.Path, file.Name())), brief("⍠: "+file.Name()), 82-len(brief("⍠: "+file.Name())), "["+file.ModTime().Format(time.Stamp)+"]")))
} else {
w.Write([]byte(fmt.Sprintf("=> %s % 80s %*v\n", filepath.Clean(path.Join(r.URL.Path, file.Name())), brief(file.Name()), 80-len(brief(file.Name())), "["+file.ModTime().Format(time.Stamp)+"]")))
}
}
return
}
FILE:
ext := filepath.Ext(p)
var mimeType string
if ext == ".gmi" {
mimeType = "text/gemini"
} else {
mimeType = mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "octet/stream"
}
}
f, err := os.OpenFile(p, os.O_RDONLY, 0600)
if err != nil {
w.SetStatus(StatusTemporaryFailure, "Error reading file!")
return
}
defer f.Close()
w.SetStatus(StatusSuccess, mimeType)
_, err = io.Copy(w, f)
if err != nil {
// .. remote closed the connection, nothing we can do besides log
// or io error, but status is already sent, everything is broken!
w.SetStatus(StatusTemporaryFailure, "IO error!")
}
}
func (fh *fileHandler) ServeGemini(w *Response, r *Request) {
p := filepath.Clean(path.Join(fh.root, r.URL.Path))
if !strings.HasPrefix(p, fh.root) {
w.SetStatus(StatusTemporaryFailure, "Path not in scope!")
return
}
ServeFilePath(p, w, r)
}
func allowed(fi os.FileInfo) bool {
return uint64(fi.Mode().Perm())&0444 == 0444
}
// almost copy pasta form https://tildegit.org/solderpunk/molly-brown
func generateDirectoryListing(path string, out io.Writer) error {
return nil
}