Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReportCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
8
9namespace Waher.Content.Dsn
10{
17 {
21 public const string ContentType = "multipart/report";
22
28 public ReportCodec()
29 {
30 }
31
35 public string[] ContentTypes => contentTypes;
36
37 private static readonly string[] contentTypes = new string[] { ContentType };
38
42 public string[] FileExtensions => new string[] { "report" };
43
50 public bool Decodes(string ContentType, out Grade Grade)
51 {
53 {
54 Grade = Grade.Excellent;
55 return true;
56 }
57 else
58 {
59 Grade = Grade.NotAtAll;
60 return false;
61 }
62 }
63
74 public async Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
75 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
76 {
78
79 Exception Error = await FormDataDecoder.Decode(Data, Fields, null, List, BaseUri, Progress);
80
81 if (Error is null)
82 return new ContentResponse(ContentType, new ReportContent(List.ToArray()), Data);
83 else
84 return new ContentResponse(Error);
85 }
86
93 public bool TryGetContentType(string FileExtension, out string ContentType)
94 {
95 if (string.Compare(FileExtension, "report", true) == 0)
96 {
98 return true;
99 }
100 else
101 {
102 ContentType = string.Empty;
103 return false;
104 }
105 }
106
113 public bool TryGetFileExtension(string ContentType, out string FileExtension)
114 {
115 switch (ContentType.ToLower())
116 {
118 FileExtension = "report";
119 return true;
120
121 default:
122 FileExtension = string.Empty;
123 return false;
124 }
125 }
126
134 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
135 {
136 if (Object is ReportContent &&
137 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
138 {
139 Grade = Grade.Ok;
140 return true;
141 }
142 else
143 {
144 Grade = Grade.NotAtAll;
145 return false;
146 }
147 }
148
157 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
158 {
159 if (Object is ReportContent Report &&
160 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
161 {
162 string Boundary = Guid.NewGuid().ToString();
163 string ContentType = ReportCodec.ContentType + "; boundary=\"" + Boundary + "\"";
164 byte[] Bin = await FormDataDecoder.Encode(Report.Content, Boundary, Progress);
165
166 return new ContentResponse(ContentType, Object, Bin);
167 }
168 else
169 return new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object)));
170 }
171 }
172}
Contains information about a response to a content request.
Decoder of report data.
Definition: ReportCodec.cs:17
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: ReportCodec.cs:93
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: ReportCodec.cs:134
string[] FileExtensions
Supported file extensions.
Definition: ReportCodec.cs:42
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: ReportCodec.cs:113
ReportCodec()
Decoder of report data.
Definition: ReportCodec.cs:28
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: ReportCodec.cs:50
string[] ContentTypes
Supported content types.
Definition: ReportCodec.cs:35
const string ContentType
multipart/report
Definition: ReportCodec.cs:21
async Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: ReportCodec.cs:74
async Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: ReportCodec.cs:157
Represents report content, encoded with multipart/report
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
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