1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 19:41:28 +00:00

mem zip file var refactor

This commit is contained in:
JessonChan 2015-09-22 14:11:02 +08:00
parent 9c17f73489
commit 6ad215a9bb

View File

@ -29,72 +29,72 @@ import (
) )
var ( var (
gmfim = make(map[string]*memFileInfo) menFileInfoMap = make(map[string]*memFileInfo)
lock sync.RWMutex lock sync.RWMutex
) )
// OpenMemZipFile returns MemFile object with a compressed static file. // openMemZipFile returns MemFile object with a compressed static file.
// it's used for serve static file if gzip enable. // it's used for serve static file if gzip enable.
func openMemZipFile(path string, zip string) (*memFile, error) { func openMemZipFile(path string, zip string) (*memFile, error) {
osfile, e := os.Open(path) osFile, e := os.Open(path)
if e != nil { if e != nil {
return nil, e return nil, e
} }
defer osfile.Close() defer osFile.Close()
osfileinfo, e := osfile.Stat() osFileInfo, e := osFile.Stat()
if e != nil { if e != nil {
return nil, e return nil, e
} }
modtime := osfileinfo.ModTime() modTime := osFileInfo.ModTime()
fileSize := osfileinfo.Size() fileSize := osFileInfo.Size()
lock.RLock() lock.RLock()
cfi, ok := gmfim[zip+":"+path] cfi, ok := menFileInfoMap[zip+":"+path]
lock.RUnlock() lock.RUnlock()
if !(ok && cfi.ModTime() == modtime && cfi.fileSize == fileSize) { if !(ok && cfi.ModTime() == modTime && cfi.fileSize == fileSize) {
var content []byte var content []byte
if zip == "gzip" { if zip == "gzip" {
var zipbuf bytes.Buffer var zipBuf bytes.Buffer
gzipwriter, e := gzip.NewWriterLevel(&zipbuf, gzip.BestCompression) gzipWriter, e := gzip.NewWriterLevel(&zipBuf, gzip.BestCompression)
if e != nil { if e != nil {
return nil, e return nil, e
} }
_, e = io.Copy(gzipwriter, osfile) _, e = io.Copy(gzipWriter, osFile)
gzipwriter.Close() gzipWriter.Close()
if e != nil { if e != nil {
return nil, e return nil, e
} }
content, e = ioutil.ReadAll(&zipbuf) content, e = ioutil.ReadAll(&zipBuf)
if e != nil { if e != nil {
return nil, e return nil, e
} }
} else if zip == "deflate" { } else if zip == "deflate" {
var zipbuf bytes.Buffer var zipBuf bytes.Buffer
deflatewriter, e := flate.NewWriter(&zipbuf, flate.BestCompression) deflateWriter, e := flate.NewWriter(&zipBuf, flate.BestCompression)
if e != nil { if e != nil {
return nil, e return nil, e
} }
_, e = io.Copy(deflatewriter, osfile) _, e = io.Copy(deflateWriter, osFile)
deflatewriter.Close() deflateWriter.Close()
if e != nil { if e != nil {
return nil, e return nil, e
} }
content, e = ioutil.ReadAll(&zipbuf) content, e = ioutil.ReadAll(&zipBuf)
if e != nil { if e != nil {
return nil, e return nil, e
} }
} else { } else {
content, e = ioutil.ReadAll(osfile) content, e = ioutil.ReadAll(osFile)
if e != nil { if e != nil {
return nil, e return nil, e
} }
} }
cfi = &memFileInfo{osfileinfo, modtime, content, int64(len(content)), fileSize} cfi = &memFileInfo{osFileInfo, modTime, content, int64(len(content)), fileSize}
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
gmfim[zip+":"+path] = cfi menFileInfoMap[zip+":"+path] = cfi
} }
return &memFile{fi: cfi, offset: 0}, nil return &memFile{fi: cfi, offset: 0}, nil
} }
@ -139,7 +139,7 @@ func (fi *memFileInfo) Sys() interface{} {
return nil return nil
} }
// MemFile contains MemFileInfo and bytes offset when reading. // memFile contains MemFileInfo and bytes offset when reading.
// it implements io.Reader,io.ReadCloser and io.Seeker. // it implements io.Reader,io.ReadCloser and io.Seeker.
type memFile struct { type memFile struct {
fi *memFileInfo fi *memFileInfo
@ -198,7 +198,7 @@ func (f *memFile) Seek(offset int64, whence int) (ret int64, err error) {
return f.offset, nil return f.offset, nil
} }
// GetAcceptEncodingZip returns accept encoding format in http header. // getAcceptEncodingZip returns accept encoding format in http header.
// zip is first, then deflate if both accepted. // zip is first, then deflate if both accepted.
// If no accepted, return empty string. // If no accepted, return empty string.
func getAcceptEncodingZip(r *http.Request) string { func getAcceptEncodingZip(r *http.Request) string {
@ -206,9 +206,9 @@ func getAcceptEncodingZip(r *http.Request) string {
ss = strings.ToLower(ss) ss = strings.ToLower(ss)
if strings.Contains(ss, "gzip") { if strings.Contains(ss, "gzip") {
return "gzip" return "gzip"
} else if strings.Contains(ss, "deflate") { }
if strings.Contains(ss, "deflate") {
return "deflate" return "deflate"
} else { }
return "" return ""
} }
}