Parcourir la source

Refactor image loading methods for improved clarity and error handling

Marcin Ziąbek il y a 7 mois
Parent
commit
d5df32c8a4

+ 2 - 2
Source/QuestPDF.UnitTests/ImageTests.cs

@@ -85,8 +85,8 @@ namespace QuestPDF.UnitTests
         [Test]
         public void ImageObject_ThrowsFileNotFoundException_FileIsNotFound()
         {
-            Func<Infrastructure.Image> action = () => Infrastructure.Image.FromFile("non-existing-file.jpg");
-            Assert.That(action, Throws.Exception.TypeOf<DocumentComposeException>().With.Message.EqualTo("Cannot load provided image, file not found: non-existing-file.jpg"));
+            var action = () => Infrastructure.Image.FromFile("non-existing-file.jpg");
+            Assert.That(action, Throws.Exception.TypeOf<DocumentComposeException>().With.Message.EqualTo("Cannot load an image under the provided relative path, file not found: non-existing-file.jpg"));
         }
 
         [Test]

+ 1 - 1
Source/QuestPDF/Fluent/ImageExtensions.cs

@@ -154,7 +154,7 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.descriptor"]/*' />
         public static ImageDescriptor Image(this IContainer parent, string filePath)
         {
-            var image = StaticImageCache.Load(filePath);
+            var image = StaticImageCache.LoadFromCache(filePath);
             return parent.Image(image);
         }
         

+ 1 - 2
Source/QuestPDF/Infrastructure/Image.cs

@@ -94,8 +94,7 @@ namespace QuestPDF.Infrastructure
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.remarks"]/*' />
         public static Image FromFile(string filePath)
         {
-            using var imageData = SkData.FromFile(filePath);
-            return StaticImageCache.DecodeImage(imageData, isShared: true);
+            return StaticImageCache.DirectlyLoadFromFile(filePath, isShared: true);
         }
 
         /// <summary>

+ 27 - 22
Source/QuestPDF/Infrastructure/StaticImageCache.cs

@@ -14,40 +14,27 @@ static class StaticImageCache
     private const int MaxCacheSize = 25_000_000;
     private const int MaxItemSize = 1_000_000;
     
-    public static Image Load(string filePath)
+    public static Image LoadFromCache(string filePath)
     {
-        var isPathRooted = Path.IsPathRooted(filePath);
-        
         // check fallback path
-        if (!File.Exists(filePath))
-        {
-            var fallbackPath = Path.Combine(Helpers.Helpers.ApplicationFilesPath, filePath);
-
-            if (!File.Exists(fallbackPath))
-                throw new DocumentComposeException($"Cannot load provided image, file not found: {filePath}");   
+        filePath = AdjustPath(filePath);
 
-            filePath = fallbackPath;
-        }
-        
-        if (isPathRooted)
-            return LoadImage(filePath, false);
+        // check if the image is already in cache
+        if (Items.TryGetValue(filePath, out var cacheItem))
+            return cacheItem;
         
         // check file size
         var fileInfo = new FileInfo(filePath);
         
         if (fileInfo.Length > MaxItemSize)
-            return LoadImage(filePath, false);
-        
-        // check if the image is already in cache
-        if (Items.TryGetValue(filePath, out var cacheItem))
-            return cacheItem;
+            return DirectlyLoadFromFile(filePath, false);
         
         // if cache is larger than expected, the usage might be different from loading static images
         if (!CacheIsEnabled)
-            return LoadImage(filePath, false);
+            return DirectlyLoadFromFile(filePath, false);
         
         // create new cache item and add it to the cache
-        var image = LoadImage(filePath, true);
+        var image = DirectlyLoadFromFile(filePath, true);
         Items.TryAdd(filePath, image);
         
         // check cache size
@@ -56,9 +43,27 @@ static class StaticImageCache
         // return cached value
         return image;
     }
+
+    public static string AdjustPath(string filePath)
+    {
+        if (File.Exists(filePath))
+            return filePath;
+        
+        if (Path.IsPathRooted(filePath))
+            throw new DocumentComposeException($"Cannot load an image under the provided absolute path, file not found: {filePath}");
+
+        var fallbackPath = Path.Combine(Helpers.Helpers.ApplicationFilesPath, filePath);
+
+        if (!File.Exists(fallbackPath))
+            throw new DocumentComposeException($"Cannot load an image under the provided relative path, file not found: {filePath}");
+
+        return fallbackPath;
+    }
     
-    private static Image LoadImage(string filePath, bool isShared)
+    public static Image DirectlyLoadFromFile(string filePath, bool isShared)
     {
+        filePath = AdjustPath(filePath);
+        
         using var imageData = SkData.FromFile(filePath);
         return DecodeImage(imageData, isShared);
     }

+ 1 - 1
Source/QuestPDF/Infrastructure/SvgImage.cs

@@ -46,7 +46,7 @@ public sealed class SvgImage : IDisposable
             var fallbackPath = Path.Combine(Helpers.Helpers.ApplicationFilesPath, filePath);
                 
             if (!File.Exists(fallbackPath))
-                throw new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
+                throw new DocumentComposeException($"Cannot load an SVG image under the provided path, file not found: ${filePath}");
                 
             filePath = fallbackPath;
         }