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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
15 {
19 public const string ContentType = "multipart/mixed";
20
26 public MixedCodec()
27 {
28 }
29
33 public string[] ContentTypes => new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "mixed" };
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
76 return new MixedContent(List.ToArray());
77 }
78
85 public bool TryGetContentType(string FileExtension, out string ContentType)
86 {
87 if (string.Compare(FileExtension, "mixed", true) == 0)
88 {
90 return true;
91 }
92 else
93 {
94 ContentType = string.Empty;
95 return false;
96 }
97 }
98
105 public bool TryGetFileExtension(string ContentType, out string FileExtension)
106 {
107 switch (ContentType.ToLower())
108 {
110 FileExtension = "mixed";
111 return true;
112
113 default:
114 FileExtension = string.Empty;
115 return false;
116 }
117 }
118
126 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
127 {
128 if (Object is MixedContent &&
129 InternetContent.IsAccepted(ContentType, AcceptedContentTypes))
130 {
131 Grade = Grade.Ok;
132 return true;
133 }
134 else
135 {
136 Grade = Grade.NotAtAll;
137 return false;
138 }
139 }
140
149 public async Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
150 {
151 if (Object is MixedContent Mixed &&
152 InternetContent.IsAccepted(ContentType, AcceptedContentTypes))
153 {
154 string Boundary = Guid.NewGuid().ToString();
155 string ContentType = MixedCodec.ContentType + "; boundary=\"" + Boundary + "\"";
156 return new KeyValuePair<byte[], string>(await FormDataDecoder.Encode(Mixed.Content, Boundary), ContentType);
157 }
158 else
159 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
160 }
161 }
162}
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
async Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: MixedCodec.cs:70
MixedCodec()
Codec of mixed data.
Definition: MixedCodec.cs:26
string[] ContentTypes
Supported content types.
Definition: MixedCodec.cs:33
async Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: MixedCodec.cs:149
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: MixedCodec.cs:126
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: MixedCodec.cs:105
string[] FileExtensions
Supported file extensions.
Definition: MixedCodec.cs:38
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: MixedCodec.cs:46
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: MixedCodec.cs:85
const string ContentType
multipart/mixed
Definition: MixedCodec.cs:19
Represents mixed content, encoded with multipart/mixed
Definition: MixedContent.cs:7
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