attachment.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package socketio
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "reflect"
  8. )
  9. // Attachment is an attachment handler used in emit args. All attachments will be sent as binary data in the transport layer. When using an attachment, make sure it is a pointer.
  10. //
  11. // For example:
  12. //
  13. // type Arg struct {
  14. // Title string `json:"title"`
  15. // File *Attachment `json:"file"`
  16. // }
  17. //
  18. // f, _ := os.Open("./some_file")
  19. // arg := Arg{
  20. // Title: "some_file",
  21. // File: &Attachment{
  22. // Data: f,
  23. // }
  24. // }
  25. //
  26. // socket.Emit("send file", arg)
  27. // socket.On("get file", func(so Socket, arg Arg) {
  28. // b, _ := ioutil.ReadAll(arg.File.Data)
  29. // })
  30. type Attachment struct {
  31. Data io.ReadWriter
  32. num int
  33. }
  34. func encodeAttachments(v interface{}) []io.Reader {
  35. index := 0
  36. return encodeAttachmentValue(reflect.ValueOf(v), &index)
  37. }
  38. func encodeAttachmentValue(v reflect.Value, index *int) []io.Reader {
  39. v = reflect.Indirect(v)
  40. ret := []io.Reader{}
  41. if !v.IsValid() {
  42. return ret
  43. }
  44. switch v.Kind() {
  45. case reflect.Struct:
  46. if v.Type().Name() == "Attachment" {
  47. a, ok := v.Addr().Interface().(*Attachment)
  48. if !ok {
  49. panic("can't convert")
  50. }
  51. a.num = *index
  52. ret = append(ret, a.Data)
  53. (*index)++
  54. return ret
  55. }
  56. for i, n := 0, v.NumField(); i < n; i++ {
  57. var r []io.Reader
  58. r = encodeAttachmentValue(v.Field(i), index)
  59. ret = append(ret, r...)
  60. }
  61. case reflect.Map:
  62. if v.IsNil() {
  63. return ret
  64. }
  65. for _, key := range v.MapKeys() {
  66. var r []io.Reader
  67. r = encodeAttachmentValue(v.MapIndex(key), index)
  68. ret = append(ret, r...)
  69. }
  70. case reflect.Slice:
  71. if v.IsNil() {
  72. return ret
  73. }
  74. fallthrough
  75. case reflect.Array:
  76. for i, n := 0, v.Len(); i < n; i++ {
  77. var r []io.Reader
  78. r = encodeAttachmentValue(v.Index(i), index)
  79. ret = append(ret, r...)
  80. }
  81. case reflect.Interface:
  82. ret = encodeAttachmentValue(reflect.ValueOf(v.Interface()), index)
  83. }
  84. return ret
  85. }
  86. func decodeAttachments(v interface{}, binary [][]byte) error {
  87. return decodeAttachmentValue(reflect.ValueOf(v), binary)
  88. }
  89. func decodeAttachmentValue(v reflect.Value, binary [][]byte) error {
  90. v = reflect.Indirect(v)
  91. if !v.IsValid() {
  92. return fmt.Errorf("invalid value")
  93. }
  94. switch v.Kind() {
  95. case reflect.Struct:
  96. if v.Type().Name() == "Attachment" {
  97. a, ok := v.Addr().Interface().(*Attachment)
  98. if !ok {
  99. panic("can't convert")
  100. }
  101. if a.num >= len(binary) || a.num < 0 {
  102. return fmt.Errorf("out of range")
  103. }
  104. if a.Data == nil {
  105. a.Data = bytes.NewBuffer(nil)
  106. }
  107. for b := binary[a.num]; len(b) > 0; {
  108. n, err := a.Data.Write(b)
  109. if err != nil {
  110. return err
  111. }
  112. b = b[n:]
  113. }
  114. return nil
  115. }
  116. for i, n := 0, v.NumField(); i < n; i++ {
  117. if err := decodeAttachmentValue(v.Field(i), binary); err != nil {
  118. return err
  119. }
  120. }
  121. case reflect.Map:
  122. if v.IsNil() {
  123. return nil
  124. }
  125. for _, key := range v.MapKeys() {
  126. if err := decodeAttachmentValue(v.MapIndex(key), binary); err != nil {
  127. return err
  128. }
  129. }
  130. case reflect.Slice:
  131. if v.IsNil() {
  132. return nil
  133. }
  134. fallthrough
  135. case reflect.Array:
  136. for i, n := 0, v.Len(); i < n; i++ {
  137. if err := decodeAttachmentValue(v.Index(i), binary); err != nil {
  138. return err
  139. }
  140. }
  141. case reflect.Interface:
  142. if err := decodeAttachmentValue(reflect.ValueOf(v.Interface()), binary); err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }
  148. func (a Attachment) MarshalJSON() ([]byte, error) {
  149. return []byte(fmt.Sprintf("{\"_placeholder\":true,\"num\":%d}", a.num)), nil
  150. }
  151. func (a *Attachment) UnmarshalJSON(b []byte) error {
  152. var v struct {
  153. Num int `json:"num"`
  154. }
  155. if err := json.Unmarshal(b, &v); err != nil {
  156. return err
  157. }
  158. a.num = v.Num
  159. return nil
  160. }