Skip to content

Commit

Permalink
增加虚拟根目录
Browse files Browse the repository at this point in the history
  • Loading branch information
TruthHun88 committed Sep 23, 2019
1 parent b178c03 commit baff1b6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ render.jpg
version_control
node_modules
dist
output
output
virtualroot
57 changes: 0 additions & 57 deletions controllers/BaseController.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package controllers
import (
"bytes"
"fmt"
"net/http"
"strconv"
"unicode/utf8"

"github.com/TruthHun/BookStack/models/store"

"github.com/TruthHun/BookStack/utils"

"encoding/json"
Expand All @@ -17,9 +14,6 @@ import (

"compress/gzip"

"io/ioutil"
"path/filepath"

"time"

"errors"
Expand Down Expand Up @@ -223,22 +217,6 @@ func (this *BaseController) Sitemap() {
this.TplName = "widgets/sitemap.html"
}

//静态文件,这个加在路由的最后
func (this *BaseController) StaticFile() {
splat := this.GetString(":splat")
ext := filepath.Ext(splat)
if strings.Contains(beego.AppConfig.String("StaticExt"), strings.ToLower(ext)) {
if b, err := ioutil.ReadFile(splat); err == nil {
//if strings.ToLower(ext) == ".svg" {
// this.Ctx.ResponseWriter.Header().Set("content-type", "image/svg+xml")
//}
this.Ctx.ResponseWriter.Write(b)
return
}
}
this.Abort("404")
}

func (this *BaseController) loginByMemberId(memberId int) (err error) {
member, err := models.NewMember().Find(memberId)
if member.MemberId == 0 {
Expand Down Expand Up @@ -476,38 +454,3 @@ func (this *BaseController) SetFollow() {
}
this.JsonResult(0, "您已经成功关注了Ta")
}

// 项目静态文件
func (this *BaseController) ProjectsFile() {
prefix := "projects/"
object := prefix + 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
reader, err := store.NewOss().GetFileReader(object)
if err != nil {
beego.Error(err.Error())
this.Abort("404")
}
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)
} else { //local
this.Abort("404")
}
}
64 changes: 64 additions & 0 deletions controllers/StaticController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package controllers

import (
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"time"

"github.com/TruthHun/BookStack/models/store"
"github.com/TruthHun/BookStack/utils"
"github.com/astaxie/beego"
)

type StaticController struct {
beego.Controller
}

//静态文件,这个加在路由的最后
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
reader, err := store.NewOss().GetFileReader(object)
if err != nil {
beego.Error(err.Error())
this.Abort("404")
}
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)
} else { //local
this.Abort("404")
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ func main() {
os.Exit(1)
}
utils.PrintInfo()
utils.InitVirtualRoot()
s.Run()
}
6 changes: 4 additions & 2 deletions routers/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package routers
import (
"encoding/json"

"github.com/TruthHun/BookStack/utils"

"github.com/TruthHun/BookStack/conf"
"github.com/TruthHun/BookStack/models"
"github.com/astaxie/beego"
Expand Down Expand Up @@ -37,8 +39,8 @@ func init() {

var FinishRouter = func(ctx *context.Context) {
ctx.ResponseWriter.Header().Add("Application", "BookStack")
ctx.ResponseWriter.Header().Add("Version", beego.AppConfig.DefaultString("version", "v1.0.0"))
ctx.ResponseWriter.Header().Add("Version", utils.Version)
}

beego.InsertFilter("/*", beego.BeforeRouter, FinishRouter, false)
beego.SetStaticPath("/sitemap", "sitemap")
}
4 changes: 2 additions & 2 deletions routers/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ func webRouter() {
beego.Router("/tags", &controllers.LabelController{}, "get:List")
beego.Router("/sitemap.html", &controllers.BaseController{}, "get:Sitemap")
beego.Router("/local-render", &controllers.LocalhostController{}, "get,post:RenderMarkdown")
beego.Router("/projects/*", &controllers.BaseController{}, "get:ProjectsFile")
beego.Router("/*", &controllers.BaseController{}, "get:StaticFile")
beego.Router("/projects/*", &controllers.StaticController{}, "get:ProjectsFile")
beego.Router("/*", &controllers.StaticController{}, "get:StaticFile")
}
9 changes: 7 additions & 2 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ import (

//更多存储类型有待扩展
const (
StoreLocal string = "local"
StoreOss string = "oss"
StoreLocal string = "local"
StoreOss string = "oss"
VirtualRoot string = "virtualroot"
)

//分词器
Expand Down Expand Up @@ -81,6 +82,10 @@ func PrintInfo() {
fmt.Println("GitHash: ", GitHash)
}

func InitVirtualRoot() {
os.MkdirAll(VirtualRoot, os.ModePerm)
}

func GetLang(lang string) string {
if val, ok := langs.Load(lang); ok {
return val.(string)
Expand Down

0 comments on commit baff1b6

Please sign in to comment.