1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:00:54 +00:00

bytes reader replace string reader

This commit is contained in:
JessonChan 2015-11-12 11:44:29 +08:00
parent f8db8ae9c3
commit 214030fad4
2 changed files with 7 additions and 12 deletions

View File

@ -15,6 +15,7 @@
package context package context
import ( import (
"bytes"
"compress/flate" "compress/flate"
"compress/gzip" "compress/gzip"
"io" "io"
@ -22,30 +23,24 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"io/ioutil"
) )
// WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate) // WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate)
func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error) { func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error) {
content, err := ioutil.ReadAll(file) return writeLevel(encoding, writer, file, flate.BestCompression)
if err != nil {
return false, "", err
}
return writeLevel(encoding, writer, content, flate.BestCompression)
} }
// WriteBody reads writes content to writer by the specific encoding(gzip/deflate) // WriteBody reads writes content to writer by the specific encoding(gzip/deflate)
func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error) { func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error) {
return writeLevel(encoding, writer, content, flate.BestSpeed) return writeLevel(encoding, writer, bytes.NewReader(content), flate.BestSpeed)
} }
// writeLevel reads from reader,writes to writer by specific encoding and compress level // writeLevel reads from reader,writes to writer by specific encoding and compress level
// the compress level is defined by deflate package // the compress level is defined by deflate package
func writeLevel(encoding string, writer io.Writer, content []byte, level int) (bool, string, error) { func writeLevel(encoding string, writer io.Writer, reader io.Reader, level int) (bool, string, error) {
var outputWriter io.Writer var outputWriter io.Writer
var err error var err error
if cf, ok := encoderMap[encoding]; ok { if cf, ok := encoderMap[encoding]; ok {
@ -57,7 +52,7 @@ func writeLevel(encoding string, writer io.Writer, content []byte, level int) (b
if err != nil { if err != nil {
return false, "", err return false, "", err
} }
outputWriter.Write(content) io.Copy(outputWriter, reader)
switch outputWriter.(type) { switch outputWriter.(type) {
case io.WriteCloser: case io.WriteCloser:
outputWriter.(io.WriteCloser).Close() outputWriter.(io.WriteCloser).Close()

View File

@ -87,7 +87,7 @@ func serverStaticRouter(ctx *context.Context) {
} }
type serveContentHolder struct { type serveContentHolder struct {
*strings.Reader *bytes.Reader
modTime time.Time modTime time.Time
size int64 size int64
encoding string encoding string
@ -120,7 +120,7 @@ func openFile(filePath string, fi os.FileInfo, acceptEncoding string) (bool, str
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }
mapFile = &serveContentHolder{Reader: strings.NewReader(string(bufferWriter.Bytes())), modTime: fi.ModTime(), size: int64(bufferWriter.Len()), encoding: n} mapFile = &serveContentHolder{Reader: bytes.NewReader(bufferWriter.Bytes()), modTime: fi.ModTime(), size: int64(bufferWriter.Len()), encoding: n}
staticFileMap[mapKey] = mapFile staticFileMap[mapKey] = mapFile
} }