🏷️ Created MediaType domain

This commit is contained in:
Maxim Lebedev 2023-11-10 06:14:30 +06:00
parent 7557c73e55
commit c20c35f625
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 52 additions and 0 deletions

View File

@ -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 + ")"
}