Browse Source

adding compressiong support!

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@59 282b0af5-e82d-9cf1-ede4-77906d7719d0
pull/2/head
chris.lu@gmail.com 13 years ago
parent
commit
f3a4125ee7
  1. 6
      weed-fs/src/cmd/weedvolume/weedvolume.go
  2. 36
      weed-fs/src/pkg/storage/compress.go
  3. 12
      weed-fs/src/pkg/storage/needle.go

6
weed-fs/src/cmd/weedvolume/weedvolume.go

@ -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

@ -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()
}

12
weed-fs/src/pkg/storage/needle.go

@ -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())
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…
Cancel
Save