VectSharp  2.2.1
A light library for C# vector graphics
ImageCache.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using Avalonia.Controls.ApplicationLifetimes;
19 using System;
20 using System.Collections.Generic;
21 using System.IO;
22 
24 {
25  internal static class ImageCache
26  {
27  private const string imageCacheId = "bb5a724e-93f7-431d-87c3-53f31d8da16e";
28 
29  private static string imageCacheFolder;
30 
31  private static Dictionary<string, string> imageCache;
32 
33  static ImageCache()
34  {
35  imageCacheFolder = Path.Combine(Path.GetTempPath(), imageCacheId);
36  Directory.CreateDirectory(imageCacheFolder);
37  imageCache = new Dictionary<string, string>();
38  }
39 
40  private static bool exitHandlerSet = false;
41  public static void SetExitEventHandler()
42  {
43  if (!exitHandlerSet)
44  {
45  if (Avalonia.Application.Current.ApplicationLifetime is IControlledApplicationLifetime lifetime)
46  {
47  lifetime.Exit += (s, e) =>
48  {
49  try
50  {
51  Directory.Delete(imageCacheFolder, true);
52  }
53  catch
54  {
55  try
56  {
57  foreach (string sr in Directory.GetFiles(imageCacheFolder, "*.*"))
58  {
59  try
60  {
61  File.Delete(sr);
62  }
63  catch { }
64  }
65  }
66  catch { }
67  }
68  };
69  }
70  exitHandlerSet = true;
71  }
72  }
73 
74  public static (string, bool) ImageUriResolver(string imageUri, string baseUriString)
75  {
76  if (!imageCache.TryGetValue(baseUriString + "|||" + imageUri, out string cachedImage))
77  {
78  (string imagePath, bool wasDownloaded) = VectSharp.Markdown.HTTPUtils.ResolveImageURI(imageUri, baseUriString);
79 
80  if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
81  {
82  string id = Guid.NewGuid().ToString();
83 
84  cachedImage = Path.Combine(imageCacheFolder, id + Path.GetExtension(imagePath));
85 
86  if (wasDownloaded)
87  {
88  if (!Directory.Exists(imageCacheFolder))
89  {
90  Directory.CreateDirectory(imageCacheFolder);
91  }
92 
93  File.Move(imagePath, cachedImage);
94  Directory.Delete(Path.GetDirectoryName(imagePath));
95  }
96  else
97  {
98  File.Copy(imagePath, cachedImage);
99  }
100 
101  imageCache[baseUriString + "|||" + imageUri] = cachedImage;
102  }
103  else
104  {
105  cachedImage = null;
106  }
107  }
108 
109  return (cachedImage, false);
110  }
111  }
112 }
VectSharp
Definition: Brush.cs:26
VectSharp.Markdown.HTTPUtils
Contains utilities to resolve absolute and relative URIs.
Definition: HtmlTag.cs:245
VectSharp.Markdown
Definition: HtmlTag.cs:26
VectSharp.MarkdownCanvas
Definition: ImageCache.cs:24