Chris Lu
12 years ago
1 changed files with 58 additions and 38 deletions
@ -1,49 +1,69 @@ |
|||
package storage |
|||
|
|||
import ( |
|||
"bytes" |
|||
"compress/flate" |
|||
"compress/gzip" |
|||
"io/ioutil" |
|||
"strings" |
|||
"bytes" |
|||
"compress/flate" |
|||
"compress/gzip" |
|||
"io/ioutil" |
|||
"strings" |
|||
) |
|||
|
|||
/* |
|||
* Default more not to gzip since gzip can be done on client side. |
|||
*/ |
|||
func IsGzippable(ext, mtype string) bool { |
|||
if ext == ".zip" { |
|||
return false |
|||
} |
|||
if ext == ".rar" { |
|||
return false |
|||
} |
|||
if ext == ".gz" { |
|||
return false |
|||
} |
|||
if strings.Index(mtype,"text/")==0 { |
|||
return true |
|||
} |
|||
if strings.Index(mtype,"application/")==0 { |
|||
return true |
|||
} |
|||
return false |
|||
if strings.HasPrefix(mtype, "text/"){ |
|||
return true |
|||
} |
|||
if ext == ".zip" { |
|||
return false |
|||
} |
|||
if ext == ".rar" { |
|||
return false |
|||
} |
|||
if ext == ".gz" { |
|||
return false |
|||
} |
|||
if ext == ".pdf" { |
|||
return true |
|||
} |
|||
if ext == ".css" { |
|||
return true |
|||
} |
|||
if ext == ".js" { |
|||
return true |
|||
} |
|||
if ext == ".json" { |
|||
return true |
|||
} |
|||
if strings.HasPrefix(mtype, "application/") { |
|||
if strings.HasSuffix(mtype, "xml") { |
|||
return true |
|||
} |
|||
if strings.HasSuffix(mtype, "script") { |
|||
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 { |
|||
println("error compressing data:", err) |
|||
} |
|||
if err := w.Close(); err!=nil { |
|||
println("error closing compressed data:", err) |
|||
} |
|||
return buf.Bytes() |
|||
buf := new(bytes.Buffer) |
|||
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) |
|||
if _, err := w.Write(input); err != nil { |
|||
println("error compressing data:", err) |
|||
} |
|||
if err := w.Close(); err != nil { |
|||
println("error closing compressed data:", err) |
|||
} |
|||
return buf.Bytes() |
|||
} |
|||
func UnGzipData(input []byte) []byte { |
|||
buf := bytes.NewBuffer(input) |
|||
r, _ := gzip.NewReader(buf) |
|||
defer r.Close() |
|||
output, err := ioutil.ReadAll(r) |
|||
if err!=nil { |
|||
println("error uncompressing data:", err) |
|||
} |
|||
return output |
|||
buf := bytes.NewBuffer(input) |
|||
r, _ := gzip.NewReader(buf) |
|||
defer r.Close() |
|||
output, err := ioutil.ReadAll(r) |
|||
if err != nil { |
|||
println("error uncompressing data:", err) |
|||
} |
|||
return output |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue