Skip to content

Commit

Permalink
Added support for popular image formats
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeHoefling committed Jul 24, 2020
1 parent 1cc7eba commit fa0d505
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ After securing your files just set the top level folder permission and you will
| ⚡ Static Site ⚡ Handler 🤚 | DNN (DotNetNuke) | Supported |
|-------------------------------|------------------|-----------|
| 1.0.0 | 9.4.1 | Yes |
| 1.0.1 | 9.4.1 | Yes |

# Changes to DNN
This module makes minor changes to the DNN web.config and adds a new assembly to the directory. Below documents what changes are going to occur to your DNN site so you can make approprate decisions on installing this
Expand All @@ -38,6 +39,13 @@ This module makes minor changes to the DNN web.config and adds a new assembly to
* SVG Files
* Font Files
* CSS Files
* Image Files
* jpeg/jpg
* png
* gif
* tiff
* webp
* bmp

# Create Module Installer

Expand Down
14 changes: 14 additions & 0 deletions src/Dnn.StaticSiteHandler/ContentTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public string GetContentType(string extension)
return "application/font-woff2";//"font/woff2";
case "ttf":
return "application/font-ttf";//"font/ttf";
case "jpg":
case "jpeg":
return "image/jpeg";
case "png":
return "image/png";
case "gif":
return "image/gif";
case "tiff":
return "image/tiff";
case "webp":
return "image/webp";
case "bmp":
return "image/bmp";

}

return string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/Dnn.StaticSiteHandler/Dnn.StaticSiteHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<Product>⚡ Static Site ⚡ Handler 🤚</Product>
<Description>A Static Site Handler that secures HTML websites inside secured DNN folders using the DNN Permission system.</Description>
<Copyright>Andrew Hoefling © 2019</Copyright>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 15 additions & 1 deletion src/Dnn.StaticSiteHandler/Dnn.StaticSiteHandler.dnn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dotnetnuke type="Package" version="8.0">
<packages>
<package name="Dnn.StaticSiteHandler" type="Provider" version="1.0.0">
<package name="Dnn.StaticSiteHandler" type="Provider" version="1.0.1">
<friendlyName>⚡ Static Site ⚡ Handler 🤚</friendlyName>
<description>A Static Site Handler that secures HTML websites inside secured DNN folders using the DNN Permission system.</description>

Expand Down Expand Up @@ -30,6 +30,13 @@
<add name="WoffStaticSiteHandler" path="*.woff.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="Woff2StaticSiteHandler" path="*.Woff2.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="TtfStaticSiteHandler" path="*.ttfs.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="JpegStaticSiteHandler" path="*.jpeg.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="JpgStaticSiteHandler" path="*.jpg.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="PngStaticSiteHandler" path="*.png.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="GifStaticSiteHandler" path="*.gif.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="TiffStaticSiteHandler" path="*.tiff.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="WebpStaticSiteHandler" path="*.webp.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
<add name="BmpStaticSiteHandler" path="*.bmp.axd" verb="*" type="Dnn.StaticSiteHandler.HtmlServerHandler, Dnn.StaticSiteHandler" preCondition="integratedMode" />
</node>
</nodes>
</configuration>
Expand All @@ -44,6 +51,13 @@
<node path="/configuration/system.webServer/handlers/add[@name='WoffStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='Woff2StaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='TtfStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='JpegStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='JpgStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='PngStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='GifStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='TiffStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='WebpStaticSiteHandler']" action="remove" />
<node path="/configuration/system.webServer/handlers/add[@name='BmpStaticSiteHandler']" action="remove" />
</nodes>
</configuration>
</uninstall>
Expand Down
35 changes: 28 additions & 7 deletions src/Dnn.StaticSiteHandler/HtmlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,39 @@ public void ProcessRequest(HttpContext context)
var filePath = path.Replace(".axd", ".resources");
var fullFile = context.Server.MapPath($"~/{filePath}");

using (FileStream fileStream = File.OpenRead(fullFile))
using (var streamReader = new StreamReader(fileStream))
{
var content = streamReader.ReadToEnd();
context.Response.Write(content);
}

var extension = path.Replace(".axd", string.Empty).Split('.').LastOrDefault();
var manager = new ContentTypeManager();
var contentType = manager.GetContentType(extension);
if (!string.IsNullOrEmpty(contentType))
context.Response.ContentType = contentType;

using (FileStream fileStream = File.OpenRead(fullFile))
{
if (extension == "jpeg" || extension == "jpg" || extension == "png" ||
extension == "gif" || extension == "tiff" || extension == "webp" ||
extension == "bmp")
HandleImageResponse(fileStream);
else
HandleStandardResponse(fileStream);
}

void HandleImageResponse(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
context.Response.BinaryWrite(memoryStream.ToArray());
}
}

void HandleStandardResponse(Stream stream)
{
using (var streamReader = new StreamReader(stream))
{
var content = streamReader.ReadToEnd();
context.Response.Write(content);
}
}
}
}
}

0 comments on commit fa0d505

Please sign in to comment.