Search in sources :

Example 11 with FileResource

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;
}
Also used : FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) ByteSource(com.google.common.io.ByteSource) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Date(java.util.Date) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

FileResource (org.hisp.dhis.fileresource.FileResource)11 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ByteSource (com.google.common.io.ByteSource)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URI (java.net.URI)3 Date (java.util.Date)3 DataElement (org.hisp.dhis.dataelement.DataElement)3 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)3 FileResourceWebMessageResponse (org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse)3 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 DataValue (org.hisp.dhis.datavalue.DataValue)2 ExternalFileResource (org.hisp.dhis.fileresource.ExternalFileResource)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 File (java.io.File)1 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)1 Event (org.hisp.dhis.dxf2.events.event.Event)1