-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b178c03
commit baff1b6
Showing
7 changed files
with
80 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,5 @@ render.jpg | |
version_control | ||
node_modules | ||
dist | ||
output | ||
output | ||
virtualroot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,6 @@ func main() { | |
os.Exit(1) | ||
} | ||
utils.PrintInfo() | ||
utils.InitVirtualRoot() | ||
s.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters