multipart

package module
v0.0.0-...-80e01f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 5 Imported by: 0

README

multipart

Made on the basis of mime/multipart

Example

package main

import (
	"context"
	"net/http"
	"time"

	"github.com/GoWebProd/multipart"
	"github.com/pkg/errors"
)

func main() {
	fileReq, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost/testfile", nil)
	if err != nil {
		panic(errors.Wrap(err, "Can't create new request"))
	}

	httpClient := &http.Client{
		Timeout: 5 * time.Second,
	}

	fileResp, err := httpClient.Do(fileReq)
	if err != nil {
		panic(errors.Wrap(err, "Can't file request"))
	}

	defer fileResp.Body.Close()

	writer := multipart.NewWriter()

	err = writer.CreateFormFileReader("content", "very_important_file_name", multipart.NewReader(fileResp.Body, int(fileResp.ContentLength)))
	if err != nil {
		panic(errors.Wrap(err, "Can't create file form"))
	}

	err = writer.CreateFormField("objectType", []byte("file"))
	if err != nil {
		panic(errors.Wrap(err, "Can't write object type field"))
	}
	
	defer writer.Close()

	req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://localhost/checkfile", writer)
	if err != nil {
		panic(errors.Wrap(err, "Can't create object scan request"))
	}

	req.ContentLength = int64(writer.Len())
	
	req.Header.Add("Content-Type", writer.FormDataContentType())

	resp, err := httpClient.Do(req)
	if err != nil {
		panic(errors.Wrap(err, "Can't do object scan request"))
	}

	defer resp.Body.Close()
}

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/GoWebProd/multipart
BenchmarkStd-16           465567              2602 ns/op            1345 B/op         37 allocs/op
BenchmarkThis-16         1557962               755 ns/op             275 B/op          7 allocs/op
PASS
ok      github.com/GoWebProd/multipart  4.510s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader interface {
	io.Reader

	Len() int
}

func NewReader

func NewReader(r io.Reader, length int) Reader

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer generates multipart messages.

func NewWriter

func NewWriter() *Writer

NewWriter returns a new multipart Writer with a random boundary.

func (*Writer) Boundary

func (w *Writer) Boundary() []byte

Boundary returns the Writer's boundary.

func (*Writer) Close

func (w *Writer) Close() error

func (*Writer) CreateFormField

func (w *Writer) CreateFormField(fieldname string, data []byte) error

CreateFormField creates part with a header using the given field name and data.

func (*Writer) CreateFormFieldReader

func (w *Writer) CreateFormFieldReader(fieldname string, data Reader) error

CreateFormFieldReader creates part with a header using the given field name and reader.

func (*Writer) CreateFormFile

func (w *Writer) CreateFormFile(fieldname string, filename string, data []byte) error

CreateFormFile creates a new form-data header with the provided field name and file name and data.

func (*Writer) CreateFormFileReader

func (w *Writer) CreateFormFileReader(fieldname string, filename string, data Reader) error

CreateFormFileReader creates a new form-data header with the provided field name and file name and reader.

func (*Writer) FormDataContentType

func (w *Writer) FormDataContentType() string

FormDataContentType returns the Content-Type for an HTTP multipart/form-data with this Writer's Boundary.

func (*Writer) Len

func (w *Writer) Len() int

func (*Writer) Read

func (w *Writer) Read(dst []byte) (int, error)

func (*Writer) Reset

func (w *Writer) Reset()

func (*Writer) SetBoundary

func (w *Writer) SetBoundary(boundary []byte) error

SetBoundary overrides the Writer's default randomly-generated boundary separator with an explicit value.

SetBoundary must be called before any parts are created, may only contain certain ASCII characters, and must be non-empty and at most 70 bytes long.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL