| 94 | } |
| 95 | |
| 96 | func writeJSONPart(writer *multipart.Writer, contentType string, body db.Body, compressed bool) (err error) { |
| 97 | bytes, err := base.JSONMarshalCanonical(body) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | if len(bytes) < kMinCompressedJSONSize { |
| 102 | compressed = false |
| 103 | } |
| 104 | |
| 105 | partHeaders := textproto.MIMEHeader{} |
| 106 | partHeaders.Set("Content-Type", contentType) |
| 107 | if compressed { |
| 108 | partHeaders.Set("Content-Encoding", "gzip") |
| 109 | } |
| 110 | part, err := writer.CreatePart(partHeaders) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | if compressed { |
| 116 | gz := gzip.NewWriter(part) |
| 117 | _, err = gz.Write(bytes) |
| 118 | _ = gz.Close() |
| 119 | } else { |
| 120 | _, err = part.Write(bytes) |
| 121 | } |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | // Writes a revision to a MIME multipart writer, encoding large attachments as separate parts. |
| 126 | func WriteMultipartDocument(ctx context.Context, cblReplicationPullStats *base.CBLReplicationPullStats, body db.Body, writer *multipart.Writer, compress bool) { |