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;
3using System.Text;
4using System.Threading.Tasks;
7
9{
16 {
20 public const string ContentType = "multipart/related";
21
27 public RelatedCodec()
28 {
29 }
30
34 public string[] ContentTypes => contentTypes;
35
36 private static readonly string[] contentTypes = new string[] { ContentType };
37
41 public string[] FileExtensions => new string[] { "related" };
42
49 public bool Decodes(string ContentType, out Grade Grade)
50 {
52 {
53 Grade = Grade.Excellent;
54 return true;
55 }
56 else
57 {
58 Grade = Grade.NotAtAll;
59 return false;
60 }
61 }
62
73 public async Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
74 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
75 {
77
78 Exception Error = await FormDataDecoder.Decode(Data, Fields, null, List, BaseUri, Progress);
79 if (!(Error is null))
80 return new ContentResponse(Error);
81
82 string Type = string.Empty;
83
84 foreach (KeyValuePair<string, string> P in Fields)
85 {
86 switch (P.Key.ToUpper())
87 {
88 case "TYPE":
89 Type = P.Value.Trim();
90 break;
91 }
92 }
93
94 return new ContentResponse(ContentType, new RelatedContent(List.ToArray(), Type), Data);
95 }
96
103 public bool TryGetContentType(string FileExtension, out string ContentType)
104 {
105 if (string.Compare(FileExtension, "related", true) == 0)
106 {
108 return true;
109 }
110 else
111 {
112 ContentType = string.Empty;
113 return false;
114 }
115 }
116
123 public bool TryGetFileExtension(string ContentType, out string FileExtension)
124 {
125 switch (ContentType.ToLower())
126 {
128 FileExtension = "related";
129 return true;
130
131 default:
132 FileExtension = string.Empty;
133 return false;
134 }
135 }
136
144 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
145 {
146 if (Object is RelatedContent &&
147 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
148 {
149 Grade = Grade.Ok;
150 return true;
151 }
152 else
153 {
154 Grade = Grade.NotAtAll;
155 return false;
156 }
157 }
158
167 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
168 ICodecProgress Progress, params string[] AcceptedContentTypes)
169 {
170 if (Object is RelatedContent Related &&
171 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
172 {
173 string Boundary = Guid.NewGuid().ToString();
174 string ContentType = RelatedCodec.ContentType + "; boundary=\"" + Boundary + "\"; type=\"" + Related.Type.Replace("\"", "\\\"") + "\"";
175 return new ContentResponse(ContentType, Object, await FormDataDecoder.Encode(Related.Content, Boundary, Progress));
176 }
177 else
178 return new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object)));
179 }
180 }
181}
Contains information about a response to a content request.
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< KeyValuePair< byte[], string > > Encode(IEnumerable< EmbeddedContent > Content, ICodecProgress Progress)
Encodes multi-part form data
static async Task< Exception > Decode(byte[] Data, KeyValuePair< string, string >[] Fields, Dictionary< string, object > Form, ChunkedList< EmbeddedContent > List, Uri BaseUri, ICodecProgress Progress)
Decodes a multipart object
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Interface for reporting progress about an encoding or decoding.
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