Check for duplicate keys on media if we know it's a fresh entity (#15838)

Co-authored-by: Sven Geusens <sge@umbraco.dk>
This commit is contained in:
Sven Geusens
2024-03-06 08:21:33 +01:00
committed by GitHub
parent da6aa7735a
commit 9e455f9325
5 changed files with 19 additions and 1 deletions

View File

@@ -144,6 +144,7 @@ internal sealed class MediaEditingService
// these are the only result states currently expected from Save
OperationResultType.Success => ContentEditingOperationStatus.Success,
OperationResultType.FailedCancelledByEvent => ContentEditingOperationStatus.CancelledByNotification,
OperationResultType.FailedDuplicateKey => ContentEditingOperationStatus.DuplicateKey,
// for any other state we'll return "unknown" so we know that we need to amend this
_ => ContentEditingOperationStatus.Unknown

View File

@@ -756,6 +756,13 @@ namespace Umbraco.Cms.Core.Services
scope.WriteLock(Constants.Locks.MediaTree);
if (media.HasIdentity == false)
{
if (_entityRepository.Get(media.Key, UmbracoObjectTypes.Media.GetGuid()) is not null)
{
scope.Complete();
return Attempt.Fail<OperationResult?>(
new OperationResult(OperationResultType.FailedDuplicateKey, eventMessages));
}
media.CreatorId = userId;
}

View File

@@ -40,5 +40,10 @@ public enum OperationResultType : byte
/// </summary>
NoOperation = Failed | 6, // TODO: shouldn't it be a success?
/// <summary>
/// The operation could not complete due to duplicate key detection
/// </summary>
FailedDuplicateKey = Failed | 7
// TODO: In the future, we might need to add more operations statuses, potentially like 'FailedByPermissions', etc...
}

View File

@@ -18,5 +18,6 @@ public enum ContentEditingOperationStatus
SortingInvalid,
PropertyValidationError,
InvalidCulture,
Unknown
DuplicateKey,
Unknown,
}