Skip to content

v1/media warnings #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: v1/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ Add the package to an existing Umbraco website from nuget:

Once added, a new Content App will be available alongside your Umbraco pages allowing you to trigger a sustainability report.

## Configuration

Example configuration that can be added for this package.

```
"UmbracoCommunitySustainability": {
"Enabled": true,
"MediaOptimisation": {
"ShowWarnings": true,
"FileTypes": { "Image": "2000000" }
}
}
```

## Contributing

Contributions to this package are most welcome! Please read the [Contributing Guidelines](CONTRIBUTING.md) for how to get involved.
Expand Down
15 changes: 15 additions & 0 deletions docs/README_nuget.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ Add the package to an existing Umbraco website from NuGet:

Once added, a new Content App will be available alongside your Umbraco pages allowing you to trigger a sustainability report.

## Configuration

Example configuration that can be added for this package.

```
"UmbracoCommunitySustainability": {
"Enabled": true,
"MediaOptimisation": {
"ShowWarnings": true,
"FileTypes": { "Image": "2000000" }
}
}
```


## License

Copyright © [Rick Butterfield](https://github.com/rickbutterfield), [Thomas Morris](https://github.com/tcmorris) and other contributors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@
"UpgradeUnattended": true
}
}
},
"UmbracoCommunitySustainability": {
"Enabled": true,
"MediaOptimisation": {
"ShowWarnings": true,
"FileTypes": { "Image": "2000000" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Umbraco.Community.Sustainability.Configuration
{
public class MediaOptimisationConfiguration
{
public bool ShowWarnings { get; set; } = false;

public Dictionary<string, string> FileTypes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Umbraco.Community.Sustainability.Configuration
{
public class SustainabilityConfiguration
{
public const string SectionAlias = "UmbracoCommunitySustainability";

public bool Enabled { get; set; } = true;
public MediaOptimisationConfiguration MediaOptimisation { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Community.Sustainability.Configuration;
using Umbraco.Community.Sustainability.Services;

namespace Umbraco.Community.Sustainability.Notifications
{
public class MediaTreeNodeRenderingNotificationHandler : INotificationHandler<TreeNodesRenderingNotification>
{
private readonly IMediaOptimisationService _mediaOptimisationService;
private readonly SustainabilityConfiguration _configuration;

public MediaTreeNodeRenderingNotificationHandler(
IMediaOptimisationService mediaOptimisationService,
IOptions<SustainabilityConfiguration> configuration)
{
_mediaOptimisationService = mediaOptimisationService;
_configuration = configuration.Value;
}

public void Handle(TreeNodesRenderingNotification notification)
{
if (_configuration.Enabled
&& _configuration.MediaOptimisation.ShowWarnings
&& notification.TreeAlias == Cms.Core.Constants.Trees.Media)
{
foreach (var node in notification.Nodes)
{
int.TryParse(node.Id.ToString(), out int nodeId);

if (_mediaOptimisationService.ShowMediaWarning(nodeId))
{
node.Icon = "icon-alert-alt color-red";
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Community.Sustainability.Configuration;
using Umbraco.Extensions;

namespace Umbraco.Community.Sustainability.Services
{
public interface IMediaOptimisationService
{
bool ShowMediaWarning(int nodeId);
}

public class MediaOptimisationService : IMediaOptimisationService
{
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly SustainabilityConfiguration _configuration;

public MediaOptimisationService(
IUmbracoContextFactory umbracoContextFactory,
IOptions<SustainabilityConfiguration> configuration)
{
_umbracoContextFactory = umbracoContextFactory;
_configuration = configuration.Value;
}

public bool ShowMediaWarning(int nodeId)
{
var mediaItem = GetMediaFile(nodeId);
if (mediaItem == null)
{
return false;
}

var fileTypes = _configuration.MediaOptimisation.FileTypes;
if (!fileTypes.Any(x => x.Key.InvariantEquals(mediaItem.ContentType.Alias)))
{
return false;
}

var imageSize = mediaItem.Value<int>("umbracoBytes");
var acceptedFileSize = fileTypes.FirstOrDefault(x => x.Key.InvariantEquals(mediaItem.ContentType.Alias));

long maxAcceptedFileSize = (long)Convert.ToDouble(acceptedFileSize.Value);

return imageSize > maxAcceptedFileSize;
}

private IPublishedContent? GetMediaFile(int nodeId)
{
using var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();

return umbracoContextReference.UmbracoContext.Media?.GetById(nodeId);
}
}
}
11 changes: 10 additions & 1 deletion src/Umbraco.Community.Sustainability/SustainabilityComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Community.Sustainability.Configuration;
using Umbraco.Community.Sustainability.ContentApps;
using Umbraco.Community.Sustainability.Notifications;
using Umbraco.Community.Sustainability.Sections;
Expand All @@ -22,14 +23,22 @@ public void Compose(IUmbracoBuilder builder)
throw new Exception($"Playwright exited with code {exitCode}");
}

// startup
builder.AddNotificationHandler<UmbracoApplicationStartingNotification, PageMetricsNotificationHandler>();
builder.ManifestFilters().Append<SustainabilityManifestFilter>();
builder.ContentApps().Append<SustainabilityContentApp>();

// ui
builder.ContentApps().Append<SustainabilityContentApp>();
builder.Sections().Append<SustainabilitySection>();

// services
builder.Services.AddScoped<IPageMetricService, PageMetricService>();
builder.Services.AddSingleton<ISustainabilityService, SustainabilityService>();
builder.Services.AddSingleton<IMediaOptimisationService, MediaOptimisationService>();
builder.Services.AddOptions<SustainabilityConfiguration>().Bind(builder.Config.GetSection(SustainabilityConfiguration.SectionAlias));

// notifications
builder.AddNotificationHandler<TreeNodesRenderingNotification, MediaTreeNodeRenderingNotificationHandler>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void Filter(List<PackageManifest> manifests)
{
// List any Stylesheet files
// Urls should start '/App_Plugins/UmbracoCommunitySustainability/' not '/wwwroot/Umbraco.Community.Sustainability/', e.g.
// "/App_Plugins/UmbracoCommunitySustainability/Styles/styles.css"
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</ItemGroup>
</Project>