Skip to content

Commit 9e455f9

Browse files
authored
Check for duplicate keys on media if we know it's a fresh entity (#15838)
Co-authored-by: Sven Geusens <[email protected]>
1 parent da6aa77 commit 9e455f9

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

src/Umbraco.Cms.Api.Management/Controllers/Content/ContentControllerBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ protected IActionResult ContentEditingOperationStatusResult(ContentEditingOperat
6464
.WithTitle("Invalid culture")
6565
.WithDetail("One or more of the supplied culture codes did not match the configured languages.")
6666
.Build()),
67+
ContentEditingOperationStatus.DuplicateKey => BadRequest(problemDetailsBuilder
68+
.WithTitle("Invalid Id")
69+
.WithDetail("The supplied id is already in use.")
70+
.Build()),
6771
ContentEditingOperationStatus.Unknown => StatusCode(
6872
StatusCodes.Status500InternalServerError,
6973
problemDetailsBuilder

src/Umbraco.Core/Services/MediaEditingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private ContentEditingOperationStatus Save(IMedia media, int userId)
144144
// these are the only result states currently expected from Save
145145
OperationResultType.Success => ContentEditingOperationStatus.Success,
146146
OperationResultType.FailedCancelledByEvent => ContentEditingOperationStatus.CancelledByNotification,
147+
OperationResultType.FailedDuplicateKey => ContentEditingOperationStatus.DuplicateKey,
147148

148149
// for any other state we'll return "unknown" so we know that we need to amend this
149150
_ => ContentEditingOperationStatus.Unknown

src/Umbraco.Core/Services/MediaService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,13 @@ public bool HasChildren(int id)
756756
scope.WriteLock(Constants.Locks.MediaTree);
757757
if (media.HasIdentity == false)
758758
{
759+
if (_entityRepository.Get(media.Key, UmbracoObjectTypes.Media.GetGuid()) is not null)
760+
{
761+
scope.Complete();
762+
return Attempt.Fail<OperationResult?>(
763+
new OperationResult(OperationResultType.FailedDuplicateKey, eventMessages));
764+
}
765+
759766
media.CreatorId = userId;
760767
}
761768

src/Umbraco.Core/Services/OperationResultType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@ public enum OperationResultType : byte
4040
/// </summary>
4141
NoOperation = Failed | 6, // TODO: shouldn't it be a success?
4242

43+
/// <summary>
44+
/// The operation could not complete due to duplicate key detection
45+
/// </summary>
46+
FailedDuplicateKey = Failed | 7
47+
4348
// TODO: In the future, we might need to add more operations statuses, potentially like 'FailedByPermissions', etc...
4449
}

src/Umbraco.Core/Services/OperationStatus/ContentEditingOperationStatus.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public enum ContentEditingOperationStatus
1818
SortingInvalid,
1919
PropertyValidationError,
2020
InvalidCulture,
21-
Unknown
21+
DuplicateKey,
22+
Unknown,
2223
}

0 commit comments

Comments
 (0)