forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticController.go
More file actions
81 lines (71 loc) · 2.06 KB
/
StaticController.go
File metadata and controls
81 lines (71 loc) · 2.06 KB
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
package controllers
import (
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/TruthHun/BookStack/models"
"github.com/TruthHun/BookStack/models/store"
"github.com/TruthHun/BookStack/utils"
"github.com/astaxie/beego"
)
type StaticController struct {
beego.Controller
}
func (this *StaticController) APP() {
link := strings.TrimSpace(models.GetOptionValue("APP_PAGE", ""))
if link != "" {
this.Redirect(link, 302)
}
this.Abort("404")
}
//静态文件,这个加在路由的最后
func (this *StaticController) StaticFile() {
file := this.GetString(":splat")
if strings.HasPrefix(file, ".well-known") || file == "sitemap.xml" {
http.ServeFile(this.Ctx.ResponseWriter, this.Ctx.Request, file)
return
}
file = strings.TrimLeft(file, "./")
path := filepath.Join(utils.VirtualRoot, file)
http.ServeFile(this.Ctx.ResponseWriter, this.Ctx.Request, path)
}
// 书籍静态文件
func (this *StaticController) ProjectsFile() {
prefix := "projects/"
object := prefix + strings.TrimLeft(this.GetString(":splat"), "./")
//这里的时间只是起到缓存的作用
t, _ := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
date := t.Format(http.TimeFormat)
since := this.Ctx.Request.Header.Get("If-Modified-Since")
if since == date {
this.Ctx.ResponseWriter.WriteHeader(http.StatusNotModified)
return
}
if utils.StoreType == utils.StoreOss { //oss
staticDomain := strings.Trim(beego.AppConfig.DefaultString("static_domain", ""), "/")
if staticDomain == "" {
reader, err := store.NewOss().GetFileReader(object)
if err != nil {
beego.Error(err.Error())
this.Abort("404")
}
defer reader.Close()
b, err := ioutil.ReadAll(reader)
if err != nil {
beego.Error(err.Error())
this.Abort("404")
}
this.Ctx.ResponseWriter.Header().Set("Last-Modified", date)
if strings.HasSuffix(object, ".svg") {
this.Ctx.ResponseWriter.Header().Set("Content-Type", "image/svg+xml")
}
this.Ctx.ResponseWriter.Write(b)
return
}
this.Redirect(staticDomain+"/"+object, 302)
} else { //local
this.Abort("404")
}
}