Skip to content

Commit

Permalink
Use default templates as embedded resource (#119)
Browse files Browse the repository at this point in the history
* Change nuget spec

* Update template management project and unit tests

* Change VS Macros to relative path.

* Fix pre-build command format

* Change slash in command.

* Remove debug info

* Recover load default templates from file api

Co-authored-by: Yue Fei <[email protected]>
  • Loading branch information
moria97 and feiyueZz authored Dec 8, 2020
1 parent 1157599 commit cb00b7c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class TemplateCollectionProviderTests : ArtifactProviderTests

public TemplateCollectionProviderTests()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DefaultTemplates.tar.gz");
TemplateLayer defaultTempalteLayer = TemplateLayer.ReadFromFile(path);
TemplateLayer defaultTempalteLayer = TemplateLayer.ReadFromEmbeddedResource();
_cache.Set(ImageInfo.DefaultTemplateImageReference, defaultTempalteLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = System.Runtime.Caching.ObjectCache.InfiniteAbsoluteExpiration, Size = 0 });

PushLargeSizeManifest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,20 @@ public ITemplateCollectionProvider CreateTemplateCollectionProvider(string image
return new TemplateCollectionProvider(imageInfo, client, _templateCache, _configuration);
}

public void InitDefaultTemplates(string path = null)
public void InitDefaultTemplates()
{
TemplateLayer defaultTemplateLayer = TemplateLayer.ReadFromEmbeddedResource();
_templateCache.Set(ImageInfo.DefaultTemplateImageReference, defaultTemplateLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, Size = defaultTemplateLayer.Size, Priority = Extensions.Caching.Memory.CacheItemPriority.NeverRemove });
_templateCache.Set(defaultTemplateLayer.Digest, defaultTemplateLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, Size = defaultTemplateLayer.Size, Priority = Extensions.Caching.Memory.CacheItemPriority.NeverRemove });
}

public void InitDefaultTemplates(string path)
{
path ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constants.DefaultTemplatePath);

TemplateLayer defaultTemplateLayer = TemplateLayer.ReadFromFile(path);
_templateCache.Set(ImageInfo.DefaultTemplateImageReference, defaultTemplateLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, Size = defaultTemplateLayer.Size, Priority = Extensions.Caching.Memory.CacheItemPriority.NeverRemove });
_templateCache.Set(StreamUtility.CalculateDigestFromSha256(File.ReadAllBytes(path)), defaultTemplateLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, Size = defaultTemplateLayer.Size, Priority = Extensions.Caching.Memory.CacheItemPriority.NeverRemove });
_templateCache.Set(defaultTemplateLayer.Digest, defaultTemplateLayer, new MemoryCacheEntryOptions() { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, Size = defaultTemplateLayer.Size, Priority = Extensions.Caching.Memory.CacheItemPriority.NeverRemove });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</group>
</dependencies>
<contentFiles>
<files include="any/netcoreapp3.1/DefaultTemplates.tar.gz" buildAction="Content" copyToOutput="true" />
<files include="any/netcoreapp3.1/DefaultTemplates.tar.gz" buildAction="EmbeddedResource"/>
</contentFiles>
</metadata>
<files>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.Health.Fhir.Liquid.Converter\Microsoft.Health.Fhir.Liquid.Converter.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="../../bin/DefaultTemplates.tar.gz">
<Link>DefaultTemplates.tar.gz</Link>
</EmbeddedResource>
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="mkdir ..\..\bin &amp; tar -zcvf ../../bin/DefaultTemplates.tar.gz -C ../../data/Templates/Hl7v2 ." />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using DotLiquid;
using Microsoft.Health.Fhir.TemplateManagement.Exceptions;
using Microsoft.Health.Fhir.TemplateManagement.Utilities;
Expand All @@ -17,21 +18,50 @@ public class TemplateLayer : OCIArtifactLayer
{
public Dictionary<string, Template> TemplateContent { get; set; }

public static TemplateLayer ReadFromEmbeddedResource()
{
try
{
var defaultTemplateResourceName = $"{typeof(Constants).Namespace}.{Constants.DefaultTemplatePath}";
using Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(defaultTemplateResourceName);
return ReadFromStream(resourceStream);
}
catch (Exception ex)
{
throw new DefaultTemplatesInitializeException(TemplateManagementErrorCode.InitializeDefaultTemplateFailed, $"Load default template failed.", ex);
}
}

public static TemplateLayer ReadFromFile(string filePath)
{
try
{
using Stream fielStream = File.OpenRead(filePath);
return ReadFromStream(fielStream);
}
catch (Exception ex)
{
throw new DefaultTemplatesInitializeException(TemplateManagementErrorCode.InitializeDefaultTemplateFailed, $"Load default template failed.", ex);
}
}

private static TemplateLayer ReadFromStream(Stream stream)
{
try
{
var digest = StreamUtility.CalculateDigestFromSha256(stream);
stream.Position = 0;

TemplateLayer templateLayer = new TemplateLayer();
var rawBytes = File.ReadAllBytes(filePath);
var artifacts = StreamUtility.DecompressTarGzStream(new MemoryStream(rawBytes));
var artifacts = StreamUtility.DecompressTarGzStream(stream);
templateLayer.TemplateContent = TemplateLayerParser.ParseToTemplates(artifacts);
templateLayer.Digest = StreamUtility.CalculateDigestFromSha256(File.ReadAllBytes(filePath));
templateLayer.Digest = digest;
templateLayer.Size = artifacts.Sum(x => x.Value.Length);
return templateLayer;
}
catch (Exception ex)
{
throw new DefaultTemplatesInitializeException(TemplateManagementErrorCode.InitializeDefaultTemplateFailed, $"Load default template from {filePath} failed", ex);
throw new DefaultTemplatesInitializeException(TemplateManagementErrorCode.InitializeDefaultTemplateFailed, $"Load default template failed.", ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,16 @@ public static string CalculateDigestFromSha256(byte[] content)
hashedValue += string.Join(string.Empty, hashedStrings);
return hashedValue;
}

public static string CalculateDigestFromSha256(Stream stream)
{
using SHA256 mySHA256 = SHA256.Create();
string hashedValue = "sha256:";
byte[] hashData = mySHA256.ComputeHash(stream);
string[] hashedStrings = hashData.Select(x => string.Format("{0,2:x2}", x)).ToArray();
hashedValue += string.Join(string.Empty, hashedStrings);
return hashedValue;
}

}
}

0 comments on commit cb00b7c

Please sign in to comment.