mimekit

package
v0.0.0-...-7fd672d Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidContentType = errors.New("invalid content type")
)

Functions

func DocContentTypes

func DocContentTypes() []string

func ExtensionByMime

func ExtensionByMime(mimeType string) string

func ImageContentTypes

func ImageContentTypes() []string

func IsDoc

func IsDoc(contentType string) bool

func IsImage

func IsImage(contentType string) bool

Types

type MIMEReader

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

MIMEReader represents a reader that performs MIME type detection and validation on data read from an underlying io.Reader.

func NewMIMEReader

func NewMIMEReader(r io.Reader, allowedContentTypes []string) *MIMEReader

NewMIMEReader creates a new MIMEReader instance with the provided io.Reader and a list of allowed content types.

The MIMEReader is designed to read data from the underlying reader while performing MIME type detection and validation. It allows developers to ensure that the content being read conforms to the expected MIME types. The provided allowedContentTypes is a list of MIME types that are considered valid.

It will stop reading from the underlying reader as soon it detects disallowed content type.

func (*MIMEReader) MIMEType

func (mr *MIMEReader) MIMEType() string

func (*MIMEReader) Read

func (mr *MIMEReader) Read(p []byte) (n int, err error)

Read reads data from the underlying reader into the provided byte slice, performs MIME type detection and validation, and returns the number of bytes read and an error (if any).

  • If the MIME type has already been detected, the method reads data until the end of the underlying reader.
  • If the MIME type has not been detected, the method continues reading and buffering data until the detection criteria are met.
  • Once the detection criteria are met, the buffered data is used to detect the MIME type using http.DetectContentType, and the detected MIME type is compared against the list of allowed content types.
  • If the detected MIME type is not in the allowed list, an error is returned.

The method implements the io.Reader interface.

Jump to

Keyboard shortcuts

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