Updating comments and code styling

This commit is contained in:
Benedek Farkas
2024-11-27 21:08:34 +01:00
parent deb49cb616
commit 94343e9b66

View File

@@ -21,8 +21,8 @@ namespace Orchard.MediaProcessing.Filters {
private readonly IImageProfileManager _profileManager; private readonly IImageProfileManager _profileManager;
private MediaHtmlFilterSettingsPart _settingsPart; private MediaHtmlFilterSettingsPart _settingsPart;
private static Dictionary<string, string> _validExtensions = new Dictionary<string, string> { private static readonly Dictionary<string, string> _validExtensions = new Dictionary<string, string> {
{ ".jpeg", "jpg" }, //For example: .jpeg supports compression (quality), format to 'jpg' { ".jpeg", "jpg" }, // For example: .jpeg supports compression (quality), format to 'jpg'.
{ ".jpg", "jpg" }, { ".jpg", "jpg" },
{ ".png", null }}; { ".png", null }};
@@ -76,6 +76,8 @@ namespace Orchard.MediaProcessing.Filters {
&& !src.Contains("_Profiles") && !src.Contains("_Profiles")
&& _validExtensions.ContainsKey(ext)) { && _validExtensions.ContainsKey(ext)) {
try { try {
// If the image has a combination of width, height and valid extension, that is not already in
// _Profiles, then process the image.
var newSrc = TryGetImageProfilePath(src, ext, width, height); var newSrc = TryGetImageProfilePath(src, ext, width, height);
imgTag = SetAttributeValue(imgTag, "src", newSrc); imgTag = SetAttributeValue(imgTag, "src", newSrc);
} }
@@ -88,6 +90,7 @@ namespace Orchard.MediaProcessing.Filters {
private string TryGetImageProfilePath(string src, string ext, int width, int height) { private string TryGetImageProfilePath(string src, string ext, int width, int height) {
var filters = new List<FilterRecord> { var filters = new List<FilterRecord> {
// Factor in a minimum width and height with respect to higher pixel density devices.
CreateResizeFilter(width * Settings.DensityThreshold, height * Settings.DensityThreshold) CreateResizeFilter(width * Settings.DensityThreshold, height * Settings.DensityThreshold)
}; };
@@ -106,6 +109,8 @@ namespace Orchard.MediaProcessing.Filters {
} }
private FilterRecord CreateResizeFilter(int width, int height) { private FilterRecord CreateResizeFilter(int width, int height) {
// Because the images can be resized in the HTML editor, we must assume that the image is of the exact desired
// dimensions and that stretch is an appropriate mode. Note that the default is to never upscale images.
var state = new Dictionary<string, string> { var state = new Dictionary<string, string> {
{ "Width", width.ToString() }, { "Width", width.ToString() },
{ "Height", height.ToString() }, { "Height", height.ToString() },
@@ -146,7 +151,7 @@ namespace Orchard.MediaProcessing.Filters {
} }
private string GetAttributeValue(string tag, string attributeName) { private string GetAttributeValue(string tag, string attributeName) {
var match = Regex.Match(tag, $@"\b{attributeName}\s*=\s*[""']?([^""'\s>]+)[""']?"); var match = Regex.Match(tag, GetAttributeRegex(attributeName));
return match.Success ? match.Groups[1].Value : null; return match.Success ? match.Groups[1].Value : null;
} }
@@ -156,12 +161,15 @@ namespace Orchard.MediaProcessing.Filters {
} }
private string SetAttributeValue(string tag, string attributeName, string value) { private string SetAttributeValue(string tag, string attributeName, string value) {
if (Regex.IsMatch(tag, $@"\b{attributeName}\s*=\s*[""']?([^""'\s>]+)[""']?")) { var attributeRegex = GetAttributeRegex(attributeName);
return Regex.Replace(tag, $@"\b{attributeName}\s*=\s*[""']?([^""'\s>]+)[""']?", $"{attributeName}=\"{value}\""); if (Regex.IsMatch(tag, attributeRegex)) {
return Regex.Replace(tag, attributeRegex, $"{attributeName}=\"{value}\"");
} }
else { else {
return tag.Insert(tag.Length - 1, $" {attributeName}=\"{value}\""); return tag.Insert(tag.Length - 1, $" {attributeName}=\"{value}\"");
} }
} }
private string GetAttributeRegex(string attributeName) => $@"\b{attributeName}\s*=\s*[""']?([^""'\s>]+)[""']?";
} }
} }