2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-12-14 18:33:12 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-01-05 15:16:47 +00:00
|
|
|
"sync"
|
2013-12-14 18:33:12 +00:00
|
|
|
"time"
|
2015-11-10 05:10:42 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/acceptencoder"
|
2013-12-14 18:33:12 +00:00
|
|
|
)
|
|
|
|
|
2015-09-08 13:41:38 +00:00
|
|
|
var (
|
2015-09-22 06:11:02 +00:00
|
|
|
menFileInfoMap = make(map[string]*memFileInfo)
|
|
|
|
lock sync.RWMutex
|
2015-09-08 13:41:38 +00:00
|
|
|
)
|
2013-12-14 18:33:12 +00:00
|
|
|
|
2015-09-22 06:11:02 +00:00
|
|
|
// openMemZipFile returns MemFile object with a compressed static file.
|
2013-12-21 05:19:24 +00:00
|
|
|
// it's used for serve static file if gzip enable.
|
2014-04-05 16:18:21 +00:00
|
|
|
func openMemZipFile(path string, zip string) (*memFile, error) {
|
2015-09-22 06:11:02 +00:00
|
|
|
osFile, e := os.Open(path)
|
2013-12-14 18:33:12 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2015-09-22 06:11:02 +00:00
|
|
|
defer osFile.Close()
|
2013-12-14 18:33:12 +00:00
|
|
|
|
2015-09-22 06:11:02 +00:00
|
|
|
osFileInfo, e := osFile.Stat()
|
2013-12-14 18:33:12 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
|
2015-09-22 06:11:02 +00:00
|
|
|
modTime := osFileInfo.ModTime()
|
|
|
|
fileSize := osFileInfo.Size()
|
2015-09-22 14:02:56 +00:00
|
|
|
mapKey := zip + ":" + path
|
2014-01-05 15:16:47 +00:00
|
|
|
lock.RLock()
|
2015-09-22 14:02:56 +00:00
|
|
|
cfi, ok := menFileInfoMap[mapKey]
|
2014-01-05 15:16:47 +00:00
|
|
|
lock.RUnlock()
|
2015-09-22 06:11:02 +00:00
|
|
|
if !(ok && cfi.ModTime() == modTime && cfi.fileSize == fileSize) {
|
2015-09-22 14:02:56 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
if cfi, ok = menFileInfoMap[mapKey]; !ok {
|
|
|
|
cfi, e = newMenFileInfo(osFile, osFileInfo, zip)
|
2013-12-14 18:33:12 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2015-09-22 14:02:56 +00:00
|
|
|
menFileInfoMap[mapKey] = cfi
|
2013-12-14 18:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 14:02:56 +00:00
|
|
|
|
2014-04-05 16:18:21 +00:00
|
|
|
return &memFile{fi: cfi, offset: 0}, nil
|
2013-12-14 18:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 14:02:56 +00:00
|
|
|
// memFileInfo contains a compressed file bytes and file information.
|
2013-12-21 05:19:24 +00:00
|
|
|
// it implements os.FileInfo interface.
|
2014-04-05 16:18:21 +00:00
|
|
|
type memFileInfo struct {
|
2013-12-14 18:33:12 +00:00
|
|
|
os.FileInfo
|
|
|
|
modTime time.Time
|
|
|
|
content []byte
|
|
|
|
contentSize int64
|
|
|
|
fileSize int64
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:02:56 +00:00
|
|
|
// newMenFileInfo return a memFileInfo from file by zip type
|
|
|
|
func newMenFileInfo(file *os.File, fileInfo os.FileInfo, zip string) (*memFileInfo, error) {
|
|
|
|
var content []byte
|
|
|
|
var zipBuf bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
2015-11-10 02:55:47 +00:00
|
|
|
_, _, err = acceptencoder.WriteFile(zip, &zipBuf, file)
|
2015-09-22 14:02:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadAll(&zipBuf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &memFileInfo{
|
|
|
|
FileInfo: fileInfo,
|
|
|
|
modTime: fileInfo.ModTime(),
|
|
|
|
content: content,
|
|
|
|
contentSize: int64(len(content)),
|
|
|
|
fileSize: fileInfo.Size(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Name returns the compressed filename.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) Name() string {
|
2013-12-14 18:33:12 +00:00
|
|
|
return fi.Name()
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Size returns the raw file content size, not compressed size.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) Size() int64 {
|
2013-12-14 18:33:12 +00:00
|
|
|
return fi.contentSize
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Mode returns file mode.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) Mode() os.FileMode {
|
2013-12-14 18:33:12 +00:00
|
|
|
return fi.Mode()
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// ModTime returns the last modified time of raw file.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) ModTime() time.Time {
|
2013-12-14 18:33:12 +00:00
|
|
|
return fi.modTime
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// IsDir returns the compressing file is a directory or not.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) IsDir() bool {
|
2013-12-14 18:33:12 +00:00
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// return nil. implement the os.FileInfo interface method.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (fi *memFileInfo) Sys() interface{} {
|
2013-12-14 18:33:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 06:11:02 +00:00
|
|
|
// memFile contains MemFileInfo and bytes offset when reading.
|
2013-12-21 05:19:24 +00:00
|
|
|
// it implements io.Reader,io.ReadCloser and io.Seeker.
|
2014-04-05 16:18:21 +00:00
|
|
|
type memFile struct {
|
|
|
|
fi *memFileInfo
|
2013-12-14 18:33:12 +00:00
|
|
|
offset int64
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Close memfile.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (f *memFile) Close() error {
|
2013-12-14 18:33:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Get os.FileInfo of memfile.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (f *memFile) Stat() (os.FileInfo, error) {
|
2013-12-14 18:33:12 +00:00
|
|
|
return f.fi, nil
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// read os.FileInfo of files in directory of memfile.
|
|
|
|
// it returns empty slice.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (f *memFile) Readdir(count int) ([]os.FileInfo, error) {
|
2013-12-14 18:33:12 +00:00
|
|
|
infos := []os.FileInfo{}
|
|
|
|
|
|
|
|
return infos, nil
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Read bytes from the compressed file bytes.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (f *memFile) Read(p []byte) (n int, err error) {
|
2013-12-14 18:33:12 +00:00
|
|
|
if len(f.fi.content)-int(f.offset) >= len(p) {
|
|
|
|
n = len(p)
|
|
|
|
} else {
|
|
|
|
n = len(f.fi.content) - int(f.offset)
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
copy(p, f.fi.content[f.offset:f.offset+int64(n)])
|
|
|
|
f.offset += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var errWhence = errors.New("Seek: invalid whence")
|
|
|
|
var errOffset = errors.New("Seek: invalid offset")
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// Read bytes from the compressed file bytes by seeker.
|
2014-04-05 16:18:21 +00:00
|
|
|
func (f *memFile) Seek(offset int64, whence int) (ret int64, err error) {
|
2013-12-14 18:33:12 +00:00
|
|
|
switch whence {
|
|
|
|
default:
|
|
|
|
return 0, errWhence
|
|
|
|
case os.SEEK_SET:
|
|
|
|
case os.SEEK_CUR:
|
|
|
|
offset += f.offset
|
|
|
|
case os.SEEK_END:
|
|
|
|
offset += int64(len(f.fi.content))
|
|
|
|
}
|
|
|
|
if offset < 0 || int(offset) > len(f.fi.content) {
|
|
|
|
return 0, errOffset
|
|
|
|
}
|
|
|
|
f.offset = offset
|
|
|
|
return f.offset, nil
|
|
|
|
}
|