use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.
the class DataValueController method saveFileDataValue.
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@PostMapping(FILE_PATH)
@ResponseBody
public WebMessage saveFileDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String ds, @RequestParam(required = false) String comment, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) boolean force, @RequestParam MultipartFile file, @CurrentUser User currentUser) throws WebMessageException, IOException {
FileResource fileResource = fileResourceUtils.saveFileResource(file, FileResourceDomain.DATA_VALUE);
saveDataValueInternal(de, co, cc, cp, pe, ou, ds, fileResource.getUid(), comment, followUp, force, currentUser);
WebMessage webMessage = new WebMessage(Status.OK, HttpStatus.ACCEPTED);
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
return webMessage;
}
use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.
the class DataValueController method getDataValueFile.
// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@GetMapping("/files")
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(defaultValue = "original") String dimension, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
if (!dataElement.isFileType()) {
throw new WebMessageException(conflict("DataElement must be of type file"));
}
CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, false);
CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
Period period = dataValueValidation.getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
dataValueValidation.validateOrganisationUnitPeriod(organisationUnit, period);
// ---------------------------------------------------------------------
// Get data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(conflict("Data value does not exist"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
String uid = dataValue.getValue();
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(notFound("A data value file resource with id " + uid + " does not exist."));
}
FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
if (storageStatus != FileResourceStorageStatus.STORED) {
// HTTP 409, for lack of a more suitable status code
throw new WebMessageException(conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.").setResponse(new FileResourceWebMessageResponse(fileResource)));
}
response.setContentType(fileResource.getContentType());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileResourceService.getFileResourceContentLength(fileResource)));
setNoStore(response);
try {
fileResourceService.copyFileResourceContent(fileResource, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend, could be network or filesystem related"));
}
}
use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.
the class EventController method getEventDataValueFile.
@GetMapping("/files")
public void getEventDataValueFile(@RequestParam String eventUid, @RequestParam String dataElementUid, @RequestParam(required = false) ImageFileDimension dimension, HttpServletResponse response, HttpServletRequest request) throws Exception {
Event event = eventService.getEvent(programStageInstanceService.getProgramStageInstance(eventUid));
if (event == null) {
throw new WebMessageException(notFound("Event not found for ID " + eventUid));
}
DataElement dataElement = dataElementService.getDataElement(dataElementUid);
if (dataElement == null) {
throw new WebMessageException(notFound("DataElement not found for ID " + dataElementUid));
}
if (!dataElement.isFileType()) {
throw new WebMessageException(conflict("DataElement must be of type file"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
String uid = null;
for (DataValue value : event.getDataValues()) {
if (value.getDataElement() != null && value.getDataElement().equals(dataElement.getUid())) {
uid = value.getValue();
break;
}
}
if (uid == null) {
throw new WebMessageException(conflict("DataElement must be of type file"));
}
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(notFound("A data value file resource with id " + uid + " does not exist."));
}
if (fileResource.getStorageStatus() != FileResourceStorageStatus.STORED) {
throw new WebMessageException(conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.").setResponse(new FileResourceWebMessageResponse(fileResource)));
}
FileResourceUtils.setImageFileDimensions(fileResource, MoreObjects.firstNonNull(dimension, ImageFileDimension.ORIGINAL));
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
try {
fileResourceService.copyFileResourceContent(fileResource, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend, could be network or filesystem related"));
}
}
Aggregations