compression.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package util
  2. import (
  3. "bytes"
  4. "compress/flate"
  5. "compress/gzip"
  6. "fmt"
  7. "io/ioutil"
  8. "strings"
  9. "github.com/chrislusf/seaweedfs/weed/util/log"
  10. "github.com/klauspost/compress/zstd"
  11. )
  12. var (
  13. UnsupportedCompression = fmt.Errorf("unsupported compression")
  14. )
  15. func MaybeGzipData(input []byte) []byte {
  16. if IsGzippedContent(input) {
  17. return input
  18. }
  19. gzipped, err := GzipData(input)
  20. if err != nil {
  21. return input
  22. }
  23. if len(gzipped)*10 > len(input)*9 {
  24. return input
  25. }
  26. return gzipped
  27. }
  28. func MaybeDecompressData(input []byte) []byte {
  29. uncompressed, err := DecompressData(input)
  30. if err != nil {
  31. if err != UnsupportedCompression {
  32. log.Errorf("decompressed data: %v", err)
  33. }
  34. return input
  35. }
  36. return uncompressed
  37. }
  38. func GzipData(input []byte) ([]byte, error) {
  39. buf := new(bytes.Buffer)
  40. w, _ := gzip.NewWriterLevel(buf, flate.BestSpeed)
  41. if _, err := w.Write(input); err != nil {
  42. log.Debugf("error gzip data: %v", err)
  43. return nil, err
  44. }
  45. if err := w.Close(); err != nil {
  46. log.Debugf("error closing gzipped data: %v", err)
  47. return nil, err
  48. }
  49. return buf.Bytes(), nil
  50. }
  51. var zstdEncoder, _ = zstd.NewWriter(nil)
  52. func ZstdData(input []byte) ([]byte, error) {
  53. return zstdEncoder.EncodeAll(input, nil), nil
  54. }
  55. func DecompressData(input []byte) ([]byte, error) {
  56. if IsGzippedContent(input) {
  57. return ungzipData(input)
  58. }
  59. if IsZstdContent(input) {
  60. return unzstdData(input)
  61. }
  62. return input, UnsupportedCompression
  63. }
  64. func ungzipData(input []byte) ([]byte, error) {
  65. buf := bytes.NewBuffer(input)
  66. r, _ := gzip.NewReader(buf)
  67. defer r.Close()
  68. output, err := ioutil.ReadAll(r)
  69. if err != nil {
  70. log.Debugf("error ungzip data: %v", err)
  71. }
  72. return output, err
  73. }
  74. var decoder, _ = zstd.NewReader(nil)
  75. func unzstdData(input []byte) ([]byte, error) {
  76. return decoder.DecodeAll(input, nil)
  77. }
  78. func IsGzippedContent(data []byte) bool {
  79. if len(data) < 2 {
  80. return false
  81. }
  82. return data[0] == 31 && data[1] == 139
  83. }
  84. func IsZstdContent(data []byte) bool {
  85. if len(data) < 4 {
  86. return false
  87. }
  88. return data[3] == 0xFD && data[2] == 0x2F && data[1] == 0xB5 && data[0] == 0x28
  89. }
  90. /*
  91. * Default not to compressed since compression can be done on client side.
  92. */func IsCompressableFileType(ext, mtype string) (shouldBeCompressed, iAmSure bool) {
  93. // text
  94. if strings.HasPrefix(mtype, "text/") {
  95. return true, true
  96. }
  97. // images
  98. switch ext {
  99. case ".svg", ".bmp", ".wav":
  100. return true, true
  101. }
  102. if strings.HasPrefix(mtype, "image/") {
  103. return false, true
  104. }
  105. // by file name extension
  106. switch ext {
  107. case ".zip", ".rar", ".gz", ".bz2", ".xz", ".zst":
  108. return false, true
  109. case ".pdf", ".txt", ".html", ".htm", ".css", ".js", ".json":
  110. return true, true
  111. case ".php", ".java", ".go", ".rb", ".c", ".cpp", ".h", ".hpp":
  112. return true, true
  113. case ".png", ".jpg", ".jpeg":
  114. return false, true
  115. }
  116. // by mime type
  117. if strings.HasPrefix(mtype, "application/") {
  118. if strings.HasSuffix(mtype, "zstd") {
  119. return false, true
  120. }
  121. if strings.HasSuffix(mtype, "xml") {
  122. return true, true
  123. }
  124. if strings.HasSuffix(mtype, "script") {
  125. return true, true
  126. }
  127. }
  128. if strings.HasPrefix(mtype, "audio/") {
  129. switch strings.TrimPrefix(mtype, "audio/") {
  130. case "wave", "wav", "x-wav", "x-pn-wav":
  131. return true, true
  132. }
  133. }
  134. return false, false
  135. }