diff --git a/Core/DAL/DataService.cs b/Core/DAL/DataService.cs index ba4f43e..9f53dd5 100644 --- a/Core/DAL/DataService.cs +++ b/Core/DAL/DataService.cs @@ -73,7 +73,7 @@ namespace LeafWeb.Core.DAL { return _db.LeafInputFiles.Find(id); } - + public IQueryable GetLeafInputFiles(LeafInputStatusType status) { return diff --git a/Core/Entities/LeafOutputFile.cs b/Core/Entities/LeafOutputFile.cs index afe412d..0793f8c 100644 --- a/Core/Entities/LeafOutputFile.cs +++ b/Core/Entities/LeafOutputFile.cs @@ -10,5 +10,8 @@ namespace LeafWeb.Core.Entities public string Filename { get; set; } public byte[] Contents { get; set; } + + // convention for the filename which LeafCharter uses + public bool IsLeafChartFile => Filename?.StartsWith("cntrlcomparison") ?? false; } } diff --git a/Web/Controllers/LeafCharterController.cs b/Web/Controllers/LeafCharterController.cs index 959e685..ed087cb 100644 --- a/Web/Controllers/LeafCharterController.cs +++ b/Web/Controllers/LeafCharterController.cs @@ -16,30 +16,47 @@ namespace LeafWeb.Web.Controllers { public ActionResult Index(int leafInputFileId) { + var hasLeafOutputFile = + DataService + .GetLeafInputFile(leafInputFileId)? + .LeafOutputFiles? + .FirstOrDefault(f => f.IsLeafChartFile) != null; + return View(leafInputFileId); } public ActionResult LeafCharts(int leafInputFileId, int number) { - var leafInputFile = DataService.GetLeafInputFile(leafInputFileId); - var leafOutputFile = leafInputFile.LeafOutputFiles.FirstOrDefault(f => f.Filename.StartsWith("cntrlcomparison")); - //if (leafOutputFile == null) - // return View(); // TODO: break + var leafOutputFile = + DataService + .GetLeafInputFile(leafInputFileId)? + .LeafOutputFiles? + .FirstOrDefault(f => f.IsLeafChartFile); - CntrlComparison[] cntrlComparison; - using (var parser = new CntrlComparisonParser(leafOutputFile.Contents)) + if (leafOutputFile == null) + return View(); // TODO: break + + return File(GetChartImage(leafOutputFile.Contents, number), "image/png", "mychart.png"); + } + + private byte[] GetChartImage(byte[] fileContents, int number, ChartImageFormat format = ChartImageFormat.Png) + { + CntrlComparison[] cntrlComparison; + using (var parser = new CntrlComparisonParser(fileContents)) cntrlComparison = parser.Parse(); - var curveData = CurveDataConverter.Convert(cntrlComparison).ToArray(); + var curveData = CurveDataConverter.Convert(cntrlComparison).ToArray(); - var charts = LeafWebCharter.ProduceCharts(curveData[2]); + var charts = LeafWebCharter.ProduceCharts(curveData[2]); - using (var ms = new MemoryStream()) + //Image.FromFile() + + using (var ms = new MemoryStream()) { - charts.Skip(number).First().SaveImage(ms, ChartImageFormat.Png); + charts.Skip(number).First().SaveImage(ms, format); ms.Seek(0, SeekOrigin.Begin); - return File(ms.ToArray(), "image/png", "mychart.png"); + return ms.ToArray(); } } diff --git a/Web/ViewModels/LeafOutput/LeafOutputViewModel.cs b/Web/ViewModels/LeafOutput/LeafOutputViewModel.cs index b4d19bc..92eae6a 100644 --- a/Web/ViewModels/LeafOutput/LeafOutputViewModel.cs +++ b/Web/ViewModels/LeafOutput/LeafOutputViewModel.cs @@ -13,6 +13,7 @@ namespace LeafWeb.Web.ViewModels.LeafOutput public string CurrentStatus { get; set; } public string[] ErrorMessages { get; set; } public string[] LeafOutputFilenames { get; set; } + public bool HasLeafChartOutputFile { get; set; } public string LeafInputName { get; set; } public string LeafInputIdentifier { get; set; } public string LeafInputSiteId { get; set; } @@ -33,6 +34,9 @@ namespace LeafWeb.Web.ViewModels.LeafOutput .Select(o => o.Filename) .ToArray() ?? new string[] {})) + .ForMember(dest => dest.HasLeafChartOutputFile, + opt => opt.ResolveUsing( + file => file.LeafOutputFiles?.Any(o => o.IsLeafChartFile))) .ForMember(dest => dest.LeafInputName, opt => opt.MapFrom(src => src.LeafInput.Name)) .ForMember(dest => dest.LeafInputIdentifier, opt => opt.MapFrom(src => src.LeafInput.Identifier)) .ForMember(dest => dest.LeafInputSiteId, opt => opt.MapFrom(src => src.LeafInput.SiteId))