Skip to content

Commit

Permalink
Fix regex and filenameWithExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Nov 10, 2023
1 parent 202921e commit 1c3ec2d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public class ResourceCreationDto {
private File file;
@FormParam("filename")
@NotNull(message = "filename is required")
@Pattern(regexp = "^[a-zA-Z0-9_-]+$", message = "it must be of form ${regexp} and must not contain file extension")
@Pattern(regexp = "^[a-zA-Z0-9_-]+\\.[a-zA-Z]+$", message = "it must be of form ${regexp}")
private String filename;
@FormParam("resourceType")
@NotNull(message = "resource type is required")
private NoDeployableResourceType resourceType;
@FormParam("path")
@Nullable
@Pattern(regexp = "^(?!/)[a-zA-Z0-9/]+(?<!/)$", message = "String must not start or end with '/' and must not contain white spaces and special characters")
@Pattern(regexp = "(^$)|(^(?!/)[a-zA-Z0-9/]+(?<!/)$)", message = "String must not start or end with '/' and must not contain white spaces and special characters")
@DefaultValue("")
private String path;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
@AllArgsConstructor
@Getter
public enum DeployableResourceType {
BPMN("bpmn","application/bpmn", "bpmn:process", "id"),
DMN("dmn", "application/dmn", "decision", "id"),
FORM("json", "application/json", "id", null);
BPMN("bpmn:process", "id"),
DMN("decision", "id"),
FORM("id", null);

String extension;
String mimetype;
String tagName;
String attribute;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
@AllArgsConstructor
@Getter
public enum NoDeployableResourceType {
HTML("html", "application/html", null, null);

String extension;
String mimetype;
String tagName;
String attribute;
HTML("html", "application/html"),
OTHER(null, null);

final String extension;
final String mimetype;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
@AllArgsConstructor
@Getter
public enum S3ResourceTypeEnum {
BPMN("bpmn","application/bpmn", "bpmn:process", "id"),
DMN("dmn", "application/dmn", "decision", "id"),
FORM("json", "application/json", null, null),
HTML("html", "application/html", null, null);
BPMN("bpmn","application/bpmn"),
DMN("dmn", "application/dmn"),
FORM("json", "application/json"),
HTML("html", "application/html"),
OTHER("other", "other");

String extension;
String mimetype;
String tagName;
String attribute;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,17 @@ public Uni<Optional<ResourceEntity>> findByUUID(UUID uuid) {
public Uni<ResourceEntity> saveAndUpload(ResourceEntity resourceEntity, File file,
String filename, String path) {
return this.save(resourceEntity)
.onItem().transformToUni(record -> {
return this.resourceEntityStorageService.uploadFile(resourceEntity, file, filename, path)
.onFailure().recoverWithUni(failure -> {
log.error(failure.getMessage());
return Uni.createFrom().failure(new AtmLayerException(
"Failed to save Resource Entity in Object Store. Resource creation aborted",
Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_SAVE_FILE_ERROR));
})
.onItem().transformToUni(putObjectResponse -> {
log.info("Completed Resource Entity Creation");
return Uni.createFrom().item(record);
});
});
.onItem().transformToUni(record -> this.resourceEntityStorageService.uploadFile(resourceEntity, file, filename, path)
.onFailure().recoverWithUni(failure -> {
log.error(failure.getMessage());
return Uni.createFrom().failure(new AtmLayerException(
"Failed to save Resource Entity in Object Store. Resource creation aborted",
Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_SAVE_FILE_ERROR));
})
.onItem().transformToUni(putObjectResponse -> {
log.info("Completed Resource Entity Creation");
return Uni.createFrom().item(record);
}));
}

@Override
Expand All @@ -98,17 +96,15 @@ public Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File fi
RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS);
}
return saveAndUpload(resourceEntity, file, filename, path)
.onItem().transformToUni(bpmn -> {
return this.findByUUID(resourceEntity.getResourceId())
.onItem().transformToUni(optionalResource -> {
if (optionalResource.isEmpty()) {
return Uni.createFrom().failure(
new AtmLayerException("Sync problem on resource creation",
Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500));
}
return Uni.createFrom().item(optionalResource.get());
});
});
.onItem().transformToUni(bpmn -> this.findByUUID(resourceEntity.getResourceId())
.onItem().transformToUni(optionalResource -> {
if (optionalResource.isEmpty()) {
return Uni.createFrom().failure(
new AtmLayerException("Sync problem on resource creation",
Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500));
}
return Uni.createFrom().item(optionalResource.get());
}));
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,
}

@Override
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename, String relativePath) {
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filenameWithExtension, String relativePath) {
S3ResourceTypeEnum resourceType = convertEnum(resourceEntity.getNoDeployableResourceType());
String path = calculatePath(resourceType);
if (!relativePath.isBlank()) {
path = path.concat("/").concat(relativePath);
}
String completeName = filename.concat(".").concat(resourceType.getExtension());

String storageKey=path.concat("/").concat(completeName);
String storageKey=path.concat("/").concat(filenameWithExtension);
String finalPath = path;
return this.resourceFileService.findByStorageKey(storageKey)
.onItem()
Expand All @@ -72,11 +71,11 @@ public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, St
}
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)
return objectStoreService.uploadFile(file, finalPath, resourceType, filenameWithExtension)
.emitOn(command -> context.runOnContext(x -> command.run()))
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
objectStorePutResponse, filenameWithExtension.split("\\.")[0]));
}));
}

Expand Down

0 comments on commit 1c3ec2d

Please sign in to comment.