Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MixedCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7
9{
16 {
20 public const string ContentType = "multipart/mixed";
21
27 public MixedCodec()
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[] { "mixed" };
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
80 if (Error is null)
81 return new ContentResponse(ContentType, new MixedContent(List.ToArray()), Data);
82 else
83 return new ContentResponse(Error);
84 }
85
92 public bool TryGetContentType(string FileExtension, out string ContentType)
93 {
94 if (string.Compare(FileExtension, "mixed", true) == 0)
95 {
97 return true;
98 }
99 else
100 {
101 ContentType = string.Empty;
102 return false;
103 }
104 }
105
112 public bool TryGetFileExtension(string ContentType, out string FileExtension)
113 {
114 switch (ContentType.ToLower())
115 {
117 FileExtension = "mixed";
118 return true;
119
120 default:
121 FileExtension = string.Empty;
122 return false;
123 }
124 }
125
133 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
134 {
135 if (Object is MixedContent &&
136 InternetContent.IsAccepted(ContentType, AcceptedContentTypes))
137 {
138 Grade = Grade.Ok;
139 return true;
140 }
141 else
142 {
143 Grade = Grade.NotAtAll;
144 return false;
145 }
146 }
147
156 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
157 ICodecProgress Progress, params string[] AcceptedContentTypes)
158 {
159 if (Object is MixedContent Mixed &&
160 InternetContent.IsAccepted(ContentType, AcceptedContentTypes))
161 {
162 string Boundary = Guid.NewGuid().ToString();
163 string ContentType = MixedCodec.ContentType + "; boundary=\"" + Boundary + "\"";
164 return new ContentResponse(ContentType, Object, await FormDataDecoder.Encode(Mixed.Content, Boundary, Progress));
165 }
166 else
167 return new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object)));
168 }
169 }
170}
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
MixedCodec()
Codec of mixed data.
Definition: MixedCodec.cs:27
string[] ContentTypes
Supported content types.
Definition: MixedCodec.cs:34
async Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: MixedCodec.cs:156
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: MixedCodec.cs:133
async Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: MixedCodec.cs:73
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: MixedCodec.cs:112
string[] FileExtensions
Supported file extensions.
Definition: MixedCodec.cs:41
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: MixedCodec.cs:49
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: MixedCodec.cs:92
const string ContentType
multipart/mixed
Definition: MixedCodec.cs:20
Represents mixed content, encoded with multipart/mixed
Definition: MixedContent.cs:7
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
T[] ToArray()
Returns an array containing all elements of the collection.
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