Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AlternativeCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
15 {
19 public const string ContentType = "multipart/alternative";
20
27 {
28 }
29
33 public string[] ContentTypes => new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "alternative" };
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 ContentAlternatives(List.ToArray());
77 }
78
85 public bool TryGetContentType(string FileExtension, out string ContentType)
86 {
87 if (string.Compare(FileExtension, "alternative", 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 = "alternative";
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 ContentAlternatives &&
129 InternetContent.IsAccepted(this.ContentTypes, 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 ContentAlternatives Alternatives &&
152 InternetContent.IsAccepted(this.ContentTypes, AcceptedContentTypes))
153 {
154 string Boundary = Guid.NewGuid().ToString();
155 string ContentType = AlternativeCodec.ContentType + "; boundary=\"" + Boundary + "\"";
156 return new KeyValuePair<byte[], string>(await FormDataDecoder.Encode(Alternatives.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.
async Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
string[] FileExtensions
Supported file extensions.
const string ContentType
multipart/alternative
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
AlternativeCodec()
Codec of alternative data.
async Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
string[] ContentTypes
Supported content types.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Represents alternative versions of the same content, encoded with multipart/alternative
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