Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FileEncoder.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Threading.Tasks;
7
9{
14 {
18 public FileEncoder()
19 {
20 }
21
25 public string[] ContentTypes => Array.Empty<string>();
26
30 public string[] FileExtensions => Array.Empty<string>();
31
40 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
41 ICodecProgress Progress, params string[] AcceptedContentTypes)
42 {
43 if (Object is FileReference Ref)
44 {
45 using (FileStream f = File.OpenRead(Ref.FileName))
46 {
47 byte[] Bin = await f.ReadAllAsync();
48 return new ContentResponse(Ref.ContentType, Object, Bin);
49 }
50 }
51 else
52 return new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object)));
53 }
54
62 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
63 {
64 if (Object is FileReference Ref)
65 {
66 if (InternetContent.IsAccepted(Ref.ContentType, AcceptedContentTypes))
67 {
68 Grade = Grade.Ok;
69 return true;
70 }
71 }
72
73 Grade = Grade.NotAtAll;
74 return false;
75 }
76
83 public bool TryGetContentType(string FileExtension, out string ContentType)
84 {
85 ContentType = null;
86 return false;
87 }
88
95 public bool TryGetFileExtension(string ContentType, out string FileExtension)
96 {
97 FileExtension = null;
98 return false;
99 }
100 }
101}
string[] FileExtensions
Supported file extensions.
Definition: FileEncoder.cs:30
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: FileEncoder.cs:83
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: FileEncoder.cs:95
string[] ContentTypes
Supported content types.
Definition: FileEncoder.cs:25
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: FileEncoder.cs:62
async Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: FileEncoder.cs:40
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.
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7