本文整理汇总了Golang中mime/multipart.Writer类的典型用法代码示例。如果您正苦于以下问题:Golang Writer类的具体用法?Golang Writer怎么用?Golang Writer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Writer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeJSONPart
func writeJSONPart(writer *multipart.Writer, contentType string, body Body, compressed bool) (err error) {
bytes, err := json.Marshal(body)
if err != nil {
return err
}
if len(bytes) < kMinCompressedJSONSize {
compressed = false
}
partHeaders := textproto.MIMEHeader{}
partHeaders.Set("Content-Type", contentType)
if compressed {
partHeaders.Set("Content-Encoding", "gzip")
}
part, err := writer.CreatePart(partHeaders)
if err != nil {
return err
}
if compressed {
gz := gzip.NewWriter(part)
_, err = gz.Write(bytes)
gz.Close()
} else {
_, err = part.Write(bytes)
}
return
}
示例2: Write
// Write the attachment to the specified multipart writer.
func (a Attachment) Write(w *multipart.Writer) error {
headers := make(textproto.MIMEHeader)
if len(a.Filename) != 0 {
headers.Add("Content-Type", fmt.Sprintf("%s; name=%s", a.ContentType, a.Filename))
} else {
headers.Add("Content-Type", a.ContentType)
}
if a.Encoded {
headers.Add("Content-Transfer-Encoding", "base64")
} else {
headers.Add("Content-Transfer-Encoding", "quoted-printable")
}
p, err := w.CreatePart(headers)
if err != nil {
return err
}
if a.Encoded {
if _, err := p.Write([]byte(a.Content)); err != nil {
return err
}
} else {
q := quotedprintable.NewWriter(p)
if _, err := q.Write([]byte(a.Content)); err != nil {
return err
}
return q.Close()
}
return nil
}
示例3: writeMultipartStart
func (m *Message) writeMultipartStart(b *bytes.Buffer, writer *multipart.Writer) error {
multipart := `
Content-Type: multipart/alternative; charset="UTF-8"; boundary="%s"
`
_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
return err
}
示例4: Write
func Write(w *multipart.Writer, params map[string][]string) error {
for key, param1 := range params {
param := param1[0]
if len(param) > 0 && param[0] == '@' {
file := param[1:]
fw, err := w.CreateFormFile(key, file)
if err != nil {
log.Println("CreateFormFile failed:", err)
return err
}
fd, err := os.Open(file)
if err != nil {
log.Println("Open file failed:", err)
return err
} else {
_, err = io.Copy(fw, fd)
fd.Close()
if err != nil {
log.Println("Copy file failed:", err)
return err
}
}
} else {
err := w.WriteField(key, param)
if err != nil {
return err
}
}
}
return nil
}
示例5: WriteRevisionAsPart
// Adds a new part to the given multipart writer, containing the given revision.
// The revision will be written as a nested multipart body if it has attachments.
func (db *Database) WriteRevisionAsPart(revBody Body, isError bool, compressPart bool, writer *multipart.Writer) error {
partHeaders := textproto.MIMEHeader{}
docID, _ := revBody["_id"].(string)
revID, _ := revBody["_rev"].(string)
if len(docID) > 0 {
partHeaders.Set("X-Doc-ID", docID)
partHeaders.Set("X-Rev-ID", revID)
}
if hasInlineAttachments(revBody) {
// Write as multipart, including attachments:
// OPT: Find a way to do this w/o having to buffer the MIME body in memory!
var buffer bytes.Buffer
docWriter := multipart.NewWriter(&buffer)
contentType := fmt.Sprintf("multipart/related; boundary=%q",
docWriter.Boundary())
partHeaders.Set("Content-Type", contentType)
db.WriteMultipartDocument(revBody, docWriter, compressPart)
docWriter.Close()
content := bytes.TrimRight(buffer.Bytes(), "\r\n")
part, err := writer.CreatePart(partHeaders)
if err == nil {
_, err = part.Write(content)
}
return err
} else {
// Write as JSON:
contentType := "application/json"
if isError {
contentType += `; error="true"`
}
return writeJSONPart(writer, contentType, revBody, compressPart)
}
}
示例6: WriteMultipartDocument
// Writes a revision to a MIME multipart writer, encoding large attachments as separate parts.
func (db *Database) WriteMultipartDocument(body Body, writer *multipart.Writer) {
// First extract the attachments that should follow:
following := []attInfo{}
for name, value := range BodyAttachments(body) {
meta := value.(map[string]interface{})
var info attInfo
info.contentType, _ = meta["content_type"].(string)
info.data, _ = decodeAttachment(meta["data"])
if info.data != nil && len(info.data) > kMaxInlineAttachmentSize {
info.name = name
following = append(following, info)
delete(meta, "data")
meta["follows"] = true
}
}
// Write the main JSON body:
jsonOut, _ := json.Marshal(body)
partHeaders := textproto.MIMEHeader{}
partHeaders.Set("Content-Type", "application/json")
part, _ := writer.CreatePart(partHeaders)
part.Write(jsonOut)
// Write the following attachments
for _, info := range following {
partHeaders := textproto.MIMEHeader{}
if info.contentType != "" {
partHeaders.Set("Content-Type", info.contentType)
}
partHeaders.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", info.name))
part, _ := writer.CreatePart(partHeaders)
part.Write(info.data)
}
}
示例7: createFormFile
func createFormFile(writer *multipart.Writer, fieldname, filename string) {
// Try to open the file.
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
// Create a new form-data header with the provided field name and file name.
// Determine Content-Type of the file by its extension.
h := textproto.MIMEHeader{}
h.Set("Content-Disposition", fmt.Sprintf(
`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname),
escapeQuotes(filepath.Base(filename)),
))
h.Set("Content-Type", "application/octet-stream")
if ct := mime.TypeByExtension(filepath.Ext(filename)); ct != "" {
h.Set("Content-Type", ct)
}
part, err := writer.CreatePart(h)
if err != nil {
panic(err)
}
// Copy the content of the file we have opened not reading the whole
// file into memory.
_, err = io.Copy(part, file)
if err != nil {
panic(err)
}
}
示例8: createBlobletPartWriter
func createBlobletPartWriter(i int, path string, size int64, hash string, writer *multipart.Writer) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="bloblet-%d"; path="%s"; filename="%s"; hash="%s"`, i, path, bloblet.BlobletFileName, hash))
h.Set("Content-Type", "application/zip")
h.Set("Content-Length", fmt.Sprintf("%d", size))
h.Set("Content-Transfer-Encoding", "binary")
return writer.CreatePart(h)
}
示例9: writeField
func writeField(w multipart.Writer, fieldName string, fieldVal string) error {
if err := w.WriteField(fieldName, fieldVal); err != nil {
message := "Can not write field " + fieldName + " into the request. Reason: " + err.Error()
logging.Logger().Warn(message)
return errors.New(message)
}
return nil
}
示例10: writeMultipartEnd
func (m *Message) writeMultipartEnd(b *bytes.Buffer, writer *multipart.Writer) error {
multipart := `
--%s--
`
_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
return err
}
示例11: createZipPartWriter
func createZipPartWriter(zipStats os.FileInfo, writer *multipart.Writer) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="application"; filename="application.zip"`)
h.Set("Content-Type", "application/zip")
h.Set("Content-Length", fmt.Sprintf("%d", zipStats.Size()))
h.Set("Content-Transfer-Encoding", "binary")
return writer.CreatePart(h)
}
示例12: addFileReader
func addFileReader(w *multipart.Writer, f *File) error {
part, err := w.CreateFormFile(f.ParamName, f.Name)
if err != nil {
return err
}
_, err = io.Copy(part, f.Reader)
return err
}
示例13: addFormFileFromData
// Add a file to a multipart writer.
func addFormFileFromData(writer *multipart.Writer, name, path string, data []byte) error {
part, err := writer.CreateFormFile(name, filepath.Base(path))
if err != nil {
return err
}
_, err = part.Write(data)
return err
}
示例14: writeFields
func writeFields(mpw *multipart.Writer, fields map[string]string) error {
for k, v := range fields {
err := mpw.WriteField(k, v)
if err != nil {
return err
}
}
return nil
}
示例15: mwSet
func mwSet(mw *multipart.Writer, key, value string) error {
pw, err := mw.CreateFormField(key)
if err != nil {
return err
}
if _, err = pw.Write([]byte(value)); err != nil {
return err
}
return nil
}