-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract file access pattern for DSS provider
This is an intial change to abstract file access using IPlateTilePyramid. This interface provides a single method that will take a plate file name along with the desired level and coordinates and will retrieve the associated image. This change includes two implementations of this: - ConfigurationManagerFilePlateTilePyramid allows for a request to a given plate file and returns a stream for the level and coordinates specified. - AzurePlateTilePyramid retrieves the file from Azure blob storage This new access method has been incorporated into the DSS.aspx page and will surface the content via Azure or local storage via a configuration flag (requires a restart of the service).
- Loading branch information
1 parent
66d14e2
commit d51b618
Showing
12 changed files
with
212 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Azure.Core; | ||
using Azure.Storage.Blobs; | ||
using System; | ||
using System.IO; | ||
|
||
namespace WWTWebservices.Azure | ||
{ | ||
public class AzurePlateTilePyramid : IPlateTilePyramid | ||
{ | ||
private readonly TokenCredential _credentials; | ||
private readonly string _storageUri; | ||
|
||
public AzurePlateTilePyramid(string storageUri, TokenCredential credentials) | ||
{ | ||
_credentials = credentials; | ||
_storageUri = storageUri; | ||
} | ||
|
||
public Stream GetStream(string plateName, int level, int x, int y) | ||
{ | ||
var name = Path.GetFileNameWithoutExtension(plateName).ToLowerInvariant(); | ||
var uri = new Uri($"{_storageUri}/{name}"); | ||
var container = new BlobContainerClient(uri, _credentials); | ||
var client = container.GetBlobClient($"L{level}X{x}Y{y}.png"); | ||
var download = client.Download(); | ||
|
||
return download.Value.Content; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net48</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Storage.Blobs" Version="12.6.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WWTWebservices\WWTWebservices.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="System.Configuration" /> | ||
</ItemGroup> | ||
|
||
</Project> |
69 changes: 69 additions & 0 deletions
69
WWTWebservices/ConfigurationManagerFilePlateTilePyramid.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace WWTWebservices | ||
{ | ||
public class ConfigurationManagerFilePlateTilePyramid : IPlateTilePyramid | ||
{ | ||
private readonly RegexDictionary _plateNamePath; | ||
|
||
public ConfigurationManagerFilePlateTilePyramid() | ||
{ | ||
_plateNamePath = new RegexDictionary | ||
{ | ||
{ @"dssterrapixel\.plate", "WWTTilesDir" }, | ||
{ @"DSSpngL5to12_x(\d+)_y(\d+)\.plate", "DssTerapixelDir" }, | ||
}; | ||
} | ||
|
||
public Stream GetStream(string plateName, int level, int x, int y) | ||
{ | ||
var path = _plateNamePath.GetPath(plateName); | ||
|
||
return PlateTilePyramid.GetFileStream(Path.Combine(path, plateName), level, x, y); | ||
} | ||
|
||
private class RegexDictionary : IEnumerable | ||
{ | ||
private readonly Dictionary<Regex, string> _regex = new Dictionary<Regex, string>(); | ||
private readonly ConcurrentDictionary<string, string> _strings = new ConcurrentDictionary<string, string>(); | ||
|
||
public void Add(string pattern, string configName) | ||
{ | ||
var path = ConfigurationManager.AppSettings[configName]; | ||
|
||
_regex.Add(new Regex(pattern, RegexOptions.Compiled), path); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => _strings.GetEnumerator(); | ||
|
||
/// <summary> | ||
/// Finds the directory of a platefile. | ||
/// </summary> | ||
/// <param name="plateFile">The platefile name</param> | ||
/// <returns>The directory the platefile is in</returns> | ||
/// <remarks> | ||
/// This uses a list of regex entries that match a prospective platefile to | ||
/// its known directory. Once identified, it is cached so no more regex | ||
/// matches (which are potentially very expensive) must be performed to | ||
/// identify its path. | ||
/// </remarks> | ||
public string GetPath(string plateFile) => _strings.GetOrAdd(plateFile, p => | ||
{ | ||
foreach (var t in _regex) | ||
{ | ||
if (t.Key.IsMatch(plateFile)) | ||
{ | ||
return t.Value; | ||
} | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.IO; | ||
|
||
namespace WWTWebservices | ||
{ | ||
public interface IPlateTilePyramid | ||
{ | ||
Stream GetStream(string plateName, int level, int x, int y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace WWT.Providers | ||
{ | ||
internal static class WwtContextExtensions | ||
{ | ||
public static (int level, int tileX, int tileY) GetLevelAndCoordinates(this WwtContext context) | ||
{ | ||
string query = context.Request.Params["Q"]; | ||
string[] values = query.Split(','); | ||
int level = Convert.ToInt32(values[0]); | ||
int tileX = Convert.ToInt32(values[1]); | ||
int tileY = Convert.ToInt32(values[2]); | ||
|
||
return (level, tileX, tileY); | ||
} | ||
} | ||
} |