From 35184e22e089ae5de274b94c3201f432dac5bb87 Mon Sep 17 00:00:00 2001 From: samantha copeland Date: Wed, 10 Jul 2024 19:18:03 -0500 Subject: [PATCH] add loads of expressions to aid in strong typing to widget value retrievals refactor image sizes --- CMSInterfaces/ParmParser.cs | 197 +++++++++++++++--- .../GalleryObjects/GalleryMetaData.cs | 5 +- PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs | 12 +- .../PhotoGalleryAdminMetaData.ascx.cs | 5 +- .../PhotoGalleryPrettyPhoto.ascx.cs | 12 +- PluginPhotoGallery/PublicGalleryBase.cs | 20 +- PluginPhotoGallery/PublicGallerySingleBase.cs | 15 +- WebControls/FileData.cs | 5 + 8 files changed, 213 insertions(+), 58 deletions(-) diff --git a/CMSInterfaces/ParmParser.cs b/CMSInterfaces/ParmParser.cs index 3821ba0f..1a438cb3 100644 --- a/CMSInterfaces/ParmParser.cs +++ b/CMSInterfaces/ParmParser.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; +using System.Reflection; using System.Web; /* @@ -17,49 +19,196 @@ namespace Carrotware.CMS.Interface { public static class ParmParser { - public static string GetParmValue(this Dictionary parmDictionary, string sKey) { + public static void SetGuidValue(this T widgetObject, Expression> widgetProperty, Guid? value) { + SetGuidValue(widgetObject, widgetProperty, value, Guid.Empty); + } + + public static void SetGuidValue(this T widgetObject, Expression> widgetProperty, Guid? value, Guid defaultValue) { + Guid val = defaultValue; + if (value.HasValue) { + val = value.Value; + } + + SetValue(widgetObject, widgetProperty, val); + } + + public static void SetStringValue(this T widgetObject, Expression> widgetProperty, string value) { + SetStringValue(widgetObject, widgetProperty, value, string.Empty); + } + + public static void SetStringValue(this T widgetObject, Expression> widgetProperty, string value, string defaultValue) { + string val = defaultValue; + if (!string.IsNullOrEmpty(value)) { + val = value; + } + + SetValue(widgetObject, widgetProperty, val); + } + + public static void SetIntValue(this T widgetObject, Expression> widgetProperty, int? value) { + SetIntValue(widgetObject, widgetProperty, value, 0); + } + + public static void SetIntValue(this T widgetObject, Expression> widgetProperty, int? value, int defaultValue) { + int val = defaultValue; + if (value.HasValue) { + val = value.Value; + } + + SetValue(widgetObject, widgetProperty, val); + } + + public static void SetBoolValue(this T widgetObject, Expression> widgetProperty, bool? value) { + SetBoolValue(widgetObject, widgetProperty, value, false); + } + + public static void SetBoolValue(this T widgetObject, Expression> widgetProperty, bool? value, bool defaultValue) { + bool val = defaultValue; + if (value.HasValue) { + val = value.Value; + } + + SetValue(widgetObject, widgetProperty, val); + } + + public static void SetValue(this T widgetObject, Expression> widgetProperty, TValue value) { + var selector = widgetProperty.Body as MemberExpression; + if (selector != null) { + var property = selector.Member as PropertyInfo; + if (property != null) { + property.SetValue(widgetObject, value, null); + } + } + } + + public static string GetValue(this T widgetObject, Expression> widgetProperty) { + return GetValue(widgetObject, widgetProperty, string.Empty); + } + + public static string GetStringValue(this T widgetObject, Expression> widgetProperty) { + return GetValue(widgetObject, widgetProperty, string.Empty); + } + + public static string GetValue(this T widgetObject, Expression> widgetProperty, string defaultVal) { + string foundVal = defaultVal; + var selector = widgetProperty.Body as MemberExpression; + + if (selector != null && widgetObject is IWidgetParmData) { + var property = selector.Member as PropertyInfo; + foundVal = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal); + } + + return foundVal; + } + + public static int GetIntValue(this T widgetObject, Expression> widgetProperty) { + return GetValue(widgetObject, widgetProperty, 0); + } + + public static int GetValue(this T widgetObject, Expression> widgetProperty, int defaultVal) { + int foundVal = defaultVal; + var selector = widgetProperty.Body as MemberExpression; + + if (selector != null && widgetObject is IWidgetParmData) { + var property = selector.Member as PropertyInfo; + var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal); + if (!string.IsNullOrEmpty(foundString)) { + foundVal = Convert.ToInt32(foundString); + } + } + + return foundVal; + } + + public static bool GetBoolValue(this T widgetObject, Expression> widgetProperty) { + return GetValue(widgetObject, widgetProperty, false); + } + + public static bool GetValue(this T widgetObject, Expression> widgetProperty, bool defaultVal) { + bool foundVal = defaultVal; + var selector = widgetProperty.Body as MemberExpression; + + if (selector != null && widgetObject is IWidgetParmData) { + var property = selector.Member as PropertyInfo; + var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal); + if (!string.IsNullOrEmpty(foundString)) { + foundVal = Convert.ToBoolean(foundString); + } + } + + return foundVal; + } + + public static Guid GetGuidValue(this T widgetObject, Expression> widgetProperty) { + return GetValue(widgetObject, widgetProperty, Guid.Empty); + } + + public static Guid GetValue(this T widgetObject, Expression> widgetProperty, Guid defaultVal) { + Guid foundVal = defaultVal; + var selector = widgetProperty.Body as MemberExpression; + + if (selector != null && widgetObject is IWidgetParmData) { + var property = selector.Member as PropertyInfo; + var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal.ToString()); + if (!string.IsNullOrEmpty(foundString)) { + foundVal = new Guid(foundString); + } + } + + return foundVal; + } + + public static string GetParmValue(this Dictionary parmDictionary, string key) { string ret = null; if (parmDictionary.Any()) { ret = (from c in parmDictionary - where c.Key.ToLowerInvariant() == sKey.ToLowerInvariant() + where c.Key.ToLowerInvariant() == key.ToLowerInvariant() select c.Value).FirstOrDefault(); } return ret; } - public static string GetParmValue(this Dictionary parmDictionary, string sKey, string sDefault) { + public static string GetParmValue(this Dictionary parmDictionary, string key, string defaultValue) { string ret = null; if (parmDictionary.Any()) { ret = (from c in parmDictionary - where c.Key.ToLowerInvariant() == sKey.ToLowerInvariant() + where c.Key.ToLowerInvariant() == key.ToLowerInvariant() select c.Value).FirstOrDefault(); } - ret = ret == null ? sDefault : ret; + ret = ret == null ? defaultValue : ret; return ret; } - public static string GetParmValueDefaultEmpty(Dictionary parmDictionary, string sKey, string sDefault) { - string ret = GetParmValue(parmDictionary, sKey, sDefault); + public static string GetParmValue(this Dictionary parmDictionary, string key, bool defaultValue) { + return parmDictionary.GetParmValue(key, defaultValue.ToString().ToLowerInvariant()); + } + + public static string GetParmValue(this Dictionary parmDictionary, string key, int defaultValue) { + return parmDictionary.GetParmValue(key, defaultValue.ToString()); + } + + public static string GetParmValueDefaultEmpty(this Dictionary parmDictionary, string key, string defaultValue) { + string ret = GetParmValue(parmDictionary, key, defaultValue); - ret = string.IsNullOrEmpty(ret) ? sDefault : ret; + ret = string.IsNullOrEmpty(ret) ? defaultValue : ret; return ret; } - public static List GetParmValueList(Dictionary parmDictionary, string sKey) { - sKey = sKey.EndsWith("|") ? sKey : sKey + "|"; - sKey = sKey.ToLowerInvariant(); + public static List GetParmValueList(this Dictionary parmDictionary, string key) { + key = key.EndsWith("|") ? key : key + "|"; + key = key.ToLowerInvariant(); List ret = new List(); if (parmDictionary.Any()) { ret = (from c in parmDictionary - where c.Key.ToLowerInvariant().StartsWith(sKey) + where c.Key.ToLowerInvariant().StartsWith(key) select c.Value).ToList(); } @@ -68,6 +217,10 @@ where c.Key.ToLowerInvariant().StartsWith(sKey) #region QueryString Parsers + public static bool IsWebView { + get { return (HttpContext.Current != null); } + } + public static Guid GetGuidPageIDFromQuery() { return GetGuidParameterFromQuery("pageid"); } @@ -76,24 +229,20 @@ public static Guid GetGuidIDFromQuery() { return GetGuidParameterFromQuery("id"); } - public static Guid GetGuidParameterFromQuery(string ParmName) { + public static Guid GetGuidParameterFromQuery(string parmName) { Guid id = Guid.Empty; - if (HttpContext.Current != null) { - if (HttpContext.Current.Request.QueryString[ParmName] != null - && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[ParmName].ToString())) { - id = new Guid(HttpContext.Current.Request.QueryString[ParmName].ToString()); - } + if (IsWebView && HttpContext.Current.Request.QueryString[parmName] != null + && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[parmName].ToString())) { + id = new Guid(HttpContext.Current.Request.QueryString[parmName].ToString()); } return id; } - public static string GetStringParameterFromQuery(string ParmName) { + public static string GetStringParameterFromQuery(string parmName) { string id = string.Empty; - if (HttpContext.Current != null) { - if (HttpContext.Current.Request.QueryString[ParmName] != null - && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[ParmName].ToString())) { - id = HttpContext.Current.Request.QueryString[ParmName].ToString(); - } + if (IsWebView && HttpContext.Current.Request.QueryString[parmName] != null + && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[parmName].ToString())) { + id = HttpContext.Current.Request.QueryString[parmName].ToString(); } return id; } diff --git a/PluginPhotoGallery/GalleryObjects/GalleryMetaData.cs b/PluginPhotoGallery/GalleryObjects/GalleryMetaData.cs index 8a71b6b2..1f444ff4 100644 --- a/PluginPhotoGallery/GalleryObjects/GalleryMetaData.cs +++ b/PluginPhotoGallery/GalleryObjects/GalleryMetaData.cs @@ -36,11 +36,14 @@ internal GalleryMetaData(tblGalleryImageMeta gal) { public string ImageMetaData { get; set; } public void ValidateGalleryImage() { + if (string.IsNullOrEmpty(this.GalleryImage)) { + throw new Exception("Image path must be provided."); + } if (this.GalleryImage.Contains("../") || this.GalleryImage.Contains(@"..\")) { throw new Exception("Cannot use relative paths."); } if (this.GalleryImage.Contains(":")) { - throw new Exception("Cannot specify drive letters."); + throw new Exception("Cannot specify drive letters or other protocols."); } if (this.GalleryImage.Contains("//") || this.GalleryImage.Contains(@"\\")) { throw new Exception("Cannot use UNC paths."); diff --git a/PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs b/PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs index 285e4668..97fd262b 100644 --- a/PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs +++ b/PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs @@ -30,6 +30,7 @@ protected string SetSitePath(string sPath) { protected void BuildFolderList() { List lstFolders = new List(); + var now = DateTime.Now.Date; string sRoot = HttpContext.Current.Server.MapPath("~/"); @@ -40,12 +41,12 @@ protected void BuildFolderList() { subdirs = null; } - lstFolders.Add(new FileData { FileName = " -- whole site -- ", FolderPath = "/", FileDate = DateTime.Now }); + lstFolders.Add(new FileData { FileName = " -- whole site -- ", FolderPath = "/", FileDate = now }); if (subdirs != null) { foreach (string theDir in subdirs) { string w = FileDataHelper.MakeWebFolderPath(theDir); - lstFolders.Add(new FileData { FileName = w, FolderPath = w, FileDate = DateTime.Now }); + lstFolders.Add(new FileData { FileName = w, FolderPath = w, FileDate = now }); string[] subdirs2; try { @@ -57,7 +58,7 @@ protected void BuildFolderList() { if (subdirs2 != null) { foreach (string theDir2 in subdirs2) { string w2 = FileDataHelper.MakeWebFolderPath(theDir2); - lstFolders.Add(new FileData { FileName = w2, FolderPath = w2, FileDate = DateTime.Now }); + lstFolders.Add(new FileData { FileName = w2, FolderPath = w2, FileDate = now }); } } } @@ -66,9 +67,12 @@ protected void BuildFolderList() { lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith(SiteData.AdminFolderPath)); lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/bin/")); lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/obj/")); + lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/.")); + lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_code/")); lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_data/")); + lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_start/")); - ddlFolders.DataSource = lstFolders.OrderBy(f => f.FileName); + ddlFolders.DataSource = lstFolders.Distinct().OrderBy(f => f.FileName); ddlFolders.DataBind(); } diff --git a/PluginPhotoGallery/PhotoGalleryAdminMetaData.ascx.cs b/PluginPhotoGallery/PhotoGalleryAdminMetaData.ascx.cs index 94d1eefa..ff04574f 100644 --- a/PluginPhotoGallery/PhotoGalleryAdminMetaData.ascx.cs +++ b/PluginPhotoGallery/PhotoGalleryAdminMetaData.ascx.cs @@ -63,11 +63,14 @@ protected void btnSave_Click(object sender, EventArgs e) { } protected void ValidateGalleryImage(string imageFile) { + if (string.IsNullOrEmpty(imageFile)) { + throw new Exception("Image path must be provided."); + } if (imageFile.Contains("../") || imageFile.Contains(@"..\")) { throw new Exception("Cannot use relative paths."); } if (imageFile.Contains(":")) { - throw new Exception("Cannot specify drive letters."); + throw new Exception("Cannot specify drive letters or other protocols."); } if (imageFile.Contains("//") || imageFile.Contains(@"\\")) { throw new Exception("Cannot use UNC paths."); diff --git a/PluginPhotoGallery/PhotoGalleryPrettyPhoto.ascx.cs b/PluginPhotoGallery/PhotoGalleryPrettyPhoto.ascx.cs index 0d51b966..baca4c28 100644 --- a/PluginPhotoGallery/PhotoGalleryPrettyPhoto.ascx.cs +++ b/PluginPhotoGallery/PhotoGalleryPrettyPhoto.ascx.cs @@ -1,9 +1,9 @@ -using System; +using Carrotware.CMS.Core; +using Carrotware.CMS.Interface; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; -using Carrotware.CMS.Core; -using Carrotware.CMS.Interface; namespace Carrotware.CMS.UI.Plugins.PhotoGallery { @@ -26,7 +26,7 @@ public string GetThumbSize() { [Description("Gallery appearance (pretty photo skin)")] [Widget(WidgetAttribute.FieldMode.DropDownList, "lstPrettySkins")] - public string PrettyPhotoSkin { get; set; } + public string PrettyPhotoSkin { get; set; } = "light_rounded"; [Widget(WidgetAttribute.FieldMode.DictionaryList)] public Dictionary lstPrettySkins { @@ -55,13 +55,13 @@ protected void Page_Load(object sender, EventArgs e) { try { string sFoundVal = GetParmValue("PrettyPhotoSkin", "light_rounded"); - if (!String.IsNullOrEmpty(sFoundVal)) { + if (!string.IsNullOrEmpty(sFoundVal)) { this.PrettyPhotoSkin = sFoundVal; } } catch (Exception ex) { } } - if (String.IsNullOrEmpty(PrettyPhotoSkin)) { + if (string.IsNullOrEmpty(PrettyPhotoSkin)) { PrettyPhotoSkin = "light_rounded"; } diff --git a/PluginPhotoGallery/PublicGalleryBase.cs b/PluginPhotoGallery/PublicGalleryBase.cs index 2a54d052..d6002611 100644 --- a/PluginPhotoGallery/PublicGalleryBase.cs +++ b/PluginPhotoGallery/PublicGalleryBase.cs @@ -21,11 +21,11 @@ public abstract class PublicGalleryBase : WidgetParmDataUserControl, IWidgetEdit [Description("Display gallery heading")] [Widget(WidgetAttribute.FieldMode.CheckBox)] - public bool ShowHeading { get; set; } + public bool ShowHeading { get; set; } = false; [Description("Scale gallery images")] [Widget(WidgetAttribute.FieldMode.CheckBox)] - public bool ScaleImage { get; set; } + public bool ScaleImage { get; set; } = true; [Widget(WidgetAttribute.FieldMode.DictionaryList)] public Dictionary lstGalleryID { @@ -72,23 +72,17 @@ public virtual Dictionary lstSizes { public virtual void GetPublicParmValues() { if (this.PublicParmValues.Any()) { - this.ScaleImage = false; + this.ScaleImage = true; this.ShowHeading = false; try { - string sFoundVal = GetParmValue("ShowHeading", "false"); - - if (!String.IsNullOrEmpty(sFoundVal)) { - this.ShowHeading = Convert.ToBoolean(sFoundVal); - } + var foundVal = this.GetValue(x => x.ShowHeading, this.ShowHeading); + this.SetBoolValue(x => x.ShowHeading, foundVal); } catch (Exception ex) { } try { - string sFoundVal = GetParmValue("ScaleImage", "false"); - - if (!String.IsNullOrEmpty(sFoundVal)) { - this.ScaleImage = Convert.ToBoolean(sFoundVal); - } + var foundVal = this.GetValue(x => x.ScaleImage, this.ScaleImage); + this.SetBoolValue(x => x.ScaleImage, foundVal); } catch (Exception ex) { } } } diff --git a/PluginPhotoGallery/PublicGallerySingleBase.cs b/PluginPhotoGallery/PublicGallerySingleBase.cs index 4bae606c..dce18627 100644 --- a/PluginPhotoGallery/PublicGallerySingleBase.cs +++ b/PluginPhotoGallery/PublicGallerySingleBase.cs @@ -24,7 +24,7 @@ public abstract class PublicGallerySingleBase : PublicGalleryBase { [Description("Gallery image pixel height/width")] [Widget(WidgetAttribute.FieldMode.DropDownList, "lstSizes")] - public int ThumbSize { get; set; } + public int ThumbSize { get; set; } = 100; public override void GetPublicParmValues() { base.GetPublicParmValues(); @@ -33,19 +33,16 @@ public override void GetPublicParmValues() { this.ThumbSize = 100; try { - string sFoundVal = GetParmValue("GalleryID", Guid.Empty.ToString()); + var foundVal = this.GetValue(x => x.GalleryID, Guid.Empty); - if (!String.IsNullOrEmpty(sFoundVal)) { - this.GalleryID = new Guid(sFoundVal); + if (foundVal != Guid.Empty && this.GalleryID == Guid.Empty) { + this.SetGuidValue(x => x.GalleryID, foundVal); } } catch (Exception ex) { } try { - string sFoundVal = GetParmValueDefaultEmpty("ThumbSize", "150"); - - if (!String.IsNullOrEmpty(sFoundVal)) { - this.ThumbSize = Convert.ToInt32(sFoundVal); - } + var foundVal = this.GetValue(x => x.ThumbSize, this.ThumbSize); + this.SetIntValue(x => x.ThumbSize, foundVal); } catch (Exception ex) { } } } diff --git a/WebControls/FileData.cs b/WebControls/FileData.cs index a1aba3ab..b654ed19 100644 --- a/WebControls/FileData.cs +++ b/WebControls/FileData.cs @@ -213,6 +213,11 @@ where b.Key.ToLowerInvariant() == f.FileExtension.ToLowerInvariant() } catch (Exception ex) { } } + if (File.Exists(testFile) == false) { + f.FileName = Path.GetFileName(myFileName); + f.FolderPath = MakeWebFolderPath(sQuery); + } + return f; }