3using System.IO.Compression;
4using System.Collections.Generic;
7using System.Threading.Tasks;
60 private ManualResetEvent initialized =
new ManualResetEvent(
false);
61 private readonly
string zipFileName;
62 private readonly
string programDataFolder;
63 private readonly
string imageUrl;
64 private readonly
int width;
65 private readonly
int height;
76 string ZipFileName,
string ProgramDataFolder)
92 string ZipFileName,
string ProgramDataFolder)
97 this.imageUrl = ImageURL;
98 this.zipFileName = ZipFileName;
99 this.programDataFolder = ProgramDataFolder;
103 DateTime TP = File.GetLastWriteTime(this.zipFileName);
105 if (File.Exists(
this.zipFileName) &&
RuntimeSettings.
Get(
this.zipFileName, DateTime.MinValue) != TP)
107 if (!Directory.Exists(ProgramDataFolder))
108 Directory.CreateDirectory(ProgramDataFolder);
111 string Folder = Path.Combine(ProgramDataFolder,
"Emoji1");
113 if (Directory.Exists(Folder))
114 Directory.Delete(Folder,
true);
117 this.initialized =
new ManualResetEvent(
false);
119 Task.Run(this.Unpack);
122 this.initialized =
new ManualResetEvent(
true);
130 private async Task Unpack()
135 new KeyValuePair<string, object>(
"FileName", this.zipFileName),
136 new KeyValuePair<string, object>(
"Destination", this.programDataFolder));
138 ZipFile.ExtractToDirectory(this.zipFileName, this.programDataFolder);
140 DateTime TP = File.GetLastWriteTime(this.zipFileName);
145 File.Delete(this.zipFileName);
146 Log.
Informational(
"File unpacked and deleted.",
new KeyValuePair<string, object>(
"FileName", this.zipFileName));
150 Log.
Informational(
"File unpacked.",
new KeyValuePair<string, object>(
"FileName", this.zipFileName));
159 this.initialized.Set();
170 return this.initialized.WaitOne(TimeoutMilliseconds);
196 return File.Exists(FileName);
206 switch (this.sourceFileType)
241 Output.Append(
"<img alt=\":");
243 Output.Append(
":\" title=\"");
245 Output.Append(
"\" width=\"");
247 Output.Append(
"\" height=\"");
249 Output.Append(
"\" src=\"");
250 Output.Append(Encode(await this.
GetUrl(Emoji, EmbedImage)));
251 Output.Append(
"\"/>");
264 OrgSize = (OrgSize * 4) / 3;
268 OrgSize = (OrgSize * 3) / 2;
275 private static string Encode(
string s)
277 if (s is
null || s.IndexOfAny(specialCharacters) < 0)
281 Replace(
"&",
"&").
282 Replace(
"<",
"<").
283 Replace(
">",
">").
284 Replace(
"\"",
""").
285 Replace(
"'",
"'");
288 private static readonly
char[] specialCharacters =
new char[] {
'<',
'>',
'&',
'"',
'\'' };
298 if (Embed ||
string.IsNullOrEmpty(this.imageUrl))
300 StringBuilder Output =
new StringBuilder();
302 Output.Append(
"data:");
309 Output.Append(
";base64,");
314 Output.Append(Convert.ToBase64String(Data));
316 return Output.ToString();
324 return this.imageUrl.Replace(
"%FILENAME%", s);
348 Url = await this.
GetUrl(Emoji,
false),
359 this.initialized?.Dispose();
360 this.initialized =
null;
Provides emojis from Emoji One (http://emojione.com/) stored as local files.
Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height, string ImageURL, string ZipFileName, string ProgramDataFolder)
Provides emojis from Emoji One (http://emojione.com/) stored as local files.
void Dispose()
IDisposable
Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height, string ZipFileName, string ProgramDataFolder)
Provides emojis from Emoji One (http://emojione.com/) stored as local files.
Task GenerateHTML(StringBuilder Output, EmojiInfo Emoji, bool EmbedImage)
Generates HTML for a given Emoji.
string GetFileName(EmojiInfo Emoji)
Gets the local file name for a given emoji.
Task< IImageSource > GetImageSource(EmojiInfo Emoji)
Gets the image source of an emoji.
bool WaitUntilInitialized(int TimeoutMilliseconds)
Waits until initialization is completed.
async Task< IImageSource > GetImageSource(EmojiInfo Emoji, int Level)
Gets the image source of an emoji.
bool EmojiSupported(EmojiInfo Emoji)
If the emoji is supported by the emoji source.
int Width
Desired width of emojis.
Emoji1SourceFileType SourceFileType
Type of files to use.
async Task GenerateHTML(StringBuilder Output, EmojiInfo Emoji, int Level, bool EmbedImage)
Generates HTML for a given Emoji.
async Task< string > GetUrl(EmojiInfo Emoji, bool Embed)
Gets an URL for the emoji.
int CalcSize(int OrgSize, int Level)
Calculates the size of an emoji.
int Height
Desired height of emojis.
Contains information about an emoji.
string FileName
Emoji file name.
string ShortName
Emoji short name.
string Description
Short description of emoji.
Contains information about an emoji image.
const string FileExtensionPng
png
const string ContentTypePng
image/png
const string FileExtensionSvg
svg
const string ContentTypeSvg
image/svg+xml
Static class managing loading of resources stored as embedded resources or in content files.
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Static class managing the application event log. Applications and services log events on this static ...
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.
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.
Static class managing persistent settings.
static string Get(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Interface for Emoji sources. Emoji sources provide emojis to content providers.
Emoji1SourceFileType
What source files to use when displaying emoji.
delegate bool FileExistsHandler(string path)
Delegate to a FileExists method.
delegate byte[] ReadAllBytesHandler(string path)
Delegate to a ReadAllBytes method.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.