Skip to content

Commit

Permalink
updated disable API algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleMaiocchiFilippo committed Jun 20, 2024
1 parent ca600a3 commit 16b6475
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.smallrye.mutiny.Uni;
import io.vertx.core.buffer.Buffer;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.enumeration.ObjectStoreStrategyEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import it.gov.pagopa.atmlayer.service.model.model.ObjectStoreResponse;
Expand All @@ -18,11 +17,11 @@ public interface ObjectStoreService {

Uni<ObjectStoreResponse> uploadFile(File file, String path, S3ResourceTypeEnum fileType, String filename);

Uni<ObjectStoreResponse> uploadDisabledFile(String storageKey, S3ResourceTypeEnum fileType, String filename);
Uni<ObjectStoreResponse> uploadDisabledFile(String originalStorageKey, String newStorageKey, S3ResourceTypeEnum fileType, String fileName);

Uni<URL> generatePresignedUrl(String objectKey);

RestMulti<Buffer> download(String key);

Uni<ObjectStoreResponse> delete(ResourceEntity resourceEntity);
Uni<ObjectStoreResponse> delete(String storageKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@

public interface ResourceEntityStorageService {

Uni<ResourceFile> uploadFile(File file, ResourceEntity resourceEntity, String filename, String path, boolean creation);
Uni<ResourceFile> uploadFile(File file, ResourceEntity resourceEntity, String filename, String path, boolean creation);

Uni<ObjectStoreResponse> uploadDisabledFile(ResourceEntity resourceEntity);
Uni<ObjectStoreResponse> uploadDisabledFile(String originalStorageKey, String newStorageKey, S3ResourceTypeEnum resourceType, String fileName);

Uni<URL> generatePresignedUrl(String storageKey);
Uni<URL> generatePresignedUrl(String storageKey);

RestMulti<Buffer> download(String storageKey);
RestMulti<Buffer> download(String storageKey);

String calculateBasePath(S3ResourceTypeEnum s3ResourceTypeEnum);
String calculateBasePath(S3ResourceTypeEnum s3ResourceTypeEnum);

String calculateCompletePath(NoDeployableResourceType resourceType, String relativePath);
String calculateCompletePath(NoDeployableResourceType resourceType, String relativePath);

String calculateStorageKey(NoDeployableResourceType resourceType, String relativePath, String fileName);
String calculateStorageKey(NoDeployableResourceType resourceType, String relativePath, String fileName);

Uni<ResourceFile> saveFile(ResourceEntity resourceEntity, File file, String fileNameWithExtension, String relativePath);
Uni<ObjectStoreResponse> delete(ResourceEntity resourceEntity);
Uni<ResourceFile> saveFile(ResourceEntity resourceEntity, File file, String fileNameWithExtension, String relativePath);

Uni<ObjectStoreResponse> delete(String storageKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ResourceFileService {

Uni<String> getStorageKey(ResourceEntity resourceEntity);

Uni<Void> updateStorageKey(ResourceEntity resourceEntity, String storageKeyDisabledResource);
Uni<ResourceFile> updateStorageKey(ResourceEntity resourceEntity);

Uni<String> getCompletePath(ResourceEntity resourceEntity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.gov.pagopa.atmlayer.service.model.entity.ResourceFile;
import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.UtilityValues;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.model.PageInfo;
Expand Down Expand Up @@ -252,20 +253,27 @@ public Uni<PageInfo<ResourceEntity>> findResourceFiltered(int pageIndex, int pag
}

@Override
@WithTransaction
public Uni<Void> disable(UUID uuid) {
return this.setDisabledResourceEntityAttributes(uuid)
return resourceEntityRepository.findById(uuid)
.onItem()
.transformToUni(resourceEntity -> resourceEntityStorageService.uploadDisabledFile(resourceEntity)
.onItem()
.transformToUni(objectStoredResponse -> resourceFileService.updateStorageKey(resourceEntity, objectStoredResponse.getStorageKey())
.onItem()
.transformToUni(itemToDelete -> resourceEntityStorageService.delete(resourceEntity)
.onItem()
.transformToUni(deletedFile -> Uni.createFrom().voidItem())
)
)
);

.transformToUni(resourceEntity -> {
String originalStorageKey = resourceEntity.getResourceFile().getStorageKey();
String originalFileName = resourceEntity.getResourceFile().getFileName();
S3ResourceTypeEnum originalType = resourceEntity.getResourceFile().getResourceType();
return this.setDisabledResourceEntityAttributes(uuid)
.onItem()
.transformToUni(disabledEntity -> resourceFileService.updateStorageKey(disabledEntity)
.onItem()
.transformToUni(resourceFileUpdated -> resourceEntityStorageService.uploadDisabledFile(originalStorageKey, resourceFileUpdated.getStorageKey(), originalType, originalFileName)
.onItem()
.transformToUni(itemToDelete -> resourceEntityStorageService.delete(originalStorageKey)
.onItem()
.transformToUni(deletedFile -> Uni.createFrom().voidItem())
)
)
);
});
}

@WithTransaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ public Uni<ResourceFile> uploadFile(File file, ResourceEntity resourceEntity, St
}

@Override
public Uni<ObjectStoreResponse> uploadDisabledFile(ResourceEntity resourceEntity) {
public Uni<ObjectStoreResponse> uploadDisabledFile(String originalStorageKey, String newStorageKey, S3ResourceTypeEnum resourceType, String fileName) {
Context context = Vertx.currentContext();
S3ResourceTypeEnum resourceType = convertEnum(resourceEntity.getNoDeployableResourceType());
return objectStoreService.uploadDisabledFile(resourceEntity.getResourceFile().getStorageKey(), resourceType, resourceEntity.getFileName())
return objectStoreService.uploadDisabledFile(originalStorageKey, newStorageKey, resourceType, fileName)
.emitOn(command -> context.runOnContext(x -> command.run()))
.onItem()
.transformToUni(objectStorePutResponse -> Uni.createFrom().item(objectStorePutResponse));
}

@Override
public Uni<ObjectStoreResponse> delete(ResourceEntity resourceEntity) {
return objectStoreService.delete(resourceEntity);
public Uni<ObjectStoreResponse> delete(String storageKey) {
Context context = Vertx.currentContext();
return objectStoreService.delete(storageKey)
.emitOn(command -> context.runOnContext(x -> command.run()))
.onItem()
.transformToUni(objectStoreResponse -> Uni.createFrom().item(objectStoreResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_DOES_NOT_EXIST;
import static it.gov.pagopa.atmlayer.service.model.utils.FileStorageS3Utils.modifyPath;
import static java.lang.String.valueOf;

@ApplicationScoped
Expand Down Expand Up @@ -55,15 +56,17 @@ public Uni<String> getStorageKey(ResourceEntity resourceEntity) {

@Override
@WithTransaction
public Uni<Void> updateStorageKey(ResourceEntity resourceEntity, String storageKeyDisabledResource) {
public Uni<ResourceFile> updateStorageKey(ResourceEntity resourceEntity) {
return resourceFileRepository.findByStorageKey(resourceEntity.getResourceFile().getStorageKey())
.onItem().transformToUni(resourceFileFound -> {
if (resourceFileFound == null) {
throw new AtmLayerException("La risorsa di riferimento non esiste: impossibile aggiornare la chiave di archiviazione", Response.Status.BAD_REQUEST, RESOURCE_DOES_NOT_EXIST);
}
resourceFileFound.setStorageKey(storageKeyDisabledResource);
String newStorageKey = modifyPath(resourceFileFound.getStorageKey());
resourceFileFound.setStorageKey(newStorageKey);
return resourceFileRepository.persist(resourceFileFound)
.replaceWith(Uni.createFrom().voidItem());
.onItem()
.transformToUni(newResourceFile -> Uni.createFrom().item(newResourceFile));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.vertx.core.buffer.Buffer;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType;
import it.gov.pagopa.atmlayer.service.model.enumeration.ObjectStoreStrategyEnum;
Expand Down Expand Up @@ -40,8 +39,6 @@
import java.util.Map;
import java.util.Objects;

import static it.gov.pagopa.atmlayer.service.model.utils.FileStorageS3Utils.modifyPath;

@ApplicationScoped
@Slf4j
public class S3ObjectStoreServiceImpl implements S3ObjectStoreService {
Expand Down Expand Up @@ -90,7 +87,7 @@ public RestMulti<Buffer> download(String key) {
return RestMulti.fromUniResponse(Uni.createFrom()
.completionStage(() -> s3.getObject(fileStorageS3Utils.buildGetRequest(key),
AsyncResponseTransformer.toPublisher())),
response -> Multi.createFrom().safePublisher(AdaptersToFlow.publisher((Publisher<ByteBuffer>) response))
response -> Multi.createFrom().safePublisher(AdaptersToFlow.publisher(response))
.map(this::toBuffer),
response -> Map.of("Content-Disposition", List.of("attachment;filename="), "Content-Type",
List.of(response.response().contentType())));
Expand Down Expand Up @@ -128,9 +125,9 @@ public Uni<ObjectStoreResponse> uploadFile(File file, String path, S3ResourceTyp
});
}

public Uni<ObjectStoreResponse> uploadDisabledFile(String storageKey, S3ResourceTypeEnum fileType, String filename) {
public Uni<ObjectStoreResponse> uploadDisabledFile(String originalStorageKey, String newStorageKey, S3ResourceTypeEnum fileType, String fileName) {

CopyObjectRequest copyObjectRequest = fileStorageS3Utils.buildCopyRequest(storageKey, modifyPath(storageKey), getMimetype(fileType, filename));
CopyObjectRequest copyObjectRequest = fileStorageS3Utils.buildCopyRequest(originalStorageKey, newStorageKey, getMimetype(fileType, fileName));
return Uni.createFrom().future(() -> s3.copyObject(copyObjectRequest))
.onFailure().transform(error -> {
String errorMessage = "Errore nel caricamento del file da disabilitare su S3";
Expand All @@ -143,17 +140,16 @@ public Uni<ObjectStoreResponse> uploadDisabledFile(String storageKey, S3Resource
});
}

public Uni<ObjectStoreResponse> delete(ResourceEntity resourceEntity){

DeleteObjectRequest deleteObjectRequest = fileStorageS3Utils.buildDeleteRequest(resourceEntity.getResourceFile().getStorageKey());
public Uni<ObjectStoreResponse> delete(String storageKey) {
DeleteObjectRequest deleteObjectRequest = fileStorageS3Utils.buildDeleteRequest(storageKey);
return Uni.createFrom().future(() -> s3.deleteObject(deleteObjectRequest))
.onFailure().transform(error -> {
String errorMessage = "Errore nel caricamento del file da disabilitare su S3";
String errorMessage = "Errore nel eliminazione del file su S3";
log.error(errorMessage, error);
return new AtmLayerException(errorMessage, Response.Status.INTERNAL_SERVER_ERROR, AppErrorType.INTERNAL.name());
})
.onItem().transformToUni(res -> {
log.info("success uploading disabled file from s3");
log.info("success deleting file on s3");
return Uni.createFrom().item(ObjectStoreResponse.builder().storageKey(deleteObjectRequest.key()).build());
});

Expand Down

0 comments on commit 16b6475

Please sign in to comment.