gspath改进

This commit is contained in:
John
2018-08-26 21:36:39 +08:00
parent 032d73d016
commit d46b17f673
2 changed files with 11 additions and 11 deletions

View File

@ -142,7 +142,10 @@ func (s *Server)callServeHandler(h *handlerItem, r *Request) {
// http server静态文件处理path可以为相对路径也可以为绝对路径
func (s *Server)serveFile(r *Request, path string) {
path = s.paths.Search(path)
// 首先判断是否给定的path已经是一个绝对路径
if !gfile.Exists(path) {
path = s.paths.Search(path)
}
if path == "" {
r.Response.WriteStatus(http.StatusNotFound)
return

View File

@ -69,21 +69,18 @@ func (sp *SPath) Add(path string) error {
}
// 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串
// 给定的name只是相对文件路径或者只是一个文件名
func (sp *SPath) Search(name string) string {
path := sp.cache.Get(name)
if path == "" {
// 优先判断是否给定是已经是一个绝对路径
path = gfile.RealPath(name)
if path == "" {
sp.mu.RLock()
for _, v := range sp.paths {
path = gfile.RealPath(v + gfile.Separator + name)
if path != "" && gfile.Exists(path) {
break
}
sp.mu.RLock()
for _, v := range sp.paths {
path = gfile.RealPath(v + gfile.Separator + name)
if path != "" && gfile.Exists(path) {
break
}
sp.mu.RUnlock()
}
sp.mu.RUnlock()
if path != "" {
sp.cache.Set(name, path)
sp.addMonitor(name, path)