diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Constants.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Constants.cs
index dc86a7540f1..92fc05002f8 100644
--- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Constants.cs
+++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Constants.cs
@@ -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";
-
}
}
\ No newline at end of file
diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Filters/MediaProcessingHtmlFilter.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Filters/MediaProcessingHtmlFilter.cs
index 8a31184f8e2..e776a2884ee 100644
--- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Filters/MediaProcessingHtmlFilter.cs
+++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Filters/MediaProcessingHtmlFilter.cs
@@ -1,28 +1,25 @@
-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 {
///
- /// 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.
///
[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 _validExtensions = new Dictionary {
@@ -30,17 +27,17 @@ public class MediaProcessingHtmlFilter : IHtmlFilter {
{ ".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();
}
@@ -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 {
@@ -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) {
@@ -88,18 +90,24 @@ private void ProcessImageContent(HtmlNode node) {
}
private string TryGetImageProfilePath(string src, string ext, int width, int height) {
-
- var filters = new List();
- 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 {
+ // 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) {
@@ -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)) {
diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Handlers/MediaHtmlFilterSettingsPartHandler.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Handlers/MediaHtmlFilterSettingsPartHandler.cs
index 34276bc5be9..95ab97eeda4 100644
--- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Handlers/MediaHtmlFilterSettingsPartHandler.cs
+++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Handlers/MediaHtmlFilterSettingsPartHandler.cs
@@ -12,14 +12,17 @@ public MediaHtmlFilterSettingsPartHandler() {
T = NullLocalizer.Instance;
Filters.Add(new ActivatingFilter("Site"));
- Filters.Add(new TemplateFilterForPart("MediaHtmlFilterSettings", "Parts.MediaProcessing.MediaHtmlFilterSettings", "media"));
+ Filters.Add(new TemplateFilterForPart(
+ "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")));
}