Skip to content

Commit

Permalink
adding bucket rollback on multi-upload (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciaM1 authored Sep 23, 2024
1 parent b12647e commit 440eae6
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File fi
@WithTransaction
public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEntityList, List<ResourceCreationDto> resourceCreationDtoList) {
List<String> errors = new ArrayList<>();
List<String> uploadedFiles = new ArrayList<>();
return Multi.createFrom().items(resourceEntityList.stream())
.onItem().transformToUniAndConcatenate(resourceEntity -> {
int index = resourceEntityList.indexOf(resourceEntity);
Expand All @@ -169,6 +170,7 @@ public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEnt
errors.add(String.format("%s-Problema di sincronizzazione sulla creazione della risorsa", filename));
return Uni.createFrom().nullItem();
}
uploadedFiles.add(optionalResource.get().getStorageKey());
return Uni.createFrom().item(optionalResource.get());
}));
});
Expand All @@ -179,12 +181,22 @@ public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEnt
});
})
.collect().asList()
.onItem().transform(resourceDTOList -> {
.onItem().transformToUni(resourceDTOList -> {
if (!errors.isEmpty()) {
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errors),
Response.Status.BAD_REQUEST, RESOURCES_CREATION_ERROR);
return deleteResourcesFromStorage(uploadedFiles, errors);
}
return errors; // This will be empty if no errors occurred
return Uni.createFrom().item(errors); // This will be empty if no errors occurred
});
}

public Uni<List<String>> deleteResourcesFromStorage(List<String> storageKeys, List<String> errorMessages){
return Multi.createFrom().items(storageKeys.stream())
.onItem().transformToUniAndConcatenate(uploadedStorageKey -> resourceEntityStorageService.delete(uploadedStorageKey)
.onItem().transform(objectStoreResponse -> String.format("Deleted %s",objectStoreResponse.getStorageKey())))
.collect().asList()
.onItem().transform(deletedKeys -> {
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errorMessages),
Response.Status.BAD_REQUEST, RESOURCES_CREATION_ERROR);
});
}

Expand Down

0 comments on commit 440eae6

Please sign in to comment.