This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
204 lines (171 loc) · 4.5 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
// 🙏 Thanks & Credits to @arsmn
package embed
import (
"fmt"
"html"
"log"
"net/http"
"os"
"path"
"sort"
"strings"
"github.com/gofiber/fiber"
)
// Config holds the configuration for the middleware
type Config struct {
// Root is a FileSystem that provides access
// to a collection of files and directories.
// Required. Default: nil
Root http.FileSystem
// ErrorHandler defines the response body when an error raised.
// Optional. Defaul: Next for NotExistError and 403 for PermissionError and 500 for others
ErrorHandler func(*fiber.Ctx, error)
// Index file for serving a directory.
// Optional. Default: "index.html"
Index string
// Enable directory browsing.
// Optional. Default: false
Browse bool
}
// New returns an embed middleware for serving files
func New(config ...Config) func(*fiber.Ctx) {
var cfg Config
if len(config) > 0 {
cfg = config[0]
}
if cfg.Root == nil {
log.Fatal("Fiber: Embed middleware requires root")
}
if cfg.ErrorHandler == nil {
cfg.ErrorHandler = func(c *fiber.Ctx, err error) {
if os.IsNotExist(err) {
c.Next()
return
}
if os.IsPermission(err) {
c.SendStatus(fiber.StatusForbidden)
return
}
c.SendStatus(fiber.StatusInternalServerError)
}
}
if cfg.Index == "" {
cfg.Index = "/index.html"
}
if !strings.HasPrefix(cfg.Index, "/") {
cfg.Index = "/" + cfg.Index
}
var prefix string
return func(c *fiber.Ctx) {
// Set prefix
if len(prefix) == 0 {
prefix = c.Route().Path
}
// Strip prefix
path := strings.TrimPrefix(c.Path(), prefix)
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
file, err := cfg.Root.Open(path)
if err != nil {
cfg.ErrorHandler(c, err)
return
}
stat, err := file.Stat()
if err != nil {
cfg.ErrorHandler(c, err)
return
}
// Serve index if path is directory
if stat.IsDir() {
indexPath := strings.TrimSuffix(path, "/") + cfg.Index
index, err := cfg.Root.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
if err == nil {
file = index
stat = indexStat
}
}
}
// Browse directory if no index found and browsing is enabled
if stat.IsDir() {
if cfg.Browse {
if err := dirList(c, file); err != nil {
cfg.ErrorHandler(c, err)
}
return
}
c.SendStatus(fiber.StatusForbidden)
return
}
modTime := stat.ModTime()
contentLength := int(stat.Size())
// Set Content Type header
c.Type(getFileExtension(stat.Name()))
// Set Last Modified header
if !modTime.IsZero() {
c.Set(fiber.HeaderLastModified, modTime.UTC().Format(http.TimeFormat))
}
if c.Method() == fiber.MethodGet {
c.Fasthttp.SetBodyStream(file, contentLength)
return
} else if c.Method() == fiber.MethodHead {
c.Fasthttp.ResetBody()
c.Fasthttp.Response.SkipBody = true
c.Fasthttp.Response.Header.SetContentLength(contentLength)
if err := file.Close(); err != nil {
cfg.ErrorHandler(c, err)
}
return
}
c.Next()
}
}
func getFileExtension(path string) string {
n := strings.LastIndexByte(path, '.')
if n < 0 {
return ""
}
return path[n:]
}
func dirList(c *fiber.Ctx, f http.File) error {
fileinfos, err := f.Readdir(-1)
if err != nil {
return err
}
fm := make(map[string]os.FileInfo, len(fileinfos))
filenames := make([]string, 0, len(fileinfos))
for _, fi := range fileinfos {
name := fi.Name()
fm[name] = fi
filenames = append(filenames, name)
}
basePathEscaped := html.EscapeString(c.Path())
c.Write(fmt.Sprintf("<html><head><title>%s</title><style>.dir { font-weight: bold }</style></head><body>", basePathEscaped))
c.Write(fmt.Sprintf("<h1>%s</h1>", basePathEscaped))
c.Write(fmt.Sprintf("<ul>"))
if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(c.Path() + "/..")
c.Write(fmt.Sprintf(`<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped))
}
sort.Strings(filenames)
for _, name := range filenames {
pathEscaped := html.EscapeString(path.Join(c.Path() + "/" + name))
fi := fm[name]
auxStr := "dir"
className := "dir"
if !fi.IsDir() {
auxStr = fmt.Sprintf("file, %d bytes", fi.Size())
className = "file"
}
c.Write(fmt.Sprintf(`<li><a href="%s" class="%s">%s</a>, %s, last modified %s</li>`,
pathEscaped, className, html.EscapeString(name), auxStr, fi.ModTime()))
}
c.Write(fmt.Sprintf("</ul></body></html>"))
c.Type("html")
return nil
}