github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/courier/transport_http/file.go (about)

     1  package transport_http
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	"github.com/johnnyeven/libtools/courier"
     8  )
     9  
    10  func NewFile(filename string, contentType string) *File {
    11  	return &File{
    12  		filename:    filename,
    13  		contentType: contentType,
    14  	}
    15  }
    16  
    17  var _ interface {
    18  	io.Reader
    19  	io.Writer
    20  	IContentType
    21  	courier.IMeta
    22  } = (*File)(nil)
    23  
    24  // swagger:strfmt binary
    25  type File struct {
    26  	filename    string
    27  	contentType string
    28  	buf         bytes.Buffer
    29  }
    30  
    31  func (file *File) Read(p []byte) (n int, err error) {
    32  	return file.buf.Read(p)
    33  }
    34  
    35  func (file *File) Write(p []byte) (n int, err error) {
    36  	return file.buf.Write(p)
    37  }
    38  
    39  func (file *File) Meta() courier.Metadata {
    40  	metadata := courier.Metadata{}
    41  	metadata.Add("Content-Disposition", "attachment; filename="+file.filename)
    42  	return metadata
    43  }
    44  
    45  func (file *File) ContentType() string {
    46  	return file.contentType
    47  }