mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-01-18 14:41:31 +08:00
adding compressiong support!
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@59 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
parent
2e1ffa189b
commit
f3a4125ee7
@ -62,7 +62,11 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if ext != "" {
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||
mtype := mime.TypeByExtension(ext)
|
||||
w.Header().Set("Content-Type", mtype)
|
||||
if storage.IsCompressable(ext, mtype){
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
}
|
||||
w.Write(n.Data)
|
||||
}
|
||||
|
36
weed-fs/src/pkg/storage/compress.go
Normal file
36
weed-fs/src/pkg/storage/compress.go
Normal file
@ -0,0 +1,36 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"compress/gzip"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func IsCompressable(ext, mtype string) bool {
|
||||
if ext == ".zip" {
|
||||
return true
|
||||
}
|
||||
if ext == ".rar" {
|
||||
return true
|
||||
}
|
||||
if strings.Index(mtype,"text/")==0 {
|
||||
return true
|
||||
}
|
||||
if strings.Index(mtype,"application/")==0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func GzipData(input []byte) []byte {
|
||||
buf := new(bytes.Buffer)
|
||||
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
|
||||
if _, err := w.Write(input); err!=nil {
|
||||
log.Printf("error compressing data:%s\n", err)
|
||||
}
|
||||
if err := w.Close(); err!=nil {
|
||||
log.Printf("error closing compressed data:%s\n", err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"pkg/util"
|
||||
@ -31,8 +32,17 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
|
||||
return
|
||||
}
|
||||
part, _ := form.NextPart()
|
||||
//log.Println("uploading file " + part.FileName())
|
||||
data, _ := ioutil.ReadAll(part)
|
||||
fname := part.FileName()
|
||||
data, _ := ioutil.ReadAll(part)
|
||||
//log.Println("uploading file " + part.FileName())
|
||||
dotIndex := strings.LastIndex(fname, ".")
|
||||
if dotIndex > 0 {
|
||||
ext := fname[dotIndex:]
|
||||
mtype := mime.TypeByExtension(ext)
|
||||
if IsCompressable(ext, mtype){
|
||||
data = GzipData(data)
|
||||
}
|
||||
}
|
||||
n.Data = data
|
||||
|
||||
commaSep := strings.LastIndex(r.URL.Path, ",")
|
||||
|
Loading…
Reference in New Issue
Block a user