|
@ -8,9 +8,8 @@ import ( |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
"golang.org/x/tools/godoc/util" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
|
|
|
"github.com/klauspost/compress/zstd" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func GzipData(input []byte) ([]byte, error) { |
|
|
func GzipData(input []byte) ([]byte, error) { |
|
@ -30,6 +29,9 @@ func DecompressData(input []byte) ([]byte, error) { |
|
|
if IsGzippedContent(input) { |
|
|
if IsGzippedContent(input) { |
|
|
return ungzipData(input) |
|
|
return ungzipData(input) |
|
|
} |
|
|
} |
|
|
|
|
|
if IsZstdContent(input) { |
|
|
|
|
|
return unzstdData(input) |
|
|
|
|
|
} |
|
|
return nil, fmt.Errorf("unsupported compression") |
|
|
return nil, fmt.Errorf("unsupported compression") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -44,19 +46,10 @@ func ungzipData(input []byte) ([]byte, error) { |
|
|
return output, err |
|
|
return output, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Default more not to gzip since gzip can be done on client side. |
|
|
|
|
|
*/ |
|
|
|
|
|
func IsGzippable(ext, mtype string, data []byte) bool { |
|
|
|
|
|
|
|
|
|
|
|
shouldBeZipped, iAmSure := IsGzippableFileType(ext, mtype) |
|
|
|
|
|
if iAmSure { |
|
|
|
|
|
return shouldBeZipped |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
isMostlyText := util.IsText(data) |
|
|
|
|
|
|
|
|
var zstdEncoder, _ = zstd.NewWriter(nil) |
|
|
|
|
|
|
|
|
return isMostlyText |
|
|
|
|
|
|
|
|
func unzstdData(input []byte) ([]byte, error) { |
|
|
|
|
|
return zstdEncoder.EncodeAll(input, nil), nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func IsGzippedContent(data []byte) bool { |
|
|
func IsGzippedContent(data []byte) bool { |
|
@ -66,9 +59,16 @@ func IsGzippedContent(data []byte) bool { |
|
|
return data[0] == 31 && data[1] == 139 |
|
|
return data[0] == 31 && data[1] == 139 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func IsZstdContent(data []byte) bool { |
|
|
|
|
|
if len(data) < 4 { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
return data[0] == 0xFD && data[1] == 0x2F && data[2] == 0xB5 && data[3] == 0x28 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* Default more not to gzip since gzip can be done on client side. |
|
|
|
|
|
*/func IsGzippableFileType(ext, mtype string) (shouldBeZipped, iAmSure bool) { |
|
|
|
|
|
|
|
|
* Default not to compressed since compression can be done on client side. |
|
|
|
|
|
*/func IsCompressableFileType(ext, mtype string) (shouldBeCompressed, iAmSure bool) { |
|
|
|
|
|
|
|
|
// text
|
|
|
// text
|
|
|
if strings.HasPrefix(mtype, "text/") { |
|
|
if strings.HasPrefix(mtype, "text/") { |
|
@ -86,7 +86,7 @@ func IsGzippedContent(data []byte) bool { |
|
|
|
|
|
|
|
|
// by file name extension
|
|
|
// by file name extension
|
|
|
switch ext { |
|
|
switch ext { |
|
|
case ".zip", ".rar", ".gz", ".bz2", ".xz": |
|
|
|
|
|
|
|
|
case ".zip", ".rar", ".gz", ".bz2", ".xz", ".zst": |
|
|
return false, true |
|
|
return false, true |
|
|
case ".pdf", ".txt", ".html", ".htm", ".css", ".js", ".json": |
|
|
case ".pdf", ".txt", ".html", ".htm", ".css", ".js", ".json": |
|
|
return true, true |
|
|
return true, true |
|
@ -98,13 +98,15 @@ func IsGzippedContent(data []byte) bool { |
|
|
|
|
|
|
|
|
// by mime type
|
|
|
// by mime type
|
|
|
if strings.HasPrefix(mtype, "application/") { |
|
|
if strings.HasPrefix(mtype, "application/") { |
|
|
|
|
|
if strings.HasSuffix(mtype, "zstd") { |
|
|
|
|
|
return false, true |
|
|
|
|
|
} |
|
|
if strings.HasSuffix(mtype, "xml") { |
|
|
if strings.HasSuffix(mtype, "xml") { |
|
|
return true, true |
|
|
return true, true |
|
|
} |
|
|
} |
|
|
if strings.HasSuffix(mtype, "script") { |
|
|
if strings.HasSuffix(mtype, "script") { |
|
|
return true, true |
|
|
return true, true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.HasPrefix(mtype, "audio/") { |
|
|
if strings.HasPrefix(mtype, "audio/") { |
|
|