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.

128 lines
2.6 KiB

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