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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
7
8namespace Waher.Content.Dsn
9{
16 {
20 public const string ContentType = "multipart/report";
21
27 public ReportCodec()
28 {
29 }
30
34 public string[] ContentTypes => new string[] { ContentType };
35
39 public string[] FileExtensions => new string[] { "report" };
40
47 public bool Decodes(string ContentType, out Grade Grade)
48 {
50 {
51 Grade = Grade.Excellent;
52 return true;
53 }
54 else
55 {
56 Grade = Grade.NotAtAll;
57 return false;
58 }
59 }
60
71 public async Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
72 {
73 List<EmbeddedContent> List = new List<EmbeddedContent>();
74
75 await FormDataDecoder.Decode(Data, Fields, null, List, BaseUri);
76
77 return new ReportContent(List.ToArray());
78 }
79
86 public bool TryGetContentType(string FileExtension, out string ContentType)
87 {
88 if (string.Compare(FileExtension, "report", true) == 0)
89 {
91 return true;
92 }
93 else
94 {
95 ContentType = string.Empty;
96 return false;
97 }
98 }
99
106 public bool TryGetFileExtension(string ContentType, out string FileExtension)
107 {
108 switch (ContentType.ToLower())
109 {
111 FileExtension = "report";
112 return true;
113
114 default:
115 FileExtension = string.Empty;
116 return false;
117 }
118 }
119
127 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
128 {
129 if (Object is ReportContent &&
130 InternetContent.IsAccepted(ContentTypes, AcceptedContentTypes))
131 {
132 Grade = Grade.Ok;
133 return true;
134 }
135 else
136 {
137 Grade = Grade.NotAtAll;
138 return false;
139 }
140 }
141
150 public async Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
151 {
152 if (Object is ReportContent Report &&
153 InternetContent.IsAccepted(ContentTypes, AcceptedContentTypes))
154 {
155 string Boundary = Guid.NewGuid().ToString();
156 string ContentType = ReportCodec.ContentType + "; boundary=\"" + Boundary + "\"";
157 byte[] Bin = await FormDataDecoder.Encode(Report.Content, Boundary);
158
159 return new KeyValuePair<byte[], string>(Bin, ContentType);
160 }
161 else
162 throw new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object));
163 }
164 }
165}
Decoder of report data.
Definition: ReportCodec.cs:16
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: ReportCodec.cs:86
async Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: ReportCodec.cs:71
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: ReportCodec.cs:127
string[] FileExtensions
Supported file extensions.
Definition: ReportCodec.cs:39
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: ReportCodec.cs:106
async Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: ReportCodec.cs:150
ReportCodec()
Decoder of report data.
Definition: ReportCodec.cs:27
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: ReportCodec.cs:47
string[] ContentTypes
Supported content types.
Definition: ReportCodec.cs:34
const string ContentType
multipart/report
Definition: ReportCodec.cs:20
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 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