s_buffer.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package backends
  2. import (
  3. "bytes"
  4. "github.com/flashmob/go-guerrilla/mail"
  5. "io"
  6. )
  7. // ----------------------------------------------------------------------------------
  8. // Processor Name: buffer
  9. // ----------------------------------------------------------------------------------
  10. // Description : Buffers the message data to envelope.Data
  11. // ----------------------------------------------------------------------------------
  12. // Config Options:
  13. // --------------:-------------------------------------------------------------------
  14. // Input :
  15. // ----------------------------------------------------------------------------------
  16. // Output : envelope.Data
  17. // ----------------------------------------------------------------------------------
  18. func init() {
  19. Streamers["buffer"] = func() *StreamDecorator {
  20. return StreamProcess()
  21. }
  22. }
  23. // Buffers to envelope.Data so that processors can be called on it at the end
  24. func StreamProcess() *StreamDecorator {
  25. sd := &StreamDecorator{}
  26. sd.Decorate =
  27. func(sp StreamProcessor, a ...interface{}) StreamProcessor {
  28. var envelope *mail.Envelope
  29. sd.Open = func(e *mail.Envelope) error {
  30. envelope = e
  31. return nil
  32. }
  33. return StreamProcessWith(func(p []byte) (int, error) {
  34. tr := io.TeeReader(bytes.NewReader(p), sp)
  35. n, err := envelope.Data.ReadFrom(tr)
  36. return int(n), err
  37. })
  38. }
  39. return sd
  40. }