🏷️ Created MediaType domain
This commit is contained in:
parent
7557c73e55
commit
c20c35f625
|
@ -0,0 +1,52 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MediaType struct {
|
||||
mainType string
|
||||
subType string
|
||||
mediaType string
|
||||
}
|
||||
|
||||
// NewMediaType creates a new MediaType domain from raw string in
|
||||
// the 'MainType/SubType' format.
|
||||
func NewMediaType(raw string) MediaType {
|
||||
parts := strings.Split(raw, "/")
|
||||
if len(parts) < 2 {
|
||||
return MediaType{}
|
||||
}
|
||||
|
||||
return MediaType{
|
||||
mainType: parts[0],
|
||||
subType: parts[1],
|
||||
mediaType: mime.FormatMediaType(strings.Join([]string{parts[0], parts[1]}, "/"), nil),
|
||||
}
|
||||
}
|
||||
|
||||
// Type returns main part of MediaType.
|
||||
// For 'image/jpeg' it returns 'image'.
|
||||
func (mt MediaType) MainType() string {
|
||||
return mt.mainType
|
||||
}
|
||||
|
||||
// Type returns sub part of MediaType.
|
||||
// For 'image/jpeg' it returns 'jpeg'.
|
||||
func (mt MediaType) SubType() string {
|
||||
return mt.subType
|
||||
}
|
||||
|
||||
// Type returns 'MainType/SubType' string.
|
||||
func (mt MediaType) Type() string {
|
||||
return mt.mediaType
|
||||
}
|
||||
|
||||
func (mt MediaType) String() string {
|
||||
return mt.mediaType
|
||||
}
|
||||
|
||||
func (mt MediaType) GoString() string {
|
||||
return "domain.MediaType(" + mt.mediaType + ")"
|
||||
}
|
Loading…
Reference in New Issue