github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/mediatype/media-type.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package mediatype 4 5 import ( 6 "strings" 7 8 "github.com/GeniusesGroup/libgo/protocol" 9 uuid "github.com/GeniusesGroup/libgo/uuid/32byte" 10 ) 11 12 // MT is the same as the MediaType. 13 // Use this type when embed in other struct to solve field & method same name problem(MediaType struct and MediaType() method) to satisfy interfaces. 14 type MT = MediaType 15 16 // MediaType implement protocol.MediaType interface 17 // type "/" [tree "."] subtype ["+" suffix]* [";" parameter] 18 // https://datatracker.ietf.org/doc/html/rfc2046 19 type MediaType struct { 20 uuid.Generated 21 22 mediaType string 23 mainType string 24 tree string 25 subType string 26 suffix string 27 parameters []string 28 } 29 30 func (mt *MediaType) Init(mediatype string) { 31 mt.mediaType = mediatype 32 mt.parse() 33 34 mt.Generated.NewHashString(mediatype) 35 } 36 37 //libgo:impl protocol.MediaType 38 func (mt *MediaType) MediaType() string { return mt.mediaType } 39 func (mt *MediaType) MainType() string { return mt.mainType } 40 func (mt *MediaType) Tree() string { return mt.tree } 41 func (mt *MediaType) SubType() string { return mt.subType } 42 func (mt *MediaType) Suffix() string { return mt.suffix } 43 func (mt *MediaType) Parameters() []string { return mt.parameters } 44 func (mt *MediaType) FileExtension() string { return "" } 45 func (mt *MediaType) Status() protocol.SoftwareStatus { return protocol.Software_Unset } 46 func (mt *MediaType) ReferenceURI() string { return "" } 47 func (mt *MediaType) IssueDate() protocol.Time { return nil } 48 func (mt *MediaType) ExpiryDate() protocol.Time { return nil } 49 func (mt *MediaType) ExpireInFavorOf() protocol.MediaType { return nil } 50 func (mt *MediaType) Fields() []protocol.Field { return nil } 51 func (mt *MediaType) ToString() string { return mt.mediaType } 52 53 // TODO::: complete extraction 54 func (mt *MediaType) parse() { 55 var mediatype = mt.mediaType 56 57 var i = strings.IndexByte(mediatype, '/') 58 if i < 0 { 59 panic("Mediatype isn't in good shape to parse it. Please check it.") 60 } 61 mt.mainType = mediatype[:i] 62 // TODO::: 63 mt.subType = mediatype[i+1:] 64 }