-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
52 lines (47 loc) · 1.3 KB
/
static.go
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
package goi
import (
"fmt"
"net/http"
"os"
"path"
"time"
)
// 静态文件处理函数
func metaStaticHandler(request *Request) any {
staticPattern := request.PathParams.Get("staticPattern").(string)
staticPath := request.PathParams.Get("staticPath").(string)
absolutePath := path.Join(staticPattern, staticPath)
if _, err := os.Stat(absolutePath); os.IsNotExist(err) {
return Response{
Status: http.StatusNotFound,
Data: nil,
}
}
file, err := os.Open(absolutePath)
if err != nil {
file.Close()
return Response{
Status: http.StatusInternalServerError,
Data: "读取文件失败!",
}
}
return file
}
// 返回响应文件内容
func metaResponseStatic(file *os.File, request *Request, response http.ResponseWriter) (int64, []byte) {
defer file.Close()
fileInfo, err := file.Stat()
if os.IsNotExist(err) {
response.WriteHeader(http.StatusNotFound)
return 0, []byte("文件不存在")
} else if err != nil {
response.WriteHeader(http.StatusNotFound)
return 0, []byte(fmt.Sprintf("获取文件状态失败: %v", err))
}
if fileInfo.IsDir() {
response.WriteHeader(http.StatusNotFound)
return 0, []byte("不是一个文件")
}
http.ServeContent(response, request.Object, fileInfo.Name(), time.Now().In(engine.Settings.LOCATION), file)
return fileInfo.Size(), nil
}