use of org.hisp.dhis.fileresource.FileResource in project dhis2-core by dhis2.
the class FileResourceController method saveFileResource.
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public WebMessage saveFileResource(@RequestParam MultipartFile file) throws WebMessageException, IOException {
String filename = StringUtils.defaultIfBlank(FilenameUtils.getName(file.getOriginalFilename()), DEFAULT_FILENAME);
String contentType = file.getContentType();
contentType = isValidContentType(contentType) ? contentType : DEFAULT_CONTENT_TYPE;
long contentLength = file.getSize();
if (contentLength <= 0) {
throw new WebMessageException(WebMessageUtils.conflict("Could not read file or file is empty."));
}
ByteSource bytes = new MultipartFileByteSource(file);
String contentMd5 = bytes.hash(Hashing.md5()).toString();
FileResource fileResource = new FileResource(filename, contentType, contentLength, contentMd5, FileResourceDomain.DATA_VALUE);
fileResource.setAssigned(false);
fileResource.setCreated(new Date());
fileResource.setUser(currentUserService.getCurrentUser());
File tmpFile = toTempFile(file);
String uid = fileResourceService.saveFileResource(fileResource, tmpFile);
if (uid == null) {
throw new WebMessageException(WebMessageUtils.error("Saving the file failed."));
}
WebMessage webMessage = new WebMessage(Status.OK, HttpStatus.ACCEPTED);
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
return webMessage;
}
Aggregations