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 Waher.Events;
7
9{
14 {
15 private static readonly Random rnd = new Random();
16 private static Scheduler? scheduler = null;
17 private static string? appDataFolder = null;
18 private static string? brotliFolder = null;
19 private static bool compressDynamic = false;
20 private static bool compressStatic = true;
21
25 public string Label => "br";
26
30 public bool SupportsDynamicEncoding => compressDynamic;
31
35 public bool SupportsStaticEncoding => compressStatic;
36
42 public void ConfigureSupport(bool Dynamic, bool Static)
43 {
44 compressDynamic = Dynamic;
45 compressStatic = Static;
46 }
47
53 public Grade Supports(string Label) => Label == this.Label ? Grade.Ok : Grade.NotAtAll;
54
62 public TransferEncoding GetEncoder(TransferEncoding Output, long? ExpectedContentLength, string ETag)
63 {
64 string? CompressedFileName;
65
66 if (!string.IsNullOrEmpty(brotliFolder))
67 CompressedFileName = Path.Combine(brotliFolder, ETag + ".br");
68 else
69 CompressedFileName = null;
70
71 BrotliEncoder Encoder = new BrotliEncoder(CompressedFileName, Output, ExpectedContentLength);
72 Encoder.PrepareForCompression();
73 return Encoder;
74 }
75
81 public FileInfo? TryGetPrecompressedFile(string ETag)
82 {
83 if (string.IsNullOrEmpty(ETag) || string.IsNullOrEmpty(brotliFolder))
84 return null;
85
86 string CompressedFileName = Path.Combine(brotliFolder, ETag + ".br");
87
88 return new FileInfo(CompressedFileName);
89 }
90
96 public static void Init(string AppDataFolder)
97 {
98 try
99 {
100 appDataFolder = AppDataFolder;
101 brotliFolder = Path.Combine(appDataFolder, "Brotli");
102
103 if (!Directory.Exists(brotliFolder))
104 Directory.CreateDirectory(brotliFolder);
105
106 if (scheduler is null)
107 {
108 if (Types.TryGetModuleParameter("Scheduler", out object Obj) && Obj is Scheduler Scheduler)
109 scheduler = Scheduler;
110 else
111 {
112 scheduler = new Scheduler();
113
114 Log.Terminating += (Sender, e) =>
115 {
116 scheduler?.Dispose();
117 scheduler = null;
118 };
119 }
120 }
121
122 DeleteOldFiles(TimeSpan.FromDays(7));
123 }
124 catch (Exception ex)
125 {
126 Log.Exception(ex);
127 }
128 }
129
130 private static void DeleteOldFiles(object P)
131 {
132 if (P is TimeSpan MaxAge)
133 DeleteOldFiles(MaxAge, true);
134 }
135
141 public static void DeleteOldFiles(TimeSpan MaxAge, bool Reschedule)
142 {
143 DateTime Limit = DateTime.Now - MaxAge;
144 int Count = 0;
145
146 DirectoryInfo BrotliFolder = new DirectoryInfo(brotliFolder);
147 FileInfo[] Files = BrotliFolder.GetFiles("*.*");
148
149 foreach (FileInfo FileInfo in Files)
150 {
151 if (FileInfo.LastAccessTime < Limit)
152 {
153 try
154 {
155 File.Delete(FileInfo.FullName);
156 Count++;
157 }
158 catch (Exception ex)
159 {
160 Log.Error("Unable to delete old file: " + ex.Message, FileInfo.FullName);
161 }
162 }
163 }
164
165 if (Count > 0)
166 Log.Informational(Count.ToString() + " old file(s) deleted.", brotliFolder);
167
168 if (Reschedule)
169 {
170 lock (rnd)
171 {
172 scheduler?.Add(DateTime.Now.AddDays(rnd.NextDouble() * 2), DeleteOldFiles, MaxAge);
173 }
174 }
175 }
176
177 }
178}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
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:682
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:334
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:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:26
void Dispose()
IDisposable.Dispose
Definition: Scheduler.cs:46
DateTime Add(DateTime When, ScheduledEventCallback Callback, object State)
Adds an event.
Definition: Scheduler.cs:66
Interface for content encodings in HTTP transfers.
Grade
Grade enumeration
Definition: Grade.cs:7