Skip to content
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

Remove RTE embedded image handling + clean up file upload configuration #16025

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/Umbraco.Core/PropertyEditors/FileUploadConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace Umbraco.Cms.Core.PropertyEditors;
/// <summary>
/// Represents the configuration for the file upload address value editor.
/// </summary>
public class FileUploadConfiguration : IFileExtensionsConfig
public class FileUploadConfiguration
{
[ConfigurationField("fileExtensions")]
public List<FileExtensionConfigItem> FileExtensions { get; set; } = new();
public IEnumerable<string> FileExtensions { get; set; } = Enumerable.Empty<string>();
}
9 changes: 0 additions & 9 deletions src/Umbraco.Core/PropertyEditors/IFileExtensionConfig.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ private bool IsAllowedInDataTypeConfiguration(string extension, object? dataType
{
// If FileExtensions is empty and no allowed extensions have been specified, we allow everything.
// If there are any extensions specified, we need to check that the uploaded extension is one of them.
return fileUploadConfiguration.FileExtensions.IsCollectionEmpty() ||
fileUploadConfiguration.FileExtensions.Any(x => x.Value?.InvariantEquals(extension) ?? false);
return fileUploadConfiguration.FileExtensions.Any() is false ||
fileUploadConfiguration.FileExtensions.Contains(extension);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

✅ Getting better: Missing Arguments Abstractions

The average number of function arguments decreases from 6.14 to 5.71, threshold = 4.00. The functions in this file have too many arguments, indicating a lack of encapsulation or too many responsibilities in the same functions. Avoid adding more.
// See LICENSE for more details.

using HtmlAgilityPack;
Expand Down Expand Up @@ -34,7 +34,6 @@
private readonly IMediaImportService _mediaImportService;
private readonly IImageUrlGenerator _imageUrlGenerator;
private readonly IUserService _userService;
private readonly ContentSettings _contentSettings;

[Obsolete("Please use the non-obsolete constructor. Will be removed in V16.")]
public RichTextEditorPastedImages(
Expand Down Expand Up @@ -81,7 +80,7 @@
IMediaImportService mediaImportService,
IImageUrlGenerator imageUrlGenerator,
IOptions<ContentSettings> contentSettings)
: this(umbracoContextAccessor, publishedUrlProvider, temporaryFileService, scopeProvider, mediaImportService, imageUrlGenerator, contentSettings)
: this(umbracoContextAccessor, publishedUrlProvider, temporaryFileService, scopeProvider, mediaImportService, imageUrlGenerator)
{
}

Expand All @@ -91,37 +90,21 @@
ITemporaryFileService temporaryFileService,
IScopeProvider scopeProvider,
IMediaImportService mediaImportService,
IImageUrlGenerator imageUrlGenerator,
IOptions<ContentSettings> contentSettings)
IImageUrlGenerator imageUrlGenerator)
{
_umbracoContextAccessor =
umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
_publishedUrlProvider = publishedUrlProvider;
_temporaryFileService = temporaryFileService;
_scopeProvider = scopeProvider;
_mediaImportService = mediaImportService;
_imageUrlGenerator = imageUrlGenerator;

Check notice on line 101 in src/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImages.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

✅ Getting better: Constructor Over-Injection

RichTextEditorPastedImages decreases from 7 to 6 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
_contentSettings = contentSettings.Value;

// this obviously is not correct. however, we only use IUserService in an obsolete method,
// so this is better than having even more obsolete constructors for V16
_userService = StaticServiceProvider.Instance.GetRequiredService<IUserService>();
}

/// <summary>
/// Used by the RTE (and grid RTE) for converting inline base64 images to Media items.
/// </summary>
/// <param name="html">HTML from the Rich Text Editor property editor.</param>
/// <param name="mediaParentFolder"></param>
/// <param name="userId"></param>
/// <returns>Formatted HTML.</returns>
/// <exception cref="NotSupportedException">Thrown if image extension is not allowed</exception>
internal string FindAndPersistEmbeddedImages(string html, Guid mediaParentFolder, Guid userId)
{
// FIXME: the FindAndPersistEmbeddedImages implementation from #14546 must be ported to V14 and added here
return html;
}

[Obsolete($"Please use {nameof(FindAndPersistPastedTempImagesAsync)}. Will be removed in V16.")]
public string FindAndPersistPastedTempImages(string html, Guid mediaParentFolder, int userId, IImageUrlGenerator imageUrlGenerator)
=> FindAndPersistPastedTempImages(html, mediaParentFolder, userId);
Expand Down Expand Up @@ -179,7 +162,7 @@
{
using Stream fileStream = temporaryFile.OpenReadStream();
Guid? parentFolderKey = mediaParentFolder == Guid.Empty ? Constants.System.RootKey : mediaParentFolder;
IMedia mediaFile = await _mediaImportService.ImportAsync(temporaryFile.FileName, fileStream, parentFolderKey, Constants.Conventions.MediaTypes.Image, userKey);
IMedia mediaFile = await _mediaImportService.ImportAsync(temporaryFile.FileName, fileStream, parentFolderKey, MediaTypeAlias(temporaryFile.FileName), userKey);
udi = mediaFile.GetUdi();
}
else
Expand Down Expand Up @@ -230,4 +213,9 @@

return htmlDoc.DocumentNode.OuterHtml;
}

private string MediaTypeAlias(string fileName)
=> fileName.InvariantEndsWith(".svg")
? Constants.Conventions.MediaTypes.VectorGraphicsAlias
: Constants.Conventions.MediaTypes.Image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,8 @@ public override IEnumerable<ITag> GetTags(object? value, object? dataTypeConfigu
return null;
}

var parseAndSaveBase64Images = _pastedImages.FindAndPersistEmbeddedImages(
richTextEditorValue.Markup, mediaParentId, userKey);
var parseAndSavedTempImages = _pastedImages
.FindAndPersistPastedTempImagesAsync(parseAndSaveBase64Images, mediaParentId, userKey)
.FindAndPersistPastedTempImagesAsync(richTextEditorValue.Markup, mediaParentId, userKey)
.GetAwaiter()
.GetResult();
var editorValueWithMediaUrlsRemoved = _imageSourceParser.RemoveImageSources(parseAndSavedTempImages);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System.Text;
using HtmlAgilityPack;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Models.TemporaryFile;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;

namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.PropertyEditors;

[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class RichTextEditorPastedImagesTests : UmbracoIntegrationTest
{
private static readonly Guid GifFileKey = Guid.Parse("E625C7FA-6CA7-4A01-92CD-FB5C6F89973D");

private static readonly Guid SvgFileKey = Guid.Parse("0E3A7DFE-DF09-4C3B-881C-E1B815A4502F");

protected override void ConfigureTestServices(IServiceCollection services)
{
// mock out the temporary file service so we don't have to read/write files from/to disk
var temporaryFileServiceMock = new Mock<ITemporaryFileService>();
temporaryFileServiceMock
.Setup(t => t.GetAsync(GifFileKey))
.Returns(Task.FromResult(new TemporaryFileModel
{
AvailableUntil = DateTime.UtcNow.AddDays(1),
FileName = "the-pixel.gif",
Key = GifFileKey,
OpenReadStream = () => new MemoryStream(Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"))
}));
temporaryFileServiceMock
.Setup(t => t.GetAsync(SvgFileKey))
.Returns(Task.FromResult(new TemporaryFileModel
{
AvailableUntil = DateTime.UtcNow.AddDays(1),
FileName = "the-vector.svg",
Key = SvgFileKey,
OpenReadStream = () => new MemoryStream(Encoding.UTF8.GetBytes(@"<svg viewBox=""0 0 70 74"" fill=""none"" xmlns=""http://www.w3.org/2000/svg""><rect width=""100%"" height=""100%"" fill=""black""/></svg>"))
}));

services.AddUnique<ITemporaryFileService>(temporaryFileServiceMock.Object);

// the integration tests do not really play nice with published content, so we need to mock a fair bit in order to generate media URLs
var publishedMediaTypeMock = new Mock<IPublishedContentType>();
publishedMediaTypeMock.SetupGet(c => c.ItemType).Returns(PublishedItemType.Media);

var publishedMediaMock = new Mock<IPublishedContent>();
publishedMediaMock.SetupGet(m => m.ContentType).Returns(publishedMediaTypeMock.Object);

var publishedMediaCacheMock = new Mock<IPublishedMediaCache>();
publishedMediaCacheMock.Setup(mc => mc.GetById(It.IsAny<Guid>())).Returns(publishedMediaMock.Object);

var umbracoContextMock = new Mock<IUmbracoContext>();
umbracoContextMock.SetupGet(c => c.Media).Returns(publishedMediaCacheMock.Object);
var umbracoContext = umbracoContextMock.Object;

var umbracoContextAccessor = new Mock<IUmbracoContextAccessor>();
umbracoContextAccessor.Setup(ca => ca.TryGetUmbracoContext(out umbracoContext)).Returns(true);

services.AddUnique<IUmbracoContextAccessor>(umbracoContextAccessor.Object);

var publishedUrlProviderMock = new Mock<IPublishedUrlProvider>();
publishedUrlProviderMock
.Setup(pu => pu.GetMediaUrl(It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string?>(), It.IsAny<string>(), It.IsAny<Uri?>()))
.Returns("the-media-url");

services.AddUnique<IPublishedUrlProvider>(publishedUrlProviderMock.Object);
}

[Test]
public async Task Can_Handle_Temp_Gif_Image()
{
var html = $"<p><img data-tmpimg=\"{GifFileKey:D}\"></p>";
var subject = Services.GetRequiredService<RichTextEditorPastedImages>();

var result = await subject.FindAndPersistPastedTempImagesAsync(html, Guid.Empty, Constants.Security.SuperUserKey);
AssertContainsMedia(result, Constants.Conventions.MediaTypes.Image);
}

Check warning on line 87 in tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/PropertyEditors/RichTextEditorPastedImagesTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

❌ New issue: Code Duplication

The module contains 4 functions with similar structure: Can_Handle_Multiple_Temp_Images,Can_Handle_Temp_Gif_Image,Can_Handle_Temp_Svg_Image,Does_Not_Create_Duplicates_Of_The_Same_Temp_Image. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

[Test]
public async Task Can_Handle_Temp_Svg_Image()
{
var html = $"<p><img data-tmpimg=\"{SvgFileKey:D}\"></p>";
var subject = Services.GetRequiredService<RichTextEditorPastedImages>();

var result = await subject.FindAndPersistPastedTempImagesAsync(html, Guid.Empty, Constants.Security.SuperUserKey);
AssertContainsMedia(result, Constants.Conventions.MediaTypes.VectorGraphicsAlias);
}

[Test]
public async Task Ignores_Non_Existing_Temp_Image()
{
var key = Guid.NewGuid();
var html = $"<p><img data-tmpimg=\"{key:D}\"></p>";
var subject = Services.GetRequiredService<RichTextEditorPastedImages>();

var result = await subject.FindAndPersistPastedTempImagesAsync(html, Guid.Empty, Constants.Security.SuperUserKey);
Assert.AreEqual(html, result);
}

[Test]
public async Task Can_Handle_Multiple_Temp_Images()
{
var html = $"<p><img data-tmpimg=\"{SvgFileKey:D}\"></p><p><img data-tmpimg=\"{GifFileKey:D}\"></p>";
var subject = Services.GetRequiredService<RichTextEditorPastedImages>();

var result = await subject.FindAndPersistPastedTempImagesAsync(html, Guid.Empty, Constants.Security.SuperUserKey);

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(result);
var imageNodes = htmlDoc.DocumentNode.SelectNodes("//img");
Assert.AreEqual(2, imageNodes.Count);

var udis = imageNodes.Select(imageNode => UdiParser.Parse(imageNode.Attributes["data-udi"].Value)).OfType<GuidUdi>().ToArray();
Assert.AreEqual(2, udis.Length);
Assert.AreNotEqual(udis.First().Guid, udis.Last().Guid);

var mediaService = Services.GetRequiredService<IMediaService>();
Assert.Multiple(() =>
{
Assert.IsNotNull(mediaService.GetById(udis.First().Guid));
Assert.IsNotNull(mediaService.GetById(udis.Last().Guid));
});
}

[Test]
public async Task Does_Not_Create_Duplicates_Of_The_Same_Temp_Image()
{
var html = $"<p><img data-tmpimg=\"{GifFileKey:D}\"></p><p><img data-tmpimg=\"{GifFileKey:D}\"></p>";
var subject = Services.GetRequiredService<RichTextEditorPastedImages>();

var result = await subject.FindAndPersistPastedTempImagesAsync(html, Guid.Empty, Constants.Security.SuperUserKey);

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(result);
var imageNodes = htmlDoc.DocumentNode.SelectNodes("//img");
Assert.AreEqual(2, imageNodes.Count);

var udis = imageNodes.Select(imageNode => UdiParser.Parse(imageNode.Attributes["data-udi"].Value)).OfType<GuidUdi>().ToArray();
Assert.AreEqual(2, udis.Length);
Assert.AreEqual(udis.First().Guid, udis.Last().Guid);

var mediaService = Services.GetRequiredService<IMediaService>();
Assert.IsNotNull(mediaService.GetById(udis.First().Guid));
}

private void AssertContainsMedia(string result, string expectedMediaTypeAlias)
{
Assert.IsFalse(result.Contains("data-tmpimg"));

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(result);
var imageNode = htmlDoc.DocumentNode.SelectNodes("//img").FirstOrDefault();
Assert.IsNotNull(imageNode);

Assert.IsTrue(imageNode.Attributes.Contains("src"));
Assert.AreEqual("the-media-url", imageNode.Attributes["src"].Value);

Assert.IsTrue(imageNode.Attributes.Contains("data-udi"));
Assert.IsTrue(UdiParser.TryParse(imageNode.Attributes["data-udi"].Value, out GuidUdi udi));
Assert.AreEqual(Constants.UdiEntityType.Media, udi.EntityType);

var media = Services.GetRequiredService<IMediaService>().GetById(udi.Guid);
Assert.IsNotNull(media);
Assert.AreEqual(expectedMediaTypeAlias, media.ContentType.Alias);
}
}
Loading