Skip to content

Commit

Permalink
create resource with relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciaM1 committed Nov 9, 2023
1 parent ef25556 commit 6587978
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_EXISTING_REFERENCED_ENTITY;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.ID_NOT_FOUND;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_UPGRADABLE;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_UPLOADABLE;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_VALID_FILE;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorType.NOT_VALID_REFERENCED_ENTITY;

Expand Down Expand Up @@ -46,7 +47,8 @@ public enum AppErrorCodeEnum {
WORKFLOW_RESOURCE_FILE_WITH_SAME_CONTENT_ALREADY_EXIST("ATMLM_4000021","A Workflow Resource file with the same content already exists", CONSTRAINT_VIOLATION),
WORKFLOW_RESOURCE_FILE_WITH_SAME_CAMUNDA_DEFINITION_KEY_ALREADY_EXISTS("ATMLM_4000022","A Workflow Resource file with the same Camunda definition key already exists", CONSTRAINT_VIOLATION),
WORKFLOW_FILE_DOES_NOT_EXIST("ATMLM_4000023", "The referenced Workflow Resource file does not exist", NOT_EXISTING_REFERENCED_ENTITY),
WORKFLOW_RESOURCE_CANNOT_BE_DELETED_FOR_STATUS("ATMLM_4000024", "The referenced Workflow Resource file can not be deleted in the actual state", NOT_DELETABLE);
WORKFLOW_RESOURCE_CANNOT_BE_DELETED_FOR_STATUS("ATMLM_4000024", "The referenced Workflow Resource file can not be deleted in the actual state", NOT_DELETABLE),
RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED("ATMLM_4000025", "A resource with same file name and path already exists", NOT_UPLOADABLE);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public enum AppErrorType {
NOT_VALID_FILE,
INVALID_DEPLOY,
ID_NOT_FOUND,
INVALID_ARGUMENT
INVALID_ARGUMENT,
NOT_UPLOADABLE
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package it.gov.pagopa.atmlayer.service.model.repository;

import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceFile;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.UUID;

@ApplicationScoped
public class ResourceFileRepository implements PanacheRepositoryBase<ResourceFile, UUID> {

public Uni<ResourceFile> findByStorageKey(String key){
return find(
"select resource from ResourceFile resource where resource.storageKey = :key",
Parameters.with("key", key)).firstResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceFile;

import java.util.Optional;

public interface ResourceFileService {

Uni<ResourceFile> save(ResourceFile resourceFile);

Uni<Optional<ResourceFile>> findByStorageKey(String storageKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import io.vertx.core.buffer.Buffer;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
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.ObjectStoreStrategyEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.ResourceTypeEnum;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.model.ObjectStorePutResponse;
import it.gov.pagopa.atmlayer.service.model.properties.ObjectStoreProperties;
import it.gov.pagopa.atmlayer.service.model.service.ObjectStoreService;
Expand All @@ -17,6 +19,7 @@
import it.gov.pagopa.atmlayer.service.model.strategy.ObjectStoreStrategy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
Expand Down Expand Up @@ -47,20 +50,31 @@ public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,
}

@Override
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename, String specificPath) {
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename, String relativePath) {
ResourceTypeEnum resourceType = resourceEntity.getResourceTypeEnum();
String path = calculatePath(resourceType);
if (!specificPath.isBlank()) {
path = path.concat("/").concat(specificPath);
if (!relativePath.isBlank()) {
path = path.concat("/").concat(relativePath);
}
String completeName = filename.concat(".").concat(resourceType.getExtension());
log.info("Requesting to write file {} in Object Store at path {}", file.getName(), path);
Context context = Vertx.currentContext();
return objectStoreService.uploadFile(file, path, resourceType, completeName)
.emitOn(command -> context.runOnContext(x -> command.run()))

String storageKey=path.concat("/").concat(completeName);
String finalPath = path;
return this.resourceFileService.findByStorageKey(storageKey)
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
.transformToUni(resource -> {
if(resource.isPresent()){
String errorMessage = String.format("Cannot upload %s: resource with same file name and path already exists", storageKey);
throw new AtmLayerException(errorMessage, Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED);
}
log.info("Requesting to write file {} in Object Store at path {}", file.getName(), finalPath);
Context context = Vertx.currentContext();
return objectStoreService.uploadFile(file, finalPath, resourceType, completeName)
.emitOn(command -> context.runOnContext(x -> command.run()))
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import it.gov.pagopa.atmlayer.service.model.service.ResourceFileService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.util.Optional;

@ApplicationScoped
public class ResourceFileServiceImpl implements ResourceFileService {

Expand All @@ -18,4 +21,10 @@ public class ResourceFileServiceImpl implements ResourceFileService {
public Uni<ResourceFile> save(ResourceFile resourceFile) {
return this.resourceFileRepository.persist(resourceFile);
}

@Override
public Uni<Optional<ResourceFile>> findByStorageKey(String storageKey) {
return resourceFileRepository.findByStorageKey(storageKey)
.onItem().transformToUni(resource -> Uni.createFrom().item(Optional.ofNullable(resource)));
}
}

0 comments on commit 6587978

Please sign in to comment.