use of org.hisp.dhis.fileresource.ExternalFileResource in project dhis2-core by dhis2.
the class ExternalFileResourceController method getExternalFileResource.
/**
* Returns a file associated with the externalFileResource resolved from the accessToken.
* <p>
* Only files contained in externalFileResources with a valid accessToken, expiration date null or in the future
* are files allowed to be served trough this endpoint.
*
* @param accessToken a unique string that resolves to a given externalFileResource
* @param response
* @throws WebMessageException
*/
@RequestMapping(value = "/{accessToken}", method = RequestMethod.GET)
public void getExternalFileResource(@PathVariable String accessToken, HttpServletResponse response) throws WebMessageException {
ExternalFileResource externalFileResource = externalFileResourceService.getExternalFileResourceByAccessToken(accessToken);
if (externalFileResource == null) {
throw new WebMessageException(WebMessageUtils.notFound("No file found with key '" + accessToken + "'"));
}
if (externalFileResource.getExpires() != null && externalFileResource.getExpires().before(new Date())) {
throw new WebMessageException(WebMessageUtils.createWebMessage("The key you requested has expired", Status.WARNING, HttpStatus.GONE));
}
FileResource fileResource = externalFileResource.getFileResource();
// ---------------------------------------------------------------------
// Attempt to build signed URL request for content and redirect
// ---------------------------------------------------------------------
URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(fileResource.getUid());
if (signedGetUri != null) {
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
return;
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
// ---------------------------------------------------------------------
// Request signing is not available, stream content back to client
// ---------------------------------------------------------------------
InputStream inputStream = null;
try {
inputStream = fileResourceService.getFileResourceContent(fileResource).openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.hisp.dhis.fileresource.ExternalFileResource in project dhis2-core by dhis2.
the class DefaultPushAnalysisService method uploadImage.
/**
* Uploads a byte array using FileResource and ExternalFileResource
*
* @param name name of the file to be stored
* @param bytes the byte array representing the file to be stored
* @return url pointing to the uploaded resource
* @throws IOException
*/
private String uploadImage(String name, byte[] bytes) throws IOException {
FileResource fileResource = new FileResource(name, // All files uploaded from PushAnalysis is PNG.
MimeTypeUtils.IMAGE_PNG.toString(), bytes.length, ByteSource.wrap(bytes).hash(Hashing.md5()).toString(), FileResourceDomain.PUSH_ANALYSIS);
fileResourceService.saveFileResource(fileResource, bytes);
ExternalFileResource externalFileResource = new ExternalFileResource();
externalFileResource.setFileResource(fileResource);
externalFileResource.setExpires(null);
String accessToken = externalFileResourceService.saveExternalFileResource(externalFileResource);
return systemSettingManager.getInstanceBaseUrl() + "/api/externalFileResources/" + accessToken;
}
Aggregations