Skip to content

Commit

Permalink
Code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
BenedekFarkas committed Nov 14, 2024
1 parent dcde93a commit 2f8c748
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
9 changes: 1 addition & 8 deletions src/Orchard.Web/Modules/Orchard.MediaProcessing/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Orchard.MediaProcessing {
namespace Orchard.MediaProcessing {
public static class Features {

public const string OrchardMediaProcessingHtmlFilter = "Orchard.MediaProcessingHtmlFilter";

}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HtmlAgilityPack;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using Orchard.FileSystems.Media;
using Orchard.Forms.Services;
using Orchard.Logging;
using Orchard.MediaProcessing.Models;
using Orchard.MediaProcessing.Services;
using Orchard.Services;
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

namespace Orchard.MediaProcessing.Filters {
/// <summary>
/// Resizes any images in HTML provided by parts that support IHtmlFilter and sets an alt text if not already supplied
/// Resizes any images in HTML provided by parts that support IHtmlFilter and sets an alt text if not already supplied.
/// </summary>
[OrchardFeature(Features.OrchardMediaProcessingHtmlFilter)]
public class MediaProcessingHtmlFilter : IHtmlFilter {

private readonly IWorkContextAccessor _wca;
private readonly IImageProcessingFileNameProvider _fileNameProvider;
private readonly IImageProfileManager _profileManager;
private readonly IStorageProvider _storageProvider;

private MediaHtmlFilterSettingsPart _settingsPart;
private Dictionary<string, string> _validExtensions = new Dictionary<string, string> {
{ ".jpeg", "jpg" }, //For example: .jpeg supports commpression (quality), format to 'jpg'
{ ".jpg", "jpg" },
{ ".png", null }};

public MediaProcessingHtmlFilter(IWorkContextAccessor wca, IImageProcessingFileNameProvider fileNameProvider, IImageProfileManager profileManager, IStorageProvider storageProvider) {
_fileNameProvider = fileNameProvider;
public MediaProcessingHtmlFilter(IWorkContextAccessor wca, IImageProfileManager profileManager) {
_profileManager = profileManager;
_storageProvider = storageProvider;
_wca = wca;

Logger = NullLogger.Instance;
}

public ILogger Logger { get; set; }

public MediaHtmlFilterSettingsPart Settings { get {
public MediaHtmlFilterSettingsPart Settings {
get {
if (_settingsPart == null) {
_settingsPart = _wca.GetContext().CurrentSite.As<MediaHtmlFilterSettingsPart>();
}
Expand All @@ -52,12 +49,14 @@ public string ProcessContent(string text, HtmlFilterContext context) {
if (!string.IsNullOrEmpty(text) && context.Flavor == "html") {
var doc = new HtmlDocument();
doc.LoadHtml(text);

foreach (var node in doc.DocumentNode.DescendantsAndSelf("img")) {
ProcessImageContent(node);
if (Settings.PopulateAlt) {
ProcessImageAltContent(node);
}
}

return doc.DocumentNode.OuterHtml;
}
else {
Expand All @@ -76,9 +75,12 @@ private void ProcessImageContent(HtmlNode node) {
var width = GetAttributeValueInt(node, "width");
var height = GetAttributeValueInt(node, "height");

if (width > 0 && height > 0 && !string.IsNullOrEmpty(src) && !src.Contains("_Profiles") && _validExtensions.ContainsKey(ext)) {
if (width > 0 && height > 0
&& !string.IsNullOrEmpty(src)
&& !src.Contains("_Profiles")
&& _validExtensions.ContainsKey(ext)) {
try {
//If img has a width, height, not already in _Profiles and a valid ext, process the image.
// If img has a width, height, not already in _Profiles and a valid ext, process the image.
node.Attributes["src"].Value = TryGetImageProfilePath(src, ext, width, height);
}
catch (Exception ex) {
Expand All @@ -88,18 +90,24 @@ private void ProcessImageContent(HtmlNode node) {
}

private string TryGetImageProfilePath(string src, string ext, int width, int height) {

var filters = new List<FilterRecord>();
filters.Add(CreateResizeFilter(width * Settings.DensityThreshold, height * Settings.DensityThreshold)); // Factor in a min height and width with respect to higher pixel density devices.
var filters = new List<FilterRecord> {
// Factor in a min height and width with respect to higher pixel density devices.
CreateResizeFilter(width * Settings.DensityThreshold, height * Settings.DensityThreshold)
};

// If the ext supports compression, also set the quality.
if (_validExtensions[ext] != null && Settings.Quality < 100) {
filters.Add(CreateFormatFilter(Settings.Quality, _validExtensions[ext]));
}

var profileName = string.Format("Transform_Resize_w_{0}_h_{1}_m_Stretch_a_MiddleCenter_c_{2}_d_@{3}x", width, height, Settings.Quality, Settings.DensityThreshold);
return _profileManager.GetImageProfileUrl(src, profileName, null, filters.ToArray());
var profileName = string.Format(
"Transform_Resize_w_{0}_h_{1}_m_Stretch_a_MiddleCenter_c_{2}_d_@{3}x",
width,
height,
Settings.Quality,
Settings.DensityThreshold);

return _profileManager.GetImageProfileUrl(src, profileName, null, filters.ToArray());
}

private FilterRecord CreateResizeFilter(int width, int height) {
Expand Down Expand Up @@ -137,20 +145,17 @@ private FilterRecord CreateFormatFilter(int quality, string format) {
private void ProcessImageAltContent(HtmlNode node) {
var src = GetAttributeValue(node, "src");
var alt = GetAttributeValue(node, "alt");

if (string.IsNullOrEmpty(alt) && !string.IsNullOrEmpty(src)) {
var text = Path.GetFileNameWithoutExtension(src).Replace("-", " ").Replace("_", " ");
SetAttributeValue(node, "alt", text);
}
}

private string GetAttributeValue(HtmlNode node, string name) {
return node.Attributes[name] == null ? null : node.Attributes[name].Value;
}
private string GetAttributeValue(HtmlNode node, string name) => node.Attributes[name]?.Value;

private int GetAttributeValueInt(HtmlNode node, string name) {
int val = 0;
return node.Attributes[name] == null ? 0 : int.TryParse(node.Attributes[name].Value, out val) ? val : 0;
}
private int GetAttributeValueInt(HtmlNode node, string name) =>
node.Attributes[name] == null ? 0 : int.TryParse(node.Attributes[name].Value, out int val) ? val : 0;

private void SetAttributeValue(HtmlNode node, string name, string value) {
if (node.Attributes.Contains(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ public MediaHtmlFilterSettingsPartHandler() {
T = NullLocalizer.Instance;

Filters.Add(new ActivatingFilter<MediaHtmlFilterSettingsPart>("Site"));
Filters.Add(new TemplateFilterForPart<MediaHtmlFilterSettingsPart>("MediaHtmlFilterSettings", "Parts.MediaProcessing.MediaHtmlFilterSettings", "media"));
Filters.Add(new TemplateFilterForPart<MediaHtmlFilterSettingsPart>(
"MediaHtmlFilterSettings",
"Parts.MediaProcessing.MediaHtmlFilterSettings",
"media"));
}

public Localizer T { get; set; }

protected override void GetItemMetadata(GetContentItemMetadataContext context) {
if (context.ContentItem.ContentType != "Site")
return;
if (context.ContentItem.ContentType != "Site") return;

base.GetItemMetadata(context);
context.Metadata.EditorGroupInfo.Add(new GroupInfo(T("Media")));
}
Expand Down

0 comments on commit 2f8c748

Please sign in to comment.