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.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
7
9{
14 {
18 public FileEncoder()
19 {
20 }
21
25 public string[] ContentTypes => new string[0];
26
30 public string[] FileExtensions => new string[0];
31
40 public async Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
41 {
42 if (Object is FileReference Ref)
43 {
44 using (FileStream f = File.OpenRead(Ref.FileName))
45 {
46 if (f.Length > int.MaxValue)
47 throw new OutOfMemoryException("File too large.");
48
49 byte[] Bin = new byte[f.Length];
50 await f.ReadAsync(Bin, 0, (int)f.Length);
51
52 return new KeyValuePair<byte[], string>(Bin, Ref.ContentType);
53 }
54 }
55 else
56 throw new ArgumentException("Unable to encode object.", nameof(Object));
57 }
58
66 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
67 {
68 if (Object is FileReference Ref)
69 {
70 if (InternetContent.IsAccepted(Ref.ContentType, AcceptedContentTypes))
71 {
72 Grade = Grade.Ok;
73 return true;
74 }
75 }
76
77 Grade = Grade.NotAtAll;
78 return false;
79 }
80
87 public bool TryGetContentType(string FileExtension, out string ContentType)
88 {
89 ContentType = null;
90 return false;
91 }
92
99 public bool TryGetFileExtension(string ContentType, out string FileExtension)
100 {
101 FileExtension = null;
102 return false;
103 }
104 }
105}
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:87
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: FileEncoder.cs:99
string[] ContentTypes
Supported content types.
Definition: FileEncoder.cs:25
async Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: FileEncoder.cs:40
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: FileEncoder.cs:66
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.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7