source: Main/branches/origin-master/contenttype/contenttype.go

Last change on this file was 4a5f10f, checked in by www <www@…>, 8 weeks ago

Mirrored from https://git.chaotic.ninja/git/yakumo_izuru/yukari

git-svn-id: https://svn.chaotic.ninja/svn/yukari-yakumo.izuru@1 ee30ecee-8fc8-4245-a645-e3b2cc3919a6

  • Property mode set to 100644
File size: 2.8 KB
Line 
1package contenttype
2
3import (
4 "mime"
5 "strings"
6)
7
8type ContentType struct {
9 TopLevelType string
10 SubType string
11 Suffix string
12 Parameters map[string]string
13}
14
15func (contenttype *ContentType) String() string {
16 var mimetype string
17 if contenttype.Suffix == "" {
18 if contenttype.SubType == "" {
19 mimetype = contenttype.TopLevelType
20 } else {
21 mimetype = contenttype.TopLevelType + "/" + contenttype.SubType
22 }
23 } else {
24 mimetype = contenttype.TopLevelType + "/" + contenttype.SubType + "+" + contenttype.Suffix
25 }
26 return mime.FormatMediaType(mimetype, contenttype.Parameters)
27}
28
29func (contenttype *ContentType) Equals(other ContentType) bool {
30 if contenttype.TopLevelType != other.TopLevelType ||
31 contenttype.SubType != other.SubType ||
32 contenttype.Suffix != other.Suffix ||
33 len(contenttype.Parameters) != len(other.Parameters) {
34 return false
35 }
36 for k, v := range contenttype.Parameters {
37 if other.Parameters[k] != v {
38 return false
39 }
40 }
41 return true
42}
43
44func (contenttype *ContentType) FilterParameters(parameters map[string]bool) {
45 for k, _ := range contenttype.Parameters {
46 if !parameters[k] {
47 delete(contenttype.Parameters, k)
48 }
49 }
50}
51
52func ParseContentType(contenttype string) (ContentType, error) {
53 mimetype, params, err := mime.ParseMediaType(contenttype)
54 if err != nil {
55 return ContentType{"", "", "", params}, err
56 }
57 splitted_mimetype := strings.SplitN(strings.ToLower(mimetype), "/", 2)
58 if len(splitted_mimetype) <= 1 {
59 return ContentType{splitted_mimetype[0], "", "", params}, nil
60 } else {
61 splitted_subtype := strings.SplitN(splitted_mimetype[1], "+", 2)
62 if len(splitted_subtype) == 1 {
63 return ContentType{splitted_mimetype[0], splitted_subtype[0], "", params}, nil
64 } else {
65 return ContentType{splitted_mimetype[0], splitted_subtype[0], splitted_subtype[1], params}, nil
66 }
67 }
68
69}
70
71type Filter func(contenttype ContentType) bool
72
73func NewFilterContains(partialMimeType string) Filter {
74 return func(contenttype ContentType) bool {
75 return strings.Contains(contenttype.TopLevelType, partialMimeType) ||
76 strings.Contains(contenttype.SubType, partialMimeType) ||
77 strings.Contains(contenttype.Suffix, partialMimeType)
78 }
79}
80
81func NewFilterEquals(TopLevelType, SubType, Suffix string) Filter {
82 return func(contenttype ContentType) bool {
83 return ((TopLevelType != "*" && TopLevelType == contenttype.TopLevelType) || (TopLevelType == "*")) &&
84 ((SubType != "*" && SubType == contenttype.SubType) || (SubType == "*")) &&
85 ((Suffix != "*" && Suffix == contenttype.Suffix) || (Suffix == "*"))
86 }
87}
88
89func NewFilterOr(contentTypeFilterList []Filter) Filter {
90 return func(contenttype ContentType) bool {
91 for _, contentTypeFilter := range contentTypeFilterList {
92 if contentTypeFilter(contenttype) {
93 return true
94 }
95 }
96 return false
97 }
98}
Note: See TracBrowser for help on using the repository browser.