From baaca4ca60e9c7f92272c3092e1fbe509f71472f Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Wed, 10 Aug 2016 21:41:43 +0100
Subject: [PATCH] Finish writing & wire up new directory previewer

---
 .../RequestRouter/RouteDirectoryListing.cs    |  9 ++++++
 GalleryShare/ThumbnailGenerator.cs            | 28 +++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/GalleryShare/RequestRouter/RouteDirectoryListing.cs b/GalleryShare/RequestRouter/RouteDirectoryListing.cs
index bb34fd3..a7ca9f9 100644
--- a/GalleryShare/RequestRouter/RouteDirectoryListing.cs
+++ b/GalleryShare/RequestRouter/RouteDirectoryListing.cs
@@ -4,6 +4,7 @@ using System.Xml;
 using System.Collections.Generic;
 using System.IO;
 using System.Threading.Tasks;
+using System.Drawing;
 
 namespace GalleryShare.RequestRouter
 {
@@ -14,6 +15,8 @@ namespace GalleryShare.RequestRouter
 
 		public int Priority { get; } = 5;
 
+		public Size thumbnailSize { get; private set; } = new Size(300, 200);
+
 		public RouteDirectoryListing()
 		{
 		}
@@ -32,6 +35,12 @@ namespace GalleryShare.RequestRouter
 
 		public async Task HandleRequestAsync(HttpListenerContext cycle, string requestedPath)
 		{
+			if (cycle.Request.QueryString["type"] == "thumbnail")
+			{
+				await ThumbnailGenerator.SendThumbnailPng(requestedPath, thumbnailSize, cycle);
+				return;
+			}
+			
 			cycle.Response.ContentType = "application/xml";
 
 			List<string> dirFiles = new List<string>(Directory.GetFiles(requestedPath));
diff --git a/GalleryShare/ThumbnailGenerator.cs b/GalleryShare/ThumbnailGenerator.cs
index bf249f6..f03da93 100644
--- a/GalleryShare/ThumbnailGenerator.cs
+++ b/GalleryShare/ThumbnailGenerator.cs
@@ -14,7 +14,13 @@ namespace GalleryShare
 	{
 		public static async Task SendThumbnailPng(string imagePath, Size thumbnailBounds, HttpListenerContext cycle)
 		{
-			Image imageToSend = GenerateFileThumbnail(imagePath, thumbnailBounds);
+			bool dummy;
+			Image imageToSend;
+			if (Directory.Exists(imagePath))
+				imageToSend = GenerateDirectoryThumbnail(imagePath, thumbnailBounds);
+			else
+				imageToSend = GenerateFileThumbnail(imagePath, thumbnailBounds, out dummy); // Has error image generation built in
+			
 			byte[] imageData = GetImageBytesPng(imageToSend);
 
 			cycle.Response.ContentType = "image/png";
@@ -33,14 +39,24 @@ namespace GalleryShare
 			Bitmap resultImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
 			using(Graphics context = Graphics.FromImage(resultImage))
 			{
-				for(int i = 0; i < 4; i++)
+				bool isErrorImage;
+				int filesSkipped = 0;
+				for(int i = 0; i < 4 + filesSkipped; i++)
 				{
 					using (Image fileThumbnail = GenerateFileThumbnail(
 						dirFiles[i],
-						new Size(thumbnailSize.Width, thumbnailSize.Height)
+						new Size(thumbnailSize.Width / 2, thumbnailSize.Height / 2),
+						out isErrorImage
 					))
 					{
-						PointF drawingPos = getDirectoryImageDrawPosition(i).Multiply(new PointF(resultImage.Width, resultImage.Height));
+						// If an error image has been generated, then we don't really want to
+						// include it in the preview
+						if(isErrorImage)
+						{
+							filesSkipped++;
+							continue;
+						}
+						PointF drawingPos = getDirectoryImageDrawPosition(i - filesSkipped).Multiply(new PointF(resultImage.Width, resultImage.Height));
 						context.DrawImage(fileThumbnail, drawingPos.ToIntPoint());
 					}
 				}
@@ -49,8 +65,9 @@ namespace GalleryShare
 			return resultImage;
 		}
 
-		public static Image GenerateFileThumbnail(string imagePath, Size thumbnailBounds)
+		public static Image GenerateFileThumbnail(string imagePath, Size thumbnailBounds, out bool errorImage)
 		{
+			errorImage = false;
 			try {
 				using (Bitmap rawImage = new Bitmap(imagePath)) {
 					float scaleFactor = Math.Min(
@@ -78,6 +95,7 @@ namespace GalleryShare
 			}
 			catch (Exception error)
 			{
+				errorImage = true;
 				Console.WriteLine("[{0}] Error generating thumbnail {1}: {2}",
 					DateTime.Now.ToShortTimeString(),
 					imagePath,
