-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
47 lines (41 loc) · 1.13 KB
/
static.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
package web
import (
"embed"
"io"
"net/http"
"strings"
"github.com/fatalbanana/filetundra/internal/log"
"go.uber.org/zap"
)
//go:embed static/css/tundra.css
//go:embed static/icons/archive.svg
//go:embed static/icons/audio.svg
//go:embed static/icons/back.svg
//go:embed static/icons/directory.svg
//go:embed static/icons/document.svg
//go:embed static/icons/executable.svg
//go:embed static/icons/find.svg
//go:embed static/icons/image.svg
//go:embed static/icons/pdf.svg
//go:embed static/icons/presentation.svg
//go:embed static/icons/spreadsheet.svg
//go:embed static/icons/text.svg
//go:embed static/icons/video.svg
var efs embed.FS
func staticHandler(w http.ResponseWriter, r *http.Request) {
virtualPath := strings.TrimPrefix(r.URL.Path, "/")
f, err := efs.Open(virtualPath)
if err != nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if strings.HasPrefix(virtualPath, "static/icons/") {
w.Header().Set("Content-type", "image/svg+xml")
}
_, err = io.Copy(w, f)
if err != nil {
log.Logger.Error("error serving embedded file",
zap.Error(err), zap.String("path", virtualPath))
panic(http.ErrAbortHandler)
}
}