Skip to content

Commit

Permalink
fix: 优化示例
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz authored Dec 21, 2023
1 parent f5dd1e8 commit 1e43e22
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions example/httpserver/serve_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,36 @@ package main

import (
"net/http"
"os"
"strings"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

// pathMap is used for URL mapping
var pathMap = map[string]string{
"/aaa/": "./",
"/aaa/": "/tmp/",
}

// ServeFile serves the file to the response.
func ServeFile(r *ghttp.Request) {
truePath := r.URL.Path
hasPrefix := false
// Replace the path prefix.
for k, v := range pathMap {
if strings.HasPrefix(truePath, k) {
truePath = strings.Replace(truePath, k, v, 1)
truePath = strings.Replace(truePath, k, v, 1) // Replace only once.
hasPrefix = true
break
}
}

// Use file from dist.
file, err := os.Open(truePath)
if err != nil {
if !hasPrefix {
r.Response.WriteStatus(http.StatusForbidden)
return
}
defer file.Close()

// Clear the response buffer before file serving.
// It ignores all custom buffer content and uses the file content.
r.Response.ClearBuffer()

info, _ := file.Stat()
if info.IsDir() {
r.Response.WriteStatus(http.StatusForbidden)
} else {
r.Response.ServeContent(info.Name(), info.ModTime(), file)
}
r.Response.ServeFile(truePath)
}

func main() {
Expand Down

0 comments on commit 1e43e22

Please sign in to comment.