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;
3using System.Text;
4using System.Threading.Tasks;
7
9{
16 {
20 public const string ContentType = "multipart/alternative";
21
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[] { "alternative" };
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 ContentAlternatives(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, "alternative", 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 = "alternative";
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 ContentAlternatives &&
136 InternetContent.IsAccepted(contentTypes, 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 ContentAlternatives Alternatives &&
160 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
161 {
162 string Boundary = Guid.NewGuid().ToString();
163 string ContentType = AlternativeCodec.ContentType + "; boundary=\"" + Boundary + "\"";
164 return new ContentResponse(ContentType, Object,
165 await FormDataDecoder.Encode(Alternatives.Content, Boundary, Progress));
166 }
167 else
168 return new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object)));
169 }
170 }
171}
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.
async Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
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.
async Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
AlternativeCodec()
Codec of alternative data.
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< 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
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