use of com.formkiq.lambda.apigateway.ApiMapResponse in project formkiq-core by formkiq.
the class DocumentIdContentRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
String documentId = event.getPathParameters().get("documentId");
String versionId = getParameter(event, "versionId");
DocumentItem item = awsservice.documentService().findDocument(siteId, documentId);
if (item == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
ApiResponse response = null;
String s3key = createS3Key(siteId, documentId);
if (MimeType.isPlainText(item.getContentType())) {
try (S3Client s3 = awsservice.s3Service().buildClient()) {
String content = awsservice.s3Service().getContentAsString(s3, awsservice.documents3bucket(), s3key, versionId);
response = new ApiMapResponse(Map.of("content", content, "contentType", item.getContentType(), "isBase64", Boolean.FALSE));
}
} else {
Duration duration = Duration.ofHours(1);
URL url = awsservice.s3Service().presignGetUrl(awsservice.documents3bucket(), s3key, duration, versionId);
response = new ApiMapResponse(Map.of("contentUrl", url.toString(), "contentType", item.getContentType() != null ? item.getContentType() : "application/octet-stream"));
}
return new ApiRequestHandlerResponse(SC_OK, response);
}
Aggregations