Search in sources :

Example 1 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class FileUploadController method upload.

@RequestMapping(value = "/api/upload", method = RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null)
        return;
    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        InputStream is = request.getInputStream();
        if (is == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        FileDescriptor fd = getFileDescriptor(request, response);
        if (fd == null)
            return;
        try {
            uploadToMiddleware(userSession, is, fd);
            saveFileDescriptor(fd);
            response.setStatus(HttpServletResponse.SC_OK);
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
            writer.write(fd.getId().toString());
            writer.close();
        } catch (FileStorageException e) {
            log.error("Unable to upload file", e);
            response.sendError(e.getType().getHttpStatus());
        } finally {
            IOUtils.closeQuietly(is);
        }
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class FileUploadController method getFileDescriptor.

protected FileDescriptor getFileDescriptor(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String name = request.getParameter("name");
    if (name == null) {
        log.error("'name' request parameter is not specified");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    String ext = request.getParameter("ext");
    if (ext == null) {
        log.error("'ext' request parameter is not specified");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    String size = request.getParameter("size");
    FileDescriptor fd = metadata.create(FileDescriptor.class);
    fd.setName(name);
    fd.setExtension(ext);
    fd.setCreateDate(timeSource.currentTimestamp());
    if (size != null) {
        fd.setSize(Long.parseLong(size));
    }
    return fd;
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 3 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class RestFileDownloadController method download.

@RequestMapping(value = "/api/download", method = RequestMethod.GET)
public ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null) {
        error(response);
        return null;
    }
    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        UUID fileId;
        try {
            fileId = UUID.fromString(request.getParameter("f"));
        } catch (Exception e) {
            log.error(e.toString());
            error(response);
            return null;
        }
        FileDescriptor fd = dataService.load(new LoadContext<>(FileDescriptor.class).setId(fileId));
        if (fd == null) {
            log.warn("Unable to find file with id " + fileId);
            error(response);
            return null;
        }
        String fileName = URLEncodeUtils.encodeUtf8(fd.getName());
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Content-Type", getContentType(fd));
        response.setHeader("Pragma", "no-cache");
        boolean attach = Boolean.valueOf(request.getParameter("a"));
        response.setHeader("Content-Disposition", (attach ? "attachment" : "inline") + "; filename=" + fileName);
        writeResponse(response, userSession, fd);
    } finally {
        AppContext.setSecurityContext(null);
    }
    return null;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) LoadContext(com.haulmont.cuba.core.global.LoadContext) UUID(java.util.UUID) IOException(java.io.IOException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class FileUploadController method uploadFile.

/**
 * Method for multipart file upload. It expects the file contents to be passed in the part called 'file'
 */
@PostMapping(consumes = "multipart/form-data")
public ResponseEntity<FileInfo> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name, HttpServletRequest request) {
    try {
        if (Strings.isNullOrEmpty(name)) {
            name = file.getOriginalFilename();
        }
        long size = file.getSize();
        FileDescriptor fd = createFileDescriptor(name, size);
        InputStream is = file.getInputStream();
        uploadToMiddleware(is, fd);
        saveFileDescriptor(fd);
        return createFileInfoResponseEntity(request, fd);
    } catch (Exception e) {
        log.error("File upload failed", e);
        throw new RestAPIException("File upload failed", "File upload failed", HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) RestAPIException(com.haulmont.restapi.exception.RestAPIException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RestAPIException(com.haulmont.restapi.exception.RestAPIException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.

the class AmazonS3FileStorageTest method setUp.

@Before
public void setUp() throws Exception {
    fileDescr = new FileDescriptor();
    fileDescr.setCreateDate(new Date());
    fileDescr.setSize((long) FILE_CONTENT.length());
    fileDescr.setName("AmazonFileStorageTest");
    fileDescr.setExtension("txt");
    fileDescr2 = new FileDescriptor();
    fileDescr2.setCreateDate(new Date());
    fileDescr2.setSize((long) FILE_CONTENT.length());
    fileDescr2.setName("AmazonFileStorageTest");
    fileStorageAPI = new AmazonS3FileStorage();
    fileStorageAPI.amazonS3Config = AppBeans.get(Configuration.class).getConfig(AmazonS3Config.class);
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) Date(java.util.Date)

Aggregations

FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)47 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)12 UUID (java.util.UUID)9 EntityManager (com.haulmont.cuba.core.EntityManager)5 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)5 UserSession (com.haulmont.cuba.security.global.UserSession)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Transaction (com.haulmont.cuba.core.Transaction)3 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)3 FileDataProvider (com.haulmont.cuba.gui.export.FileDataProvider)3 RestAPIException (com.haulmont.restapi.exception.RestAPIException)3 File (java.io.File)3 FileStorageAPI (com.haulmont.cuba.core.app.FileStorageAPI)2 FileStorageService (com.haulmont.cuba.core.app.FileStorageService)2 LoadContext (com.haulmont.cuba.core.global.LoadContext)2 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)2 TaskLifeCycle (com.haulmont.cuba.gui.executors.TaskLifeCycle)2 ByteArrayDataProvider (com.haulmont.cuba.gui.export.ByteArrayDataProvider)2