You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.6 KiB

6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
  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/glog"
  10. "github.com/klauspost/compress/zstd"
  11. )
  12. func GzipData(input []byte) ([]byte, error) {
  13. buf := new(bytes.Buffer)
  14. w, _ := gzip.NewWriterLevel(buf, flate.BestSpeed)
  15. if _, err := w.Write(input); err != nil {
  16. glog.V(2).Infoln("error compressing data:", err)
  17. return nil, err
  18. }
  19. if err := w.Close(); err != nil {
  20. glog.V(2).Infoln("error closing compressed data:", err)
  21. return nil, err
  22. }
  23. return buf.Bytes(), nil
  24. }
  25. func DecompressData(input []byte) ([]byte, error) {
  26. if IsGzippedContent(input) {
  27. return ungzipData(input)
  28. }
  29. if IsZstdContent(input) {
  30. return unzstdData(input)
  31. }
  32. return nil, fmt.Errorf("unsupported compression")
  33. }
  34. func ungzipData(input []byte) ([]byte, error) {
  35. buf := bytes.NewBuffer(input)
  36. r, _ := gzip.NewReader(buf)
  37. defer r.Close()
  38. output, err := ioutil.ReadAll(r)
  39. if err != nil {
  40. glog.V(2).Infoln("error uncompressing data:", err)
  41. }
  42. return output, err
  43. }
  44. var zstdEncoder, _ = zstd.NewWriter(nil)
  45. func unzstdData(input []byte) ([]byte, error) {
  46. return zstdEncoder.EncodeAll(input, nil), nil
  47. }
  48. func IsGzippedContent(data []byte) bool {
  49. if len(data) < 2 {
  50. return false
  51. }
  52. return data[0] == 31 && data[1] == 139
  53. }
  54. func IsZstdContent(data []byte) bool {
  55. if len(data) < 4 {
  56. return false
  57. }
  58. return data[0] == 0xFD && data[1] == 0x2F && data[2] == 0xB5 && data[3] == 0x28
  59. }
  60. /*
  61. * Default not to compressed since compression can be done on client side.
  62. */func IsCompressableFileType(ext, mtype string) (shouldBeCompressed, iAmSure bool) {
  63. // text
  64. if strings.HasPrefix(mtype, "text/") {
  65. return true, true
  66. }
  67. // images
  68. switch ext {
  69. case ".svg", ".bmp", ".wav":
  70. return true, true
  71. }
  72. if strings.HasPrefix(mtype, "image/") {
  73. return false, true
  74. }
  75. // by file name extension
  76. switch ext {
  77. case ".zip", ".rar", ".gz", ".bz2", ".xz", ".zst":
  78. return false, true
  79. case ".pdf", ".txt", ".html", ".htm", ".css", ".js", ".json":
  80. return true, true
  81. case ".php", ".java", ".go", ".rb", ".c", ".cpp", ".h", ".hpp":
  82. return true, true
  83. case ".png", ".jpg", ".jpeg":
  84. return false, true
  85. }
  86. // by mime type
  87. if strings.HasPrefix(mtype, "application/") {
  88. if strings.HasSuffix(mtype, "zstd") {
  89. return false, true
  90. }
  91. if strings.HasSuffix(mtype, "xml") {
  92. return true, true
  93. }
  94. if strings.HasSuffix(mtype, "script") {
  95. return true, true
  96. }
  97. }
  98. if strings.HasPrefix(mtype, "audio/") {
  99. switch strings.TrimPrefix(mtype, "audio/") {
  100. case "wave", "wav", "x-wav", "x-pn-wav":
  101. return true, true
  102. }
  103. }
  104. return false, false
  105. }