Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BrotliContentEncoding.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Events;
8
10{
15 {
16 private static readonly Random rnd = new Random();
17 private static Scheduler? scheduler = null;
18 private static string? appDataFolder = null;
19 private static string? brotliFolder = null;
20 private static bool compressDynamic = false;
21 private static bool compressStatic = true;
22
26 public string Label => "br";
27
31 public bool SupportsDynamicEncoding => compressDynamic;
32
36 public bool SupportsStaticEncoding => compressStatic;
37
43 public void ConfigureSupport(bool Dynamic, bool Static)
44 {
45 compressDynamic = Dynamic;
46 compressStatic = Static;
47 }
48
54 public Grade Supports(string Label) => Label == this.Label ? Grade.Ok : Grade.NotAtAll;
55
63 public TransferEncoding GetEncoder(TransferEncoding Output, long? ExpectedContentLength, string ETag)
64 {
65 string? CompressedFileName;
66
67 if (!string.IsNullOrEmpty(brotliFolder))
68 CompressedFileName = Path.Combine(brotliFolder, ETag + ".br");
69 else
70 CompressedFileName = null;
71
72 BrotliEncoder Encoder = new BrotliEncoder(CompressedFileName, Output, ExpectedContentLength);
73 Encoder.PrepareForCompression();
74 return Encoder;
75 }
76
82 public FileInfo? TryGetPrecompressedFile(string ETag)
83 {
84 if (string.IsNullOrEmpty(ETag) || string.IsNullOrEmpty(brotliFolder))
85 return null;
86
87 string CompressedFileName = Path.Combine(brotliFolder, ETag + ".br");
88
89 return new FileInfo(CompressedFileName);
90 }
91
97 public static void Init(string AppDataFolder)
98 {
99 try
100 {
101 appDataFolder = AppDataFolder;
102 brotliFolder = Path.Combine(appDataFolder, "Brotli");
103
104 if (!Directory.Exists(brotliFolder))
105 Directory.CreateDirectory(brotliFolder);
106
107 if (scheduler is null)
108 {
109 if (Types.TryGetModuleParameter("Scheduler", out Scheduler Scheduler))
110 scheduler = Scheduler;
111 else
112 {
113 scheduler = new Scheduler();
114
115 Log.Terminating += (Sender, e) =>
116 {
117 scheduler?.Dispose();
118 scheduler = null;
119 return Task.CompletedTask;
120 };
121 }
122 }
123
124 DeleteOldFiles(TimeSpan.FromDays(7));
125 }
126 catch (Exception ex)
127 {
128 Log.Exception(ex);
129 }
130 }
131
132 private static void DeleteOldFiles(object P)
133 {
134 if (P is TimeSpan MaxAge)
135 DeleteOldFiles(MaxAge, true);
136 }
137
143 public static void DeleteOldFiles(TimeSpan MaxAge, bool Reschedule)
144 {
145 DateTime Limit = DateTime.Now - MaxAge;
146 int Count = 0;
147
148 DirectoryInfo BrotliFolder = new DirectoryInfo(brotliFolder);
149 FileInfo[] Files = BrotliFolder.GetFiles("*.*");
150
151 foreach (FileInfo FileInfo in Files)
152 {
153 if (FileInfo.LastAccessTime < Limit)
154 {
155 try
156 {
157 File.Delete(FileInfo.FullName);
158 Count++;
159 }
160 catch (Exception ex)
161 {
162 Log.Error("Unable to delete old file: " + ex.Message, FileInfo.FullName);
163 }
164 }
165 }
166
167 if (Count > 0)
168 Log.Informational(Count.ToString() + " old file(s) deleted.", brotliFolder);
169
170 if (Reschedule)
171 {
172 lock (rnd)
173 {
174 scheduler?.Add(DateTime.Now.AddDays(rnd.NextDouble() * 2), DeleteOldFiles, MaxAge);
175 }
176 }
177 }
178
179 }
180}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:692
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:344
Grade Supports(string Label)
How well the Content-Encoding handles the encoding specified by Label .
void ConfigureSupport(bool Dynamic, bool Static)
Configures support for the algorithm.
static void DeleteOldFiles(TimeSpan MaxAge, bool Reschedule)
Deletes generated files older than MaxAge .
bool SupportsStaticEncoding
If encoding can be used for static encoding.
static void Init(string AppDataFolder)
Initializes the Brotli-compression integration.
bool SupportsDynamicEncoding
If encoding can be used for dynamic encoding.
string Label
Label identifying the Content-Encoding
FileInfo? TryGetPrecompressedFile(string ETag)
Tries to get a reference to the precompressed file, if available.
TransferEncoding GetEncoder(TransferEncoding Output, long? ExpectedContentLength, string ETag)
Gets an encoder.
Class performing br encoding and decoding.
void PrepareForCompression()
Prepares the encoder for compression
Base class for all transfer encodings.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:14
void Dispose()
IDisposable.Dispose
Definition: Scheduler.cs:34
DateTime Add(DateTime When, Action< object > Callback, object State)
Adds an event.
Definition: Scheduler.cs:54
Interface for content encodings in HTTP transfers.
Grade
Grade enumeration
Definition: Grade.cs:7