Skip to content

Commit

Permalink
fixed concat
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciaM1 committed Nov 9, 2023
1 parent bde2e42 commit ef25556
Showing 1 changed file with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.jboss.resteasy.reactive.RestMulti;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
Expand All @@ -30,73 +31,72 @@
@ApplicationScoped
@Slf4j
public class ResourceEntityStorageServiceImpl implements ResourceEntityStorageService {
@Inject
ObjectStoreStrategy objectStoreStrategy;
private ObjectStoreService objectStoreService;
@Inject
ObjectStoreProperties objectStoreProperties;
@Inject
ResourceFileService resourceFileService;

@Inject
ObjectStoreStrategy objectStoreStrategy;

private ObjectStoreService objectStoreService;

@Inject
ObjectStoreProperties objectStoreProperties;

@Inject
ResourceFileService resourceFileService;

public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,
ObjectStoreProperties objectStoreProperties) {
this.objectStoreStrategy = objectStoreStrategy;
this.objectStoreService = objectStoreStrategy.getType(
ObjectStoreStrategyEnum.fromValue(objectStoreProperties.type()));
}
public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,
ObjectStoreProperties objectStoreProperties) {
this.objectStoreStrategy = objectStoreStrategy;
this.objectStoreService = objectStoreStrategy.getType(
ObjectStoreStrategyEnum.fromValue(objectStoreProperties.type()));
}

@Override
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename,String specificPath) {
ResourceTypeEnum resourceType = resourceEntity.getResourceTypeEnum();
String path = calculatePath(resourceType).concat("/").concat(specificPath);
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()))
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
}
@Override
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename, String specificPath) {
ResourceTypeEnum resourceType = resourceEntity.getResourceTypeEnum();
String path = calculatePath(resourceType);
if (!specificPath.isBlank()) {
path = path.concat("/").concat(specificPath);
}
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()))
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
}

@Override
public Uni<URL> generatePresignedUrl(String storageKey) {
return this.objectStoreService.generatePresignedUrl(storageKey);
}
@Override
public Uni<URL> generatePresignedUrl(String storageKey) {
return this.objectStoreService.generatePresignedUrl(storageKey);
}

@Override
public RestMulti<Buffer> download(String storageKey) {
return this.objectStoreService.download(storageKey);
}
@Override
public RestMulti<Buffer> download(String storageKey) {
return this.objectStoreService.download(storageKey);
}

@WithTransaction
public Uni<ResourceFile> writeResourceInfoToDatabase(ResourceEntity resourceEntity,
ObjectStorePutResponse putObjectResponse, String filename) {
ResourceFile entity = ResourceFile.builder()
.fileName(filename)
.resourceType(resourceEntity.getResourceTypeEnum())
.resourceEntity(resourceEntity)
.storageKey(putObjectResponse.getStorage_key())
.build();
return resourceFileService.save(entity);
}
@WithTransaction
public Uni<ResourceFile> writeResourceInfoToDatabase(ResourceEntity resourceEntity,
ObjectStorePutResponse putObjectResponse, String filename) {
ResourceFile entity = ResourceFile.builder()
.fileName(filename)
.resourceType(resourceEntity.getResourceTypeEnum())
.resourceEntity(resourceEntity)
.storageKey(putObjectResponse.getStorage_key())
.build();
return resourceFileService.save(entity);
}

private String calculatePath(ResourceTypeEnum resourceTypeEnum) {
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("RESOURCE_TYPE", resourceTypeEnum.toString());
StringSubstitutor stringSubstitutor = new StringSubstitutor(valuesMap);
Optional<String> resourceEntityPathTemplateProps = Optional.ofNullable(
objectStoreProperties.html().pathTemplate());
String pathTemplate = null;
if (resourceEntityPathTemplateProps.isPresent() && StringUtils.isNotBlank(
resourceEntityPathTemplateProps.get())) {
pathTemplate = resourceEntityPathTemplateProps.get();
pathTemplate = pathTemplate.replace("[", "${").replace("]", "}");
private String calculatePath(ResourceTypeEnum resourceTypeEnum) {
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("RESOURCE_TYPE", resourceTypeEnum.toString());
StringSubstitutor stringSubstitutor = new StringSubstitutor(valuesMap);
Optional<String> resourceEntityPathTemplateProps = Optional.ofNullable(
objectStoreProperties.html().pathTemplate());
String pathTemplate = null;
if (resourceEntityPathTemplateProps.isPresent() && StringUtils.isNotBlank(
resourceEntityPathTemplateProps.get())) {
pathTemplate = resourceEntityPathTemplateProps.get();
pathTemplate = pathTemplate.replace("[", "${").replace("]", "}");
}
return stringSubstitutor.replace(pathTemplate);
}
return stringSubstitutor.replace(pathTemplate);
}
}

0 comments on commit ef25556

Please sign in to comment.