Skip to content

Commit

Permalink
DMP-4439: Delete audio blob if detected as a duplicate (#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Edwards-cgi authored Jan 7, 2025
1 parent 981328e commit 3971804
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void addAudioMetadataDuplicate() throws Exception {
assertEquals(0, mediaEntities.size());//shouldn't have any as no audio in that courtroom

assertFalse(Objects.requireNonNull(LogUtil.getMemoryLogger())
.searchLogs("Exact duplicate detected based upon media metadata and checksum.", toLevel(
.searchLogs("Exact duplicate detected based upon media metadata and checksum for media entity ids [2].", toLevel(
Level.INFO_INT)).isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void addAudioMetadataDuplicate() throws Exception {
assertEquals(0, mediaEntities.size());//shouldn't have any as no audio in that courtroom

assertFalse(Objects.requireNonNull(LogUtil.getMemoryLogger())
.searchLogs("Exact duplicate detected based upon media metadata and checksum.", toLevel(
.searchLogs("Exact duplicate detected based upon media metadata and checksum for media entity ids [2].", toLevel(
Level.INFO_INT)).isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import uk.gov.hmcts.darts.common.entity.MediaEntity;
import uk.gov.hmcts.darts.common.entity.ObjectRecordStatusEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.exception.AzureDeleteBlobException;
import uk.gov.hmcts.darts.common.exception.DartsApiException;
import uk.gov.hmcts.darts.common.repository.ExternalLocationTypeRepository;
import uk.gov.hmcts.darts.common.repository.ExternalObjectDirectoryRepository;
Expand Down Expand Up @@ -74,7 +75,8 @@ public void addAudio(MultipartFile audioMultipartFile, AddAudioMetadataRequest a
} catch (IOException e) {
throw new DartsApiException(FAILED_TO_UPLOAD_AUDIO_FILE, "Failed to compute incoming checksum", e);
}
addAudio(incomingChecksum, () -> saveAudioToInbound(audioMultipartFile), addAudioMetadataRequest);
//No need to delete guid on duplicate as the file is never uploaded to the blob store unless the duplicate check has passed in this flow
addAudio(incomingChecksum, () -> saveAudioToInbound(audioMultipartFile), addAudioMetadataRequest, false);
}

@Override
Expand All @@ -85,12 +87,13 @@ public void addAudio(UUID guid, AddAudioMetadataRequest addAudioMetadataRequest)
String.format("Checksum for blob '%s' does not match the one passed in the API request '%s'.",
checksum, addAudioMetadataRequest.getChecksum()));
}
addAudio(checksum, () -> guid, addAudioMetadataRequest);
addAudio(checksum, () -> guid, addAudioMetadataRequest, true);
}

private void addAudio(String incomingChecksum,
Supplier<UUID> externalLocationSupplier,
AddAudioMetadataRequest addAudioMetadataRequest) {
AddAudioMetadataRequest addAudioMetadataRequest,
boolean deleteGuidOnDuplicate) {
log.info("Adding audio using metadata {}", addAudioMetadataRequest.toString());

//remove duplicate cases as they can appear more than once, e.g. if they broke for lunch.
Expand All @@ -100,15 +103,23 @@ private void addAudio(String incomingChecksum,
List<MediaEntity> duplicatesToBeSuperseded = getLatestDuplicateMediaFiles(addAudioMetadataRequest);

List<MediaEntity> duplicatesWithDifferentChecksum = filterForMediaWithMismatchingChecksum(duplicatesToBeSuperseded, incomingChecksum);

if (isNotEmpty(duplicatesToBeSuperseded) && isEmpty(duplicatesWithDifferentChecksum)) {
log.info("Exact duplicate detected based upon media metadata and checksum. Returning 200 with no changes ");
for (MediaEntity entity : duplicatesToBeSuperseded) {
log.info("Duplicate media id {}", entity.getId());
if (deleteGuidOnDuplicate) {
UUID uuid = externalLocationSupplier.get();
try {
dataManagementApi.deleteBlobDataFromInboundContainer(uuid);
} catch (AzureDeleteBlobException e) {
log.error("Failed to delete blob from inbound container with guid: ", uuid, e);
}
}
if (log.isInfoEnabled()) {
log.info("Exact duplicate detected based upon media metadata and checksum for media entity ids {}. Returning 200 with no changes.",
duplicatesToBeSuperseded.stream().map(MediaEntity::getId).toList());
}
return;
}

// upload to the blob store
ObjectRecordStatusEntity objectRecordStatusEntity = objectRecordStatusRepository.getReferenceById(STORED.getId());
UUID externalLocation = externalLocationSupplier.get();

Expand All @@ -117,11 +128,16 @@ private void addAudio(String incomingChecksum,
List<MediaEntity> mediaToSupersede = new ArrayList<>();

// if we have not found any duplicate audio files to process lets add a new one
if (isEmpty(duplicatesWithDifferentChecksum)) {
log.info("No duplicates found. Uploading new file");
} else {
if (isNotEmpty(duplicatesWithDifferentChecksum)) {
mediaToSupersede.addAll(duplicatesWithDifferentChecksum);
log.info("Duplicate audio file has been found with difference in checksum");
if (log.isInfoEnabled()) {
log.info("Duplicate audio file has been found with difference in checksum for guid {} latest checksum {}. But found {}",
externalLocation, incomingChecksum,
duplicatesWithDifferentChecksum
.stream()
.map(mediaEntity -> "Media Id " + mediaEntity.getId().toString() + " with checksum " + mediaEntity.getChecksum())
.toList());
}
}

// version the file upload to the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import uk.gov.hmcts.darts.common.entity.HearingEntity;
import uk.gov.hmcts.darts.common.entity.MediaEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.exception.AzureDeleteBlobException;
import uk.gov.hmcts.darts.common.exception.DartsApiException;
import uk.gov.hmcts.darts.common.helper.MediaLinkedCaseHelper;
import uk.gov.hmcts.darts.common.repository.ExternalLocationTypeRepository;
Expand Down Expand Up @@ -237,7 +238,7 @@ void addAudio_shouldSaveMediaAndEod_whenMetadataOnly() {
}

@Test
void addAudio_shouldDoNothing_whenMetadataOnlyAndAudioIsADuplicate() {
void addAudio_shouldDeleteInboundBlob_whenMetadataOnlyAndAudioIsADuplicate() throws AzureDeleteBlobException {
UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(10);
when(userIdentity.getUserAccount()).thenReturn(userAccount);
Expand Down Expand Up @@ -274,6 +275,7 @@ void addAudio_shouldDoNothing_whenMetadataOnlyAndAudioIsADuplicate() {
verifyNoInteractions(logApi);
verifyNoInteractions(externalObjectDirectoryRepository);
verify(dataManagementApi, times(1)).getChecksum(DatastoreContainerType.INBOUND, externalLocation);
verify(dataManagementApi).deleteBlobDataFromInboundContainer(externalLocation);
verifyNoMoreInteractions(dataManagementApi);
}

Expand Down

0 comments on commit 3971804

Please sign in to comment.