github.com/decred/politeia@v1.4.0/politeiad/api/v1/mime/bmime.go (about)

     1  package mime
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  
     7  	svg "github.com/h2non/go-is-svg"
     8  )
     9  
    10  var (
    11  	// validMimeTypesList is a list of all acceptable MIME types that
    12  	// can be communicated between client and server.
    13  	validMimeTypesList = []string{
    14  		"image/png",
    15  		"text/plain",
    16  		"text/plain; charset=utf-8",
    17  	}
    18  
    19  	// validMimeTypesMap is the same as ValidMimeTypesList, but structured
    20  	// as a map for fast access.
    21  	validMimeTypesMap = make(map[string]struct{}, len(validMimeTypesList))
    22  
    23  	ErrUnsupportedMimeType = errors.New("unsupported MIME type")
    24  )
    25  
    26  // MimeValid returns true if the passed string is a valid
    27  // MIME type, false otherwise.
    28  func MimeValid(s string) bool {
    29  	_, ok := validMimeTypesMap[s]
    30  	return ok
    31  }
    32  
    33  // ValidMimeTypes returns the list of supported MIME types.
    34  func ValidMimeTypes() []string {
    35  	return validMimeTypesList
    36  }
    37  
    38  // DetectMimeType returns the file MIME type
    39  func DetectMimeType(data []byte) string {
    40  	// svg needs a specific check because the algorithm
    41  	// implemented by http.DetectContentType doesn't detect svg
    42  	if svg.IsSVG(data) {
    43  		return "image/svg+xml"
    44  	}
    45  	return http.DetectContentType(data)
    46  }
    47  
    48  func init() {
    49  	for _, v := range validMimeTypesList {
    50  		validMimeTypesMap[v] = struct{}{}
    51  	}
    52  }