Convert GlimpseProvider and hAlphaToastProvider (#125)

This commit is contained in:
Taylor Southwick 2020-10-12 13:43:11 -07:00 коммит произвёл GitHub
Родитель 13fe0543a7
Коммит fa54c15fa3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 98 добавлений и 40 удалений

Просмотреть файл

@ -1,5 +1,4 @@
using System;
using System.Configuration;
using System.IO;
using WWTWebservices;
@ -7,6 +6,15 @@ namespace WWT.Providers
{
public class GlimpseProvider : RequestProvider
{
private readonly IPlateTilePyramid _plateTiles;
private readonly FilePathOptions _options;
public GlimpseProvider(IPlateTilePyramid plateTiles, FilePathOptions options)
{
_plateTiles = plateTiles;
_options = options;
}
public override void Run(IWwtContext context)
{
string query = context.Request.Params["Q"];
@ -15,11 +23,6 @@ namespace WWT.Providers
int tileX = Convert.ToInt32(values[1]);
int tileY = Convert.ToInt32(values[2]);
int octsetlevel = level;
string filename;
string wwtTilesDir = ConfigurationManager.AppSettings["WWTTilesDir"];
if (level > 11)
{
context.Response.Clear();
@ -34,14 +37,14 @@ namespace WWT.Providers
try
{
context.Response.ContentType = "image/png";
Stream s = PlateTilePyramid.GetFileStream(wwtTilesDir + "\\glimpse_L0to10_x0_y0.plate", level, tileX, tileY);
int length = (int)s.Length;
byte[] data = new byte[length];
s.Read(data, 0, length);
context.Response.OutputStream.Write(data, 0, length);
context.Response.Flush();
context.Response.End();
return;
using (Stream s = _plateTiles.GetStream(_options.WwtTilesDir, "glimpse_L0to10_x0_y0.plate", level, tileX, tileY))
{
s.CopyTo(context.Response.OutputStream);
context.Response.Flush();
context.Response.End();
return;
}
}
catch
{
@ -63,20 +66,20 @@ namespace WWT.Providers
int powLev3Diff = (int)Math.Pow(2, L - 1);
int X8 = X / powLev3Diff;
int Y8 = Y / powLev3Diff;
filename = string.Format(wwtTilesDir + @"\\glimpse_L1to11_x{0}_y{1}.plate", X8, Y8);
int L3 = L - 1;
int X3 = X % powLev3Diff;
int Y3 = Y % powLev3Diff;
context.Response.ContentType = "image/png";
Stream s = PlateTilePyramid.GetFileStream(filename, L3, X3, Y3);
int length = (int)s.Length;
byte[] data = new byte[length];
s.Read(data, 0, length);
context.Response.OutputStream.Write(data, 0, length);
context.Response.Flush();
context.Response.End();
return;
using (Stream s = _plateTiles.GetStream(_options.WwtTilesDir, $"glimpse_L1to11_x{X8}_y{Y8}.plate", L3, X3, Y3))
{
s.CopyTo(context.Response.OutputStream);
context.Response.Flush();
context.Response.End();
return;
}
}
catch
{
@ -86,12 +89,7 @@ namespace WWT.Providers
context.Response.End();
return;
}
}
// This file has returns which cause this warning to show in the generated files.
// This should be refactored, but that will be a bigger change.
#pragma warning disable 0162
}
}
}

Просмотреть файл

@ -1,5 +1,4 @@
using System;
using System.Configuration;
using System.IO;
using WWTWebservices;
@ -7,6 +6,15 @@ namespace WWT.Providers
{
public class hAlphaToastProvider : RequestProvider
{
private readonly IPlateTilePyramid _plateTiles;
private readonly FilePathOptions _options;
public hAlphaToastProvider(IPlateTilePyramid plateTiles, FilePathOptions options)
{
_plateTiles = plateTiles;
_options = options;
}
public override void Run(IWwtContext context)
{
string query = context.Request.Params["Q"];
@ -14,21 +22,18 @@ namespace WWT.Providers
int level = Convert.ToInt32(values[0]);
int tileX = Convert.ToInt32(values[1]);
int tileY = Convert.ToInt32(values[2]);
string file = "halpha";
string wwtTilesDir = ConfigurationManager.AppSettings["WWTTilesDir"];
if (level < 8)
{
context.Response.ContentType = "image/png";
Stream s = PlateTilePyramid.GetFileStream(String.Format(wwtTilesDir + "\\{0}.plate", file), level, tileX, tileY);
int length = (int)s.Length;
byte[] data = new byte[length];
s.Read(data, 0, length);
context.Response.OutputStream.Write(data, 0, length);
context.Response.Flush();
context.Response.End();
return;
using (Stream s = _plateTiles.GetStream(_options.WwtTilesDir, "halpha.plate", level, tileX, tileY))
{
s.CopyTo(context.Response.OutputStream);
context.Response.Flush();
context.Response.End();
return;
}
}
}
}

Просмотреть файл

@ -0,0 +1,31 @@
using System;
using System.IO;
using WWTWebservices;
namespace WWT.Providers.Tests
{
public class GlimpseProviderTests : ProviderTests<GlimpseProvider>
{
protected override int MaxLevel => 11;
protected override Stream GetStreamFromPlateTilePyramid(IPlateTilePyramid plateTiles, int level, int x, int y)
{
if (level == 11)
{
int powLev3Diff = (int)Math.Pow(2, level - 1);
int X8 = x / powLev3Diff;
int Y8 = y / powLev3Diff;
int L3 = level - 1;
int X3 = x % powLev3Diff;
int Y3 = y % powLev3Diff;
return plateTiles.GetStream(Options.WwtTilesDir, $"glimpse_L1to11_x{X8}_y{Y8}.plate", L3, X3, Y3);
}
else
{
return plateTiles.GetStream(Options.WwtTilesDir, "glimpse_L0to10_x0_y0.plate", level, x, y);
}
}
}
}

Просмотреть файл

@ -0,0 +1,24 @@
using System;
using System.IO;
using WWTWebservices;
using Xunit;
namespace WWT.Providers.Tests
{
public class hAlphaToastProviderTests : ProviderTests<hAlphaToastProvider>
{
protected override int MaxLevel => 8;
protected override Action<IResponse> NullStreamResponseHandler => null;
protected override Action<IResponse> StreamExceptionResponseHandler => null;
protected override void ExpectedResponseAboveMaxLevel(IResponse response)
{
Assert.Empty(response.OutputStream.ToArray());
}
protected override Stream GetStreamFromPlateTilePyramid(IPlateTilePyramid plateTiles, int level, int x, int y)
=> plateTiles.GetStream(Options.WwtTilesDir, "halpha.plate", level, x, y);
}
}