Skip to content

Commit

Permalink
Merge pull request #89 from twsouthwick/hash-provider
Browse files Browse the repository at this point in the history
Replace string.GetHashCode() usage with IFileNameHasher
  • Loading branch information
pkgw authored Sep 21, 2020
2 parents 66d14e2 + 4591139 commit f28d470
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 121 deletions.
2 changes: 2 additions & 0 deletions WWTMVC5/App_Start/UnityConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.Practices.Unity;
using WWT.Providers;
using WWTWebservices;

namespace WWTMVC5
{
Expand Down Expand Up @@ -40,6 +41,7 @@ public static void RegisterTypes(IUnityContainer container)

// TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
container.RegisterType<IFileNameHasher, Net4x32BitFileNameHasher>(new ContainerControlledLifetimeManager());
}

private static void RegisterRequestProviders(IUnityContainer container)
Expand Down
50 changes: 5 additions & 45 deletions WWTMVC5/GetTourFile.aspx
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
<%@ Page Language="C#" ContentType="image/png" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="WWTWebservices" %>
<%
string returnString = "Erorr: No URL Specified";
string url = "";
string path = Server.MapPath(@"TourCache");
try
{
if (Request.Params["targeturl"] != null && Request.Params["filename"] != null)
{
url = Request.Params["targeturl"];
string targetfile = Request.Params["filename"];
string filename = path + "\\" + Math.Abs(url.GetHashCode()) + ".wtt";
if (!File.Exists(filename))
{
if (url.ToLower().StartsWith("http"))
{
using (WebClient wc = new WebClient())
{
byte[] data = wc.DownloadData(url);
<%@ Page Language="C#" ContentType="image/png" %>

//Response.ContentType = wc.ResponseHeaders["Content-type"].ToString();
int length = data.Length;
File.WriteAllBytes(filename, data);
//Response.OutputStream.Write(data, 0, length);
}
}
}
FileCabinet.Extract(filename, targetfile, Response);
}
}
catch (System.Exception e)
{
returnString = e.Message;
Response.Write(returnString);
}
%>
<%@ Import Namespace="WWT.Providers" %>
<%
RequestProvider.Get<GetTourFileProvider>().Run(this);
%>
49 changes: 4 additions & 45 deletions WWTMVC5/GetTourFile2.aspx
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
<%@ Page Language="C#" ContentType="image/png" %>
<%@ Page Language="C#" ContentType="image/png" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="WWTWebservices" %>
<%@ Import Namespace="WWT.Providers" %>
<%
string returnString = "Error: No URL Specified";
string url = "";
string path = Server.MapPath(@"TourCache");
try
{
if (Request.Params["targeturl"] != null && Request.Params["filename"] != null)
{
url = Request.Params["targeturl"];
string targetfile = Request.Params["filename"];
string filename = path + "\\" + Math.Abs(url.GetHashCode()) + ".wtt";
if (!File.Exists(filename))
{
if (url.ToLower().StartsWith("http"))
{
using (WebClient wc = new WebClient())
{
byte[] data = wc.DownloadData(url);
//Response.ContentType = wc.ResponseHeaders["Content-type"].ToString();
int length = data.Length;
File.WriteAllBytes(filename, data);
//Response.OutputStream.Write(data, 0, length);
}
}
}
FileCabinet.Extract(filename, targetfile, Response);
}
}
catch (System.Exception e)
{
returnString = e.Message;
Response.Write(returnString);
}
%>
RequestProvider.Get<GetTourFileProvider>().Run(this);
%>
7 changes: 7 additions & 0 deletions WWTWebservices/IFileNameHasher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace WWTWebservices
{
public interface IFileNameHasher
{
int HashName(string s);
}
}
39 changes: 39 additions & 0 deletions WWTWebservices/Net4x32BitFileNameHasher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace WWTWebservices
{
public class Net4x32BitFileNameHasher : IFileNameHasher
{
/// <summary>
/// Implementation to mimix string.GetHashCode() of 32-bit .NET 4.x. Was used originally for hashing to get id
/// </summary>
/// <param name="s">Input string</param>
/// <returns>Stable hash</returns>
public int HashName(string s)
=> Math.Abs(GetHashCode32BitNet4x(s));

private static unsafe int GetHashCode32BitNet4x(string s)
{
fixed (char* str = s.ToCharArray())
{
char* chPtr = str;
int num = 0x15051505;
int num2 = num;
int* numPtr = (int*)chPtr;

for (int i = s.Length; i > 0; i -= 4)
{
num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
if (i <= 2)
{
break;
}
num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
numPtr += 2;
}

return (num + (num2 * 0x5d588b65));
}
}
}
}
1 change: 0 additions & 1 deletion WWTWebservices/WWTUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace WWTWebservices
{



public class WWTUtil
{
public WWTUtil()
Expand Down
20 changes: 11 additions & 9 deletions src/WWT.Providers/Providers/GetTourFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ namespace WWT.Providers
{
public class GetTourFileProvider : RequestProvider
{
public override void Run(WwtContext context)
private readonly IFileNameHasher _hasher;

public GetTourFileProvider(IFileNameHasher hasher)
{
string returnString = "Error: No URL Specified";
string url = "";
_hasher = hasher;
}

public override void Run(WwtContext context)
{
string path = context.Server.MapPath(@"TourCache");

try
{
if (context.Request.Params["targeturl"] != null && context.Request.Params["filename"] != null)
{
url = context.Request.Params["targeturl"];

var url = context.Request.Params["targeturl"];
string targetfile = context.Request.Params["filename"];
string filename = path + "\\" + Math.Abs(url.GetHashCode()) + ".wtt";
string filename = Path.Combine(path, $"{_hasher.HashName(url)}.wtt");

if (!File.Exists(filename))
{
Expand All @@ -44,10 +47,9 @@ public override void Run(WwtContext context)
FileCabinet.Extract(filename, targetfile, context.Response);
}
}
catch (System.Exception e)
catch (Exception e)
{
returnString = e.Message;
context.Response.Write(returnString);
context.Response.Write(e.Message);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/WWT.Providers/Providers/TileImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace WWT.Providers
{
public class TileImageProvider : TileImage
{
private readonly IFileNameHasher _hasher;

public TileImageProvider(IFileNameHasher hasher)
{
_hasher = hasher;
}

public override void Run(WwtContext context)
{
{
Expand Down Expand Up @@ -53,7 +60,7 @@ public override void Run(WwtContext context)
url = "http://www.spitzer.caltech.edu/uploaded_files/images/0009/0848/sig12-011.jpg";
}

int hashID = Math.Abs(url.GetHashCode());
int hashID = _hasher.HashName(url);

//hashID = 12345;
string path = WWTUtil.GetCurrentConfigShare("DSSTileCache", true) + "\\imagesTiler\\dowloadImages\\";
Expand Down
9 changes: 8 additions & 1 deletion src/WWT.Providers/Providers/WMSEarthTodayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace WWT.Providers
{
public class WMSEarthTodayProvider : RequestProvider
{
private readonly IFileNameHasher _hasher;

public WMSEarthTodayProvider(IFileNameHasher hasher)
{
_hasher = hasher;
}

public override void Run(WwtContext context)
{
string query = context.Request.Params["Q"];
Expand All @@ -18,7 +25,7 @@ public override void Run(WwtContext context)
int tileX = Convert.ToInt32(values[1]);
int tileY = Convert.ToInt32(values[2]);
string wmsUrl = values[3];
string dirPart = Math.Abs(wmsUrl.GetHashCode()).ToString();
string dirPart = _hasher.HashName(wmsUrl).ToString();

string DSSTileCache = ConfigurationManager.AppSettings["DSSTileCache"];

Expand Down
9 changes: 8 additions & 1 deletion src/WWT.Providers/Providers/WMSMoonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace WWT.Providers
{
public class WMSMoonProvider : RequestProvider
{
private readonly IFileNameHasher _hasher;

public WMSMoonProvider(IFileNameHasher hasher)
{
_hasher = hasher;
}

public override void Run(WwtContext context)
{
string query = context.Request.Params["Q"];
Expand All @@ -17,7 +24,7 @@ public override void Run(WwtContext context)
int tileX = Convert.ToInt32(values[1]);
int tileY = Convert.ToInt32(values[2]);
string wmsUrl = values[3];
string dirPart = Math.Abs(wmsUrl.GetHashCode()).ToString();
string dirPart = _hasher.HashName(wmsUrl).ToString();
string filename;
string path;
filename = String.Format("\\\\wwt-sql01\\DSSTileCache\\WMS\\{3}\\{0}\\{2}\\{2}_{1}.png", level, tileX, tileY, dirPart);
Expand Down
9 changes: 8 additions & 1 deletion src/WWT.Providers/Providers/WMSToastProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace WWT.Providers
{
public class WMSToastProvider : RequestProvider
{
private readonly IFileNameHasher _hasher;

public WMSToastProvider(IFileNameHasher hasher)
{
_hasher = hasher;
}

public override void Run(WwtContext context)
{
string query = context.Request.Params["Q"];
Expand All @@ -18,7 +25,7 @@ public override void Run(WwtContext context)
int tileX = Convert.ToInt32(values[1]);
int tileY = Convert.ToInt32(values[2]);
string wmsUrl = values[3];
string dirPart = Math.Abs(wmsUrl.GetHashCode()).ToString();
string dirPart = _hasher.HashName(wmsUrl).ToString();
string filename;
string path;

Expand Down
27 changes: 10 additions & 17 deletions src/WWT.Providers/Providers/XML2WTT.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
using System.Net;
using System.Text;
using System.Xml;
using WWTWebservices;

namespace WWT.Providers
{
public abstract partial class WWTWeb_XML2WTT : RequestProvider
{
private readonly IFileNameHasher _hasher;

public WWTWeb_XML2WTT(IFileNameHasher hasher)
{
_hasher = hasher;
}

protected string MakeTourFromXML(WwtContext context, Stream InputStream, string baseDir)
{
XmlDocument doc = new XmlDocument();
Expand Down Expand Up @@ -181,33 +189,18 @@ protected string MakeTourFromXML(WwtContext context, Stream InputStream, string
{
}




//if (!string.IsNullOrEmpty(voiceUrl))
//{
// voicePath = dir + (Math.Abs(voiceUrl.GetHashCode()).ToString());
// if (!File.Exists(voicePath))
// {
// client.DownloadFile(voiceUrl, voicePath);
// }
// cab.AddFile(voicePath, true, tourGuid + "\\voice.wma");
//}



string tourfilename = dir + id.ToString() + ".wttxml";
File.WriteAllText(tourfilename, sb.ToString(), Encoding.UTF8);


cab.AddFile(tourfilename, false, "");
cab.AddFile(page + "\\images\\zoologo.png", true, tourGuid + "\\zoologo.png");

WebClient client = new WebClient();

if (!string.IsNullOrEmpty(musicUrl))
{
musicPath = dir + (Math.Abs(musicUrl.GetHashCode()).ToString());
musicPath = $"{dir}{_hasher.HashName(musicUrl)}";

if (!File.Exists(musicPath))
{
client.DownloadFile(musicUrl, musicPath);
Expand Down
6 changes: 6 additions & 0 deletions src/WWT.Providers/Providers/XML2WTTProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System.Configuration;
using WWTWebservices;

namespace WWT.Providers
{
public class XML2WTTProvider : WWTWeb_XML2WTT
{
public XML2WTTProvider(IFileNameHasher hasher)
: base(hasher)
{
}

public override void Run(WwtContext context)
{
//string etag = context.Request.Headers["If-None-Match"];
Expand Down

0 comments on commit f28d470

Please sign in to comment.