Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectToZipConverter.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
7
8namespace Waher.Content.Zip
9{
14 {
15 private readonly static Dictionary<string, bool> protectedContentTypes = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
16 private static string[] canConvertFrom = null;
17
22 public static void ProtectContentType(string ContentType)
23 {
24 lock (protectedContentTypes)
25 {
26 protectedContentTypes[ContentType] = true;
27 canConvertFrom = null;
28 }
29 }
30
36 public static bool IsProtected(string ContentType)
37 {
38 lock (protectedContentTypes)
39 {
40 return protectedContentTypes.TryGetValue(ContentType, out bool Protected) && Protected;
41 }
42 }
43
48 {
49 }
50
54 public string[] FromContentTypes
55 {
56 get
57 {
58 string[] Result = canConvertFrom;
59 if (!(Result is null))
60 return Result;
61
63 string[] ZipTypes = ZipCodec.ZipFileContentTypes;
64
65 foreach (string ContentType in InternetContent.CanEncodeContentTypes)
66 {
67 if (!IsProtected(ContentType) && Array.IndexOf(ZipTypes, ContentType) < 0)
68 Supported.Add(ContentType);
69 }
70
71 canConvertFrom = Result = Supported.ToArray();
72
73 return Result;
74 }
75 }
76
81
85 public virtual Grade ConversionGrade => Grade.Barely;
86
92 public async Task<bool> ConvertAsync(ConversionState State)
93 {
94 if (State is null)
95 return true;
96
97 string SourceFileName = State.FromFileName;
98
99 if (string.IsNullOrEmpty(SourceFileName))
100 SourceFileName = "File." + InternetContent.GetFileExtension(State.FromContentType);
101
102 byte[] Data;
103
104 using (MemoryStream Output = new MemoryStream())
105 {
106 await Zip.CreateZipFile(SourceFileName, State.From, DateTime.Now, Output);
107 Data = Output.ToArray();
108 }
109
110 await State.To.WriteAsync(Data, 0, Data.Length);
111
112 return false;
113 }
114 }
115}
Contains the state of a content conversion process.
string FromContentType
Content type of the content to convert from.
Stream To
Stream pointing to where binary representation of content is to be sent.
string FromFileName
If the content is coming from a file, this parameter contains the name of that file....
Stream From
Stream pointing to binary representation of content.
Static class managing encoding and decoding of internet content.
static string GetFileExtension(string ContentType)
Gets the file extension of an item, given its content type. It uses the TryGetFileExtension to see if...
static string[] CanEncodeContentTypes
Internet content types that can be encoded.
Converts an object to a ZIP File.
string[] FromContentTypes
Converts content from these content types.
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
virtual Grade ConversionGrade
How well the content is converted.
ObjectToZipConverter()
Converts an object to a ZIP File.
static void ProtectContentType(string ContentType)
Protects a content type, so that it cannot be included in generated zip files by external parties thr...
string[] ToContentTypes
Converts content to these content types.
static bool IsProtected(string ContentType)
Checks if a Content-Type is protected.
ZIP File encoder/decoder.
Definition: ZipCodec.cs:13
static readonly string[] ZipFileContentTypes
ZIP File content types.
Definition: ZipCodec.cs:29
Static class for creating ZIP files.
Definition: Zip.cs:22
static Task CreateZipFile(string SourceFileName, string OutputFileName)
Creates a ZIP file containing a single file.
Definition: Zip.cs:33
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7