s_decompress.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package backends
  2. import (
  3. "bytes"
  4. "compress/zlib"
  5. "github.com/flashmob/go-guerrilla/mail"
  6. "io"
  7. "sync"
  8. )
  9. func init() {
  10. Streamers["decompress"] = func() *StreamDecorator {
  11. return StreamDecompress()
  12. }
  13. }
  14. // StreamDecompress is a PoC demonstrating how we can connect an io.Reader to our Writer
  15. // We use an io.Pipe to connect the two, writing to one end of the pipe, while
  16. // consuming the output on the other end of the pipe.
  17. func StreamDecompress() *StreamDecorator {
  18. sd := &StreamDecorator{}
  19. sd.Decorate =
  20. func(sp StreamProcessor, a ...interface{}) StreamProcessor {
  21. var (
  22. zr io.ReadCloser
  23. pr *io.PipeReader
  24. pw *io.PipeWriter
  25. )
  26. var wg sync.WaitGroup
  27. // consumer runs as a gorouitne.
  28. // It connects the zlib reader with the read-end of the pipe
  29. // then copies the output down to the next stream processor
  30. // consumer will exit of the pipe gets closed or on error
  31. consumer := func() {
  32. defer wg.Done()
  33. var err error
  34. for {
  35. if zr == nil {
  36. zr, err = zlib.NewReader(pr)
  37. if err != nil {
  38. _ = pr.CloseWithError(err)
  39. return
  40. }
  41. }
  42. _, err := io.Copy(sp, zr)
  43. if err != nil {
  44. _ = pr.CloseWithError(err)
  45. return
  46. }
  47. }
  48. }
  49. // start our consumer goroutine
  50. sd.Open = func(e *mail.Envelope) error {
  51. pr, pw = io.Pipe()
  52. wg.Add(1)
  53. go consumer()
  54. return nil
  55. }
  56. // close both ends of the pipes when finished
  57. sd.Close = func() error {
  58. // stop the consumer
  59. errR := pr.Close()
  60. errW := pw.Close()
  61. if zr != nil {
  62. if err := zr.Close(); err != nil {
  63. return err
  64. }
  65. }
  66. if errR != nil {
  67. return errR
  68. }
  69. if errW != nil {
  70. return errW
  71. }
  72. // wait for the consumer to stop
  73. wg.Wait()
  74. pr = nil
  75. pw = nil
  76. zr = nil
  77. return nil
  78. }
  79. return StreamProcessWith(func(p []byte) (n int, err error) {
  80. // take the output and copy on the pipe, for the consumer to pick up
  81. N, err := io.Copy(pw, bytes.NewReader(p))
  82. if N > 0 {
  83. n = int(N)
  84. }
  85. return
  86. })
  87. }
  88. return sd
  89. }