-
Notifications
You must be signed in to change notification settings - Fork 4
/
static.go
43 lines (36 loc) · 1.15 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
// Package clib - Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"strings"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/assets"
)
func Favicon(rc *fasthttp.RequestCtx) {
data, contentType, err := assets.EmbedAsset("favicon.ico")
assetResponse(rc, data, contentType, err)
}
func RobotsTxt(rc *fasthttp.RequestCtx) {
data, contentType, err := assets.EmbedAsset("robots.txt")
assetResponse(rc, data, contentType, err)
}
func Static(rc *fasthttp.RequestCtx) {
p := strings.TrimPrefix(string(rc.Request.URI().Path()), "/assets")
p = strings.TrimPrefix(p, "/")
if strings.Contains(p, "../") {
rc.Error("invalid path", fasthttp.StatusNotFound)
} else {
data, contentType, e := assets.EmbedAsset(p)
assetResponse(rc, data, contentType, e)
}
}
func assetResponse(rc *fasthttp.RequestCtx, data []byte, contentType string, err error) {
if err == nil {
rc.Response.Header.SetContentType(contentType)
rc.SetStatusCode(fasthttp.StatusOK)
cutil.WriteCORS(rc)
_, _ = rc.Write(data)
} else {
rc.Error(err.Error(), fasthttp.StatusNotFound)
}
}