use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.
the class ResourceUploadPlugin method handleUpload.
/**
* Handles the file upload from the form.
*
* @param upload the {@link FileUpload} containing the upload information
*/
private void handleUpload(FileUpload upload) throws FileUploadViolationException {
final PooledS3Connector s3Connector = HippoServiceRegistry.getService(PooledS3Connector.class);
String fileName = upload.getClientFileName();
String mimeType = upload.getContentType();
try {
final S3ObjectMetadata s3ObjectMetadata = s3Connector.upload(wrapCheckedException(upload::getInputStream), fileName, mimeType);
JcrNodeModel nodeModel = (JcrNodeModel) this.getDefaultModel();
Node node = nodeModel.getNode();
try {
setResourceProperties(node, s3ObjectMetadata);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} catch (Exception ex) {
log.error("Cannot upload resource", ex);
throw new FileUploadViolationException(ex.getMessage());
}
}
use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.
the class ExternalFileCopyTask method processResourceNodes.
@Override
protected void processResourceNodes(final PooledS3Connector s3, final NodeIterator resourceNodes) throws RepositoryException, WorkflowException {
final Node copiedDocumentFolderNode = copiedDocumentFolder.getNode(getWorkflowContext().getInternalWorkflowSession());
final Node copiedDocumentHandleNode = copiedDocumentFolderNode.getNode(copiedDocumentName);
for (final NodeIterator resourceNodeIterator = findResourceNodes(copiedDocumentHandleNode); resourceNodeIterator.hasNext(); ) {
final Node copiedResourceNode = resourceNodeIterator.nextNode();
final String oldReference = copiedResourceNode.getProperty(PROPERTY_EXTERNAL_STORAGE_REFERENCE).getString();
final S3ObjectMetadata s3ObjectMetadata = s3.copyFile(oldReference, copiedResourceNode.getProperty(PROPERTY_EXTERNAL_STORAGE_FILE_NAME).getString());
s3.unpublishResource(s3ObjectMetadata.getReference());
copiedResourceNode.setProperty(PROPERTY_EXTERNAL_STORAGE_REFERENCE, s3ObjectMetadata.getReference());
copiedResourceNode.setProperty(PROPERTY_EXTERNAL_STORAGE_PUBLIC_URL, s3ObjectMetadata.getUrl());
log.debug("Copied external resource {} as private {}", oldReference, s3ObjectMetadata);
}
}
use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.
the class ImageDisplayPlugin method createFileLink.
private ResourceLink createFileLink(S3NodeMetadata metadata) throws RepositoryException {
S3FileResourceStream s3FileResourceStream = new S3FileResourceStream(metadata);
ResourceStreamResource resourceStreamResource = new ResourceStreamResource(s3FileResourceStream) {
@Override
public void respond(final Attributes attributes) {
final PooledS3Connector s3ConnectorService = HippoServiceRegistry.getService(PooledS3Connector.class);
s3ConnectorService.download(metadata.getReference(), s3File -> {
s3FileResourceStream.setS3File(s3File);
super.respond(attributes);
});
}
};
resourceStreamResource.setCacheDuration(Duration.NONE);
resourceStreamResource.setFileName(metadata.getFileName());
resourceStreamResource.setContentDisposition(ContentDisposition.ATTACHMENT);
ResourceLink filelink = new ResourceLink("link", resourceStreamResource);
filelink.add(new Label("filename", new Model<>(metadata.getFileName())));
return filelink;
}
use of uk.nhs.digital.externalstorage.s3.PooledS3Connector in project hippo by NHS-digital-website.
the class S3ConnectorValve method invoke.
public void invoke(final ValveContext context) throws ContainerException {
// We're retrieving the service at the last possible moment to ensure that
// we always use the latest instance of it as the service may ge re-registered
// without restarting the application due to config changes in the Console.
final PooledS3Connector s3Connector = serviceProvider.getService(PooledS3Connector.class);
final String s3Reference = context.getServletRequest().getParameter("s3Reference");
s3Connector.download(s3Reference, s3File -> {
final HttpServletResponse response = context.getServletResponse();
if (context.getRequestContext().isChannelManagerPreviewRequest()) {
String fileName = context.getServletRequest().getParameter("fileName");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setHeader("Content-Length", String.valueOf(s3File.getLength()));
response.setStatus(HttpServletResponse.SC_ACCEPTED);
try (OutputStream responseOutputStream = response.getOutputStream();
InputStream s3InputStream = s3File.getContent()) {
IOUtils.copyLarge(s3InputStream, responseOutputStream);
} catch (final Exception ex) {
throw new RuntimeException("Failed to download content from S3: " + fileName + ": " + s3Reference, ex);
}
} else {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
});
}
Aggregations