github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/compress/compress-types.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package compress 4 5 import ( 6 "../protocol" 7 ) 8 9 // CompressTypes store all compress types to use by anyone want protocol.CompressTypes 10 type CompressTypes struct{} 11 12 // RegisterCompressType register given CompressType 13 func (cts *CompressTypes) RegisterCompressType(ct protocol.CompressType) { 14 if ct.MediaType() == nil { 15 panic("CompressType doesn't has a valid MediaType. Can't register it.") 16 } 17 if ct.ContentEncoding() == "" { 18 panic("CompressType doesn't has a valid ContentEncoding. Can't register it.") 19 } 20 register(ct) 21 } 22 23 func (cts *CompressTypes) GetCompressTypeByID(id uint64) (ct protocol.CompressType, err protocol.Error) { 24 ct = ByID(id) 25 if ct == nil { 26 err = ErrNotFound 27 } 28 return 29 } 30 func (cts *CompressTypes) GetCompressTypeByMediaType(mt string) (ct protocol.CompressType, err protocol.Error) { 31 ct = ByMediaType(mt) 32 if ct == nil { 33 err = ErrNotFound 34 } 35 return 36 } 37 func (cts *CompressTypes) GetCompressTypeByFileExtension(ex string) (ct protocol.CompressType, err protocol.Error) { 38 ct = ByFileExtension(ex) 39 if ct == nil { 40 err = ErrNotFound 41 } 42 return 43 } 44 func (cts *CompressTypes) GetCompressTypeByContentEncoding(ce string) (ct protocol.CompressType, err protocol.Error) { 45 ct = ByContentEncoding(ce) 46 if ct == nil { 47 err = ErrNotFound 48 } 49 return 50 } 51 func (cts *CompressTypes) ContentEncodings() []string { return contentEncodings } 52 53 var ( 54 poolByID = map[uint64]protocol.CompressType{} 55 poolByMediaType = map[string]protocol.CompressType{} 56 poolByFileExtension = map[string]protocol.CompressType{} 57 poolByContentEncoding = map[string]protocol.CompressType{} 58 contentEncodings []string 59 ) 60 61 func ByID(id uint64) protocol.CompressType { return poolByID[id] } 62 func ByMediaType(mt string) protocol.CompressType { return poolByMediaType[mt] } 63 func ByFileExtension(ex string) protocol.CompressType { return poolByFileExtension[ex] } 64 func ByContentEncoding(ce string) protocol.CompressType { return poolByContentEncoding[ce] } 65 func ContentEncodings() []string { return contentEncodings } 66 67 func register(ct protocol.CompressType) { 68 // TODO::: lock?? 69 poolByID[ct.MediaType().ID()] = ct 70 poolByMediaType[ct.MediaType().MediaType()] = ct 71 poolByFileExtension[ct.FileExtension()] = ct 72 var ce = ct.ContentEncoding() 73 poolByContentEncoding[ce] = ct 74 contentEncodings = append(contentEncodings, ce) 75 }