Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RelatedCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
15 {
19 public const string ContentType = "multipart/related";
20
26 public RelatedCodec()
27 {
28 }
29
33 public string[] ContentTypes => new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "related" };
39
46 public bool Decodes(string ContentType, out Grade Grade)
47 {
49 {
50 Grade = Grade.Excellent;
51 return true;
52 }
53 else
54 {
55 Grade = Grade.NotAtAll;
56 return false;
57 }
58 }
59
70 public async Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
71 {
72 List<EmbeddedContent> List = new List<EmbeddedContent>();
73
74 await FormDataDecoder.Decode(Data, Fields, null, List, BaseUri);
75 string Type = string.Empty;
76
77 foreach (KeyValuePair<string, string> P in Fields)
78 {
79 switch (P.Key.ToUpper())
80 {
81 case "TYPE":
82 Type = P.Value.Trim();
83 break;
84 }
85 }
86
87 return new RelatedContent(List.ToArray(), Type);
88 }
89
96 public bool TryGetContentType(string FileExtension, out string ContentType)
97 {
98 if (string.Compare(FileExtension, "related", true) == 0)
99 {
101 return true;
102 }
103 else
104 {
105 ContentType = string.Empty;
106 return false;
107 }
108 }
109
116 public bool TryGetFileExtension(string ContentType, out string FileExtension)
117 {
118 switch (ContentType.ToLower())
119 {
121 FileExtension = "related";
122 return true;
123
124 default:
125 FileExtension = string.Empty;
126 return false;
127 }
128 }
129
137 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
138 {
139 if (Object is RelatedContent &&
140 InternetContent.IsAccepted(ContentTypes, AcceptedContentTypes))
141 {
142 Grade = Grade.Ok;
143 return true;
144 }
145 else
146 {
147 Grade = Grade.NotAtAll;
148 return false;
149 }
150 }
151
160 public async Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
161 {
162 if (Object is RelatedContent Related &&
163 InternetContent.IsAccepted(ContentTypes, AcceptedContentTypes))
164 {
165 string Boundary = Guid.NewGuid().ToString();
166 string ContentType = RelatedCodec.ContentType + "; boundary=\"" + Boundary + "\"; type=\"" + Related.Type.Replace("\"", "\\\"") + "\"";
167 return new KeyValuePair<byte[], string>(await FormDataDecoder.Encode(Related.Content, Boundary), ContentType);
168 }
169 else
170 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
171 }
172 }
173}
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
static async Task Decode(byte[] Data, KeyValuePair< string, string >[] Fields, Dictionary< string, object > Form, List< EmbeddedContent > List, Uri BaseUri)
Decodes a multipart object
static async Task< KeyValuePair< byte[], string > > Encode(IEnumerable< EmbeddedContent > Content)
Encodes multi-part form data
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7