// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; namespace Spludlow.MetSat { /// /// Front end code for http://metsat.spludlow.co.uk/ /// public class WebImagesFront { public static string _ImageRootDirectory = Spludlow.Config.Get("MetSatWeb.FilePath"); public static string _ImageRootUrl = Spludlow.Config.Get("MetSatWeb.WebPath"); /// /// Get thumb and target urls for product /// public static DataTable ProductImagesTable(string product, string targetExtention, int maxCount, int maxTargetCount) { string find = ".thumb960.jpg"; string[] sampleFilenames = GetDescendingFilenames(product, "*" + find, maxCount); DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Thumb960 Thumb480 Target Title", "String String String String", }); foreach (string sampleFilename in sampleFilenames) { string name = sampleFilename.Substring(0, sampleFilename.Length - find.Length); string targetFilename = name + targetExtention; string thumb480Filename = name + ".thumb480.jpg"; string thumb960Filename = name + ".thumb960.jpg"; string thumb480Url = MapUrl(thumb480Filename); string thumb960Url = MapUrl(thumb960Filename); string targetUrl = ""; if (table.Rows.Count < maxTargetCount) targetUrl = MapUrl(targetFilename); string title = Path.GetFileName(name).Replace("-", " - "); table.Rows.Add(thumb960Url, thumb480Url, targetUrl, title); } return table; } /// /// Map a filesystem filename into a web url /// public static string MapUrl(string filename) { string name = filename.Substring(_ImageRootDirectory.Length + 1); return _ImageRootUrl + name.Replace(@"\", "/"); } /// /// List a product directory /// public static string[] GetDescendingFilenames(string product, string pattern, int count) { string directory = _ImageRootDirectory + @"\" + product; if (Directory.Exists(directory) == false) return new string[0]; List filenames = new List(Directory.GetFiles(directory, pattern)); filenames.Sort(); List descending = new List(); for (int index = filenames.Count - 1; index >= 0; --index) { descending.Add(filenames[index]); if (descending.Count == count) break; } return descending.ToArray(); } /// /// Set the image and target url of HyperLink to latest product image and thumbnail /// public static void SetLinkImage(System.Web.UI.WebControls.HyperLink hyperLink, string product, int size) { DataTable table = ProductImagesTable(product, ".dat", 1, 1); if (table.Rows.Count == 0) return; DataRow row = table.Rows[0]; hyperLink.ImageUrl = (string)row["Thumb" + size]; hyperLink.ToolTip = (string)row["Title"]; } } }