-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_server.go
104 lines (93 loc) · 2.71 KB
/
file_server.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2021 fangyousong(方友松). All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package paddy
import (
"io"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/truexf/goutil"
)
const (
DefaultCacheFileSizeLimit = 30 * 1024 * 1024
ErrCodeFSNormal = 200
ErrCodeFSFileNotChanged = 201
ErrMsgFSFileNotChanged = "file not changed"
)
type FileServer struct {
maxCacheFileSize int64
fileCache map[string]*goutil.LRUFileCache
lock sync.RWMutex
}
func NewFileServer(maxCacheFileSize int64) *FileServer {
ret := &FileServer{maxCacheFileSize: maxCacheFileSize}
ret.fileCache = make(map[string]*goutil.LRUFileCache)
return ret
}
func (m *FileServer) getOrCreateCache(fileRootPath string) (*goutil.LRUFileCache, error) {
if fileRootPath == "" || !goutil.FilePathExists(fileRootPath) {
return nil, goutil.ErrorFileNotExists
}
m.lock.RLock()
ret, ok := m.fileCache[fileRootPath]
if ok {
m.lock.RUnlock()
return ret, nil
}
m.lock.RUnlock()
m.lock.Lock()
defer m.lock.Unlock()
ret = goutil.NewLRUFileCache(m.maxCacheFileSize, 0)
m.fileCache[fileRootPath] = ret
return ret, nil
}
func (m *FileServer) serve(fileRoot string, r *http.Request, w http.ResponseWriter) (done bool, e goutil.Error) {
if fileRoot == "" || r == nil || w == nil {
return false, goutil.NewError(ErrCodeFSNormal, "invalid param of FileServer.serve")
}
lruCache, err := m.getOrCreateCache(fileRoot)
if err != nil {
return false, goutil.NewError(ErrCodeFSNormal, err.Error())
}
since := time.Time{}
if ifModifiedSince := r.Header.Get("If-Modified-Since"); ifModifiedSince != "" {
if t, err := http.ParseTime(ifModifiedSince); err == nil {
since = t
}
}
fn := filepath.Join(fileRoot, r.URL.Path)
ext := filepath.Ext(fn)
if ext != "" {
ext = ext[1:]
}
if conentType, ok := MimeTypeMap[ext]; ok {
w.Header().Set("Content-Type", conentType)
}
ret, err := lruCache.Get(fn, since)
if err != nil {
if err == goutil.ErrorFileNotModified {
w.WriteHeader(304) // content not modified
return true, ErrorNoError
}
if err != goutil.ErrorFileSizeLimited {
return false, goutil.NewError(ErrCodeFSNormal, err.Error())
}
fd, err := os.Open(fn)
if err != nil {
return false, goutil.NewError(ErrCodeFSNormal, err.Error())
}
if _, err := io.Copy(w, fd); err != nil {
return false, goutil.NewError(ErrCodeFSNormal, err.Error())
}
if st, err := fd.Stat(); err == nil {
w.Header().Set("Last-Modified", st.ModTime().UTC().Format(http.TimeFormat))
}
return true, ErrorNoError
}
w.Header().Set("Last-Modified", ret.ModifyTime.UTC().Format(http.TimeFormat))
w.Write(ret.Data)
return true, ErrorNoError
}