Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

adding compressiong support!

[email protected]f3a4125ee79b
diff --git a/weed-fs/src/cmd/weedvolume/weedvolume.go b/weed-fs/src/cmd/weedvolume/weedvolume.go
index 9d07856b85..1bd790946c 100644
--- a/weed-fs/src/cmd/weedvolume/weedvolume.go
+++ b/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)
}
diff --git a/weed-fs/src/pkg/storage/compress.go b/weed-fs/src/pkg/storage/compress.go
new file mode 100644
index 0000000000..9e63a9fe83
--- /dev/null
+++ b/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()
+}
diff --git a/weed-fs/src/pkg/storage/needle.go b/weed-fs/src/pkg/storage/needle.go
index ef3914e05b..b0d499c056 100644
--- a/weed-fs/src/pkg/storage/needle.go
+++ b/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())
- 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, ",")