From 620cf2f892a8b8faa3ed995ba832fed942cdedc6 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 8 Jul 2016 17:12:57 +0100
Subject: [PATCH] Add preliminary file sending.\nThumbnail generation doesn't
 work yet (see http://stackoverflow.com/q/38271118/1460422).

---
 GalleryShare/Embed/DirectoryListing.xslt |  4 +++-
 GalleryShare/GalleryServer.cs            | 29 +++++++++++++++++++++---
 GalleryShare/GalleryShare.csproj         | 15 +++++++++---
 GalleryShare/GalleryShare.sln            | 17 --------------
 4 files changed, 41 insertions(+), 24 deletions(-)
 delete mode 100644 GalleryShare/GalleryShare.sln

diff --git a/GalleryShare/Embed/DirectoryListing.xslt b/GalleryShare/Embed/DirectoryListing.xslt
index ac1386a..7a11346 100644
--- a/GalleryShare/Embed/DirectoryListing.xslt
+++ b/GalleryShare/Embed/DirectoryListing.xslt
@@ -21,12 +21,14 @@
 	
 	<xsl:template match="ListingEntry[@Type='File']">
 		<figure class='preview file'>
-			<img src="{Name}?thumbnail" />
+			<img src="{Name}?type=thumbnail" />
+			<figcaption><xsl:value-of select="Name" /></figcaption>
 		</figure>
 	</xsl:template>
 	<xsl:template match="ListingEntry[@Type='Directory']">
 		<figure class='preview directory'>
 			(coming soon)
+			<figcaption><xsl:value-of select="Name" /></figcaption>
 		</figure>
 	</xsl:template>
 </xsl:stylesheet>
diff --git a/GalleryShare/GalleryServer.cs b/GalleryShare/GalleryServer.cs
index d7bb108..cd0bf4a 100644
--- a/GalleryShare/GalleryServer.cs
+++ b/GalleryShare/GalleryServer.cs
@@ -9,6 +9,8 @@ using System.Threading;
 using System.Xml;
 using System.Reflection;
 
+using ImageMagick;
+
 namespace GalleryShare
 {
 	enum OutputFunction
@@ -21,8 +23,12 @@ namespace GalleryShare
 
 	class GalleryServer 
 	{
+		static MimeSharp.Mime mimeDB = new MimeSharp.Mime();
+
 		int port;
 		string servingDirectory = Environment.CurrentDirectory;
+		int thumbnailWidth = 300;
+		int thumbnailHeight = 200;
 
 		HttpListener server = new HttpListener();
 		string prefix;
@@ -100,7 +106,7 @@ namespace GalleryShare
 					break;
 				
 				case OutputFunction.SendFile:
-					await sendFile(requestedPath);
+					await sendFile(cycle, requestedPath);
 					break;
 				
 				default:
@@ -216,9 +222,26 @@ namespace GalleryShare
 			await cycle.Response.OutputStream.WriteAsync(xsltData, 0, xsltData.Length);
 		}
 
-		private async Task sendFile(string requestedPath)
+		private async Task sendFile(HttpListenerContext cycle, string requestedPath)
 		{
-			Console.WriteLine("File requested: {0}", requestedPath);
+			if(cycle.Request.QueryString["type"] == "thumbnail")
+			{
+				// Send a thumbnail!
+				MagickImage img = new MagickImage(requestedPath);
+				img.Thumbnail(new MagickGeometry(thumbnailWidth, thumbnailHeight));
+				cycle.Response.ContentType = "image/webp";
+				img.Write(cycle.Response.OutputStream, MagickFormat.WebP);
+				img.Dispose();
+				return;
+			}
+
+			// Send the raw file
+
+			cycle.Response.ContentType = mimeDB.Lookup(requestedPath);
+
+			Stream fileData = File.OpenRead(requestedPath);
+			await fileData.CopyToAsync(cycle.Response.OutputStream);
+
 		}
 	}
 }
diff --git a/GalleryShare/GalleryShare.csproj b/GalleryShare/GalleryShare.csproj
index 590dd61..7dcada0 100644
--- a/GalleryShare/GalleryShare.csproj
+++ b/GalleryShare/GalleryShare.csproj
@@ -8,8 +8,6 @@
     <RootNamespace>GalleryShare</RootNamespace>
     <AssemblyName>GalleryShare</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
-    <ProductVersion>8.0.30703</ProductVersion>
-    <SchemaVersion>2.0</SchemaVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
     <DebugSymbols>true</DebugSymbols>
@@ -35,6 +33,13 @@
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Xml" />
+    <Reference Include="MimeSharp">
+      <HintPath>packages\MimeSharp.1.0.0\lib\MimeSharp.dll</HintPath>
+    </Reference>
+    <Reference Include="System.Drawing" />
+    <Reference Include="Magick.NET-Q16-AnyCPU">
+      <HintPath>..\packages\Magick.NET-Q16-AnyCPU.7.0.2.100\lib\net40-client\Magick.NET-Q16-AnyCPU.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Program.cs" />
@@ -43,10 +48,14 @@
     <Compile Include="Utilities.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <Import Project="packages\Magick.NET-Q8-x64.7.0.2.100\build\net40-client\Magick.NET-Q8-x64.targets" Condition="Exists('packages\Magick.NET-Q8-x64.7.0.2.100\build\net40-client\Magick.NET-Q8-x64.targets')" />
   <ItemGroup>
-    <Folder Include="Embed\**" />
+    <Folder Include="Embed\" />
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="Embed\**" />
   </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
 </Project>
diff --git a/GalleryShare/GalleryShare.sln b/GalleryShare/GalleryShare.sln
deleted file mode 100644
index 5270eec..0000000
--- a/GalleryShare/GalleryShare.sln
+++ /dev/null
@@ -1,17 +0,0 @@
-﻿
-Microsoft Visual Studio Solution File, Format Version 11.00
-# Visual Studio 2010
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GalleryShare", "GalleryShare.csproj", "{806258ED-F088-44A1-A6BE-2B8E4D1007E5}"
-EndProject
-Global
-	GlobalSection(SolutionConfigurationPlatforms) = preSolution
-		Debug|x86 = Debug|x86
-		Release|x86 = Release|x86
-	EndGlobalSection
-	GlobalSection(ProjectConfigurationPlatforms) = postSolution
-		{806258ED-F088-44A1-A6BE-2B8E4D1007E5}.Debug|x86.ActiveCfg = Debug|x86
-		{806258ED-F088-44A1-A6BE-2B8E4D1007E5}.Debug|x86.Build.0 = Debug|x86
-		{806258ED-F088-44A1-A6BE-2B8E4D1007E5}.Release|x86.ActiveCfg = Release|x86
-		{806258ED-F088-44A1-A6BE-2B8E4D1007E5}.Release|x86.Build.0 = Release|x86
-	EndGlobalSection
-EndGlobal
