Adds icons from App_Plugins/**/Icons/*.svg (#8884)

(cherry picked from commit 3b551bdc5d)
This commit is contained in:
Søren Kottal
2020-10-06 15:06:03 +02:00
committed by Sebastiaan Janssen
parent 879d54b525
commit d7bf980f12
2 changed files with 54 additions and 15 deletions

View File

@@ -9,13 +9,13 @@ namespace Umbraco.Core.Services
/// Gets an IconModel containing the icon name and SvgString according to an icon name found at the global icons path
/// </summary>
/// <param name="iconName"></param>
/// <returns></returns>
/// <returns><see cref="IconModel"/></returns>
IconModel GetIcon(string iconName);
/// <summary>
/// Gets a list of all svg icons found at at the global icons path.
/// </summary>
/// <returns></returns>
/// <returns>A list of <see cref="IconModel"/></returns>
IList<IconModel> GetAllIcons();
}
}

View File

@@ -19,13 +19,11 @@ namespace Umbraco.Web.Services
_globalSettings = globalSettings;
}
/// <inheritdoc />
public IList<IconModel> GetAllIcons()
{
var icons = new List<IconModel>();
var directory = new DirectoryInfo(IOHelper.MapPath($"{_globalSettings.IconsPath}/"));
var iconNames = directory.GetFiles("*.svg");
var iconNames = GetAllIconNames();
iconNames.OrderBy(f => f.Name).ToList().ForEach(iconInfo =>
{
@@ -45,15 +43,15 @@ namespace Umbraco.Web.Services
{
return string.IsNullOrWhiteSpace(iconName)
? null
: CreateIconModel(iconName.StripFileExtension(), IOHelper.MapPath($"{_globalSettings.IconsPath}/{iconName}.svg"));
: CreateIconModel(iconName.StripFileExtension());
}
/// <summary>
/// Gets an IconModel using values from a FileInfo model
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
private IconModel GetIcon(FileInfo fileInfo)
/// <returns><see cref="IconModel"/></returns>
private IconModel GetIcon(FileSystemInfo fileInfo)
{
return fileInfo == null || string.IsNullOrWhiteSpace(fileInfo.Name)
? null
@@ -64,17 +62,37 @@ namespace Umbraco.Web.Services
/// Gets an IconModel containing the icon name and SvgString
/// </summary>
/// <param name="iconName"></param>
/// <param name="iconPath"></param>
/// <returns></returns>
private IconModel CreateIconModel(string iconName, string iconPath)
/// <returns><see cref="IconModel"/></returns>
private IconModel CreateIconModel(string iconName)
{
var sanitizer = new HtmlSanitizer();
sanitizer.AllowedAttributes.UnionWith(Constants.SvgSanitizer.Attributes);
sanitizer.AllowedCssProperties.UnionWith(Constants.SvgSanitizer.Attributes);
sanitizer.AllowedTags.UnionWith(Constants.SvgSanitizer.Tags);
if (string.IsNullOrWhiteSpace(iconName))
return null;
var iconNames = GetAllIconNames();
var iconPath = iconNames.FirstOrDefault(x => x.Name.InvariantEquals($"{iconName}.svg"))?.FullName;
return iconPath == null
? null
: CreateIconModel(iconName, iconPath);
}
/// <summary>
/// Gets an IconModel containing the icon name and SvgString
/// </summary>
/// <param name="iconName"></param>
/// <param name="iconPath"></param>
/// <returns><see cref="IconModel"/></returns>
private static IconModel CreateIconModel(string iconName, string iconPath)
{
if (string.IsNullOrWhiteSpace(iconPath))
return null;
try
{
var sanitizer = new HtmlSanitizer();
sanitizer.AllowedAttributes.UnionWith(Constants.SvgSanitizer.Attributes);
sanitizer.AllowedCssProperties.UnionWith(Constants.SvgSanitizer.Attributes);
sanitizer.AllowedTags.UnionWith(Constants.SvgSanitizer.Tags);
var svgContent = System.IO.File.ReadAllText(iconPath);
var sanitizedString = sanitizer.Sanitize(svgContent);
@@ -91,5 +109,26 @@ namespace Umbraco.Web.Services
return null;
}
}
private IEnumerable<FileInfo> GetAllIconNames()
{
// add icons from plugins
var appPlugins = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.AppPlugins));
var pluginIcons = appPlugins.Exists == false
? new List<FileInfo>()
: appPlugins.GetDirectories()
// Find all directories in App_Plugins that are named "Icons" and get a list of SVGs from them
.SelectMany(x => x.GetDirectories("Icons", SearchOption.AllDirectories))
.SelectMany(x => x.GetFiles("*.svg", SearchOption.TopDirectoryOnly));
// add icons from IconsPath if not already added from plugins
var directory = new DirectoryInfo(IOHelper.MapPath($"{_globalSettings.IconsPath}/"));
var iconNames = directory.GetFiles("*.svg")
.Where(x => pluginIcons.Any(i => i.Name == x.Name) == false);
iconNames = iconNames.Concat(pluginIcons).ToList();
return iconNames;
}
}
}