Files
Umbraco-CMS/src/Umbraco.Infrastructure/PropertyEditors/FileUploadValueParser.cs
Peter 14063a0b89 Add support for file upload property editor within the block list and grid (#18976)
* Fix for https://github.com/umbraco/Umbraco-CMS/issues/18872

* Parsing added for current value

* Build fix.

* Cyclomatic complexity fix

* Resolved breaking change.

* Pass content key.

* Simplified collections.

* Added unit tests to verify behaviour.

* Allow file upload on block list.

* Added unit test verifying added property.

* Added unit test verifying removed property.

* Restored null return for null value fixing failing integration tests.

* Logic has been updated according edge cases

* Logic to copy files from block list items has been added.

* Logic to delete files from block list items on content deletion has been added

* Test fix.

* Refactoring.

* WIP: Resolved breaking changes, minor refactoring.

* Consistently return null over empty, resolving failure in integration test.

* Removed unnecessary code nesting.

* Handle distinct paths.

* Handles clean up of files added via file upload in rich text blocks on delete of the content.

* Update src/Umbraco.Infrastructure/PropertyEditors/FileUploadPropertyEditor.cs

Co-authored-by: Sven Geusens <geusens@gmail.com>

* Fixed build of integration tests project.

* Handled delete of file uploads when deleting a block from an RTE using a file upload property.

* Refactored ensure of property type property populated on rich text values to a common helper extension method.

* Fixed integration tests build.

* Handle create of new file from file upload block in an RTE when the document is copied.

* Fixed failing integration tests.

* Refactored notification handlers relating to file uploads into separate classes.

* Handle nested rich text editor block with file upload when copying content.

* Handle nested rich text editor block with file upload when deleting content.

* Minor refactor.

* Integration test compatibility supressions.

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
Co-authored-by: Sven Geusens <geusens@gmail.com>
2025-06-30 13:21:10 +02:00

46 lines
1.5 KiB
C#

using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
namespace Umbraco.Cms.Infrastructure.PropertyEditors;
/// <summary>
/// Handles the parsing of raw values to <see cref="FileUploadValue"/> objects.
/// </summary>
internal sealed class FileUploadValueParser
{
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="FileUploadValueParser"/> class.
/// </summary>
/// <param name="jsonSerializer"></param>
public FileUploadValueParser(IJsonSerializer jsonSerializer) => _jsonSerializer = jsonSerializer;
/// <summary>
/// Parses raw value to a <see cref="FileUploadValue"/>.
/// </summary>
/// <param name="editorValue">The editor value.</param>
/// <returns><a cref="FileUploadValue"></a> value</returns>
/// <exception cref="ArgumentException"></exception>
public FileUploadValue? Parse(object? editorValue)
{
if (editorValue is null)
{
return null;
}
if (editorValue is string sourceString && sourceString.DetectIsJson() is false)
{
return new FileUploadValue()
{
Src = sourceString,
};
}
return _jsonSerializer.TryDeserialize(editorValue, out FileUploadValue? modelValue)
? modelValue
: throw new ArgumentException($"Could not parse editor value to a {nameof(FileUploadValue)} object.");
}
}