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.

118 lines
2.4 KiB

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