github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/communicator/communication/UploadableFile.go (about)

     1  package communication
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  // UploadableFile represents a file that can be uploaded.
    10  type UploadableFile struct {
    11  	fileName string
    12  	content io.Reader
    13  	contentType string
    14  	contentLength int64
    15  }
    16  
    17  // GetFileName returns the name of the file.
    18  func (f *UploadableFile) GetFileName() string {
    19  	return f.fileName
    20  }
    21  
    22  // GetContent returns a reader with the file's content.
    23  func (f *UploadableFile) GetContent() io.Reader {
    24  	return f.content
    25  }
    26  
    27  // GetContentType returns the file's content type.
    28  func (f *UploadableFile) GetContentType() string {
    29  	return f.contentType
    30  }
    31  
    32  // GetContentLength returns the file's content length, or -1 if not known.
    33  func (f *UploadableFile) GetContentLength() int64 {
    34  	return f.contentLength
    35  }
    36  
    37  // NewUploadableFile creates an uploadable file with the given file name, content and content type, and an unspecified content length.
    38  func NewUploadableFile(fileName string, content io.Reader, contentType string) (*UploadableFile, error) {
    39  	return NewUploadableFileWithLength(fileName, content, contentType, -1)
    40  }
    41  
    42  // NewUploadableFileWithLength creates an uploadable file with the given file name, content, content type and content length.
    43  func NewUploadableFileWithLength(fileName string, content io.Reader, contentType string, contentLength int64) (*UploadableFile, error) {
    44  	if strings.TrimSpace(fileName) == "" {
    45  		err := errors.New("fileName is required")
    46  		return nil, err
    47  	}
    48  	if content == nil {
    49  		err := errors.New("content is required")
    50  		return nil, err
    51  	}
    52  	if strings.TrimSpace(contentType) == "" {
    53  		err := errors.New("contentType is required")
    54  		return nil, err
    55  	}
    56  	if contentLength < -1 {
    57  		contentLength = -1
    58  	}
    59  
    60  	return &UploadableFile{fileName, content, contentType, contentLength}, nil
    61  }