Search in sources :

Example 11 with FileDescriptor

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

the class DesktopFileUploadField method saveFile.

protected void saveFile(FileDescriptor fileDescriptor) {
    switch(mode) {
        case MANUAL:
            setValue(fileDescriptor);
            break;
        case IMMEDIATE:
            BackgroundTask<Long, FileDescriptor> uploadProgress = new BackgroundTask<Long, FileDescriptor>(2400, getFrame()) {

                @Override
                public Map<String, Object> getParams() {
                    return ParamsMap.of("fileId", fileId, "fileName", getFileName());
                }

                @Override
                public FileDescriptor run(final TaskLifeCycle<Long> taskLifeCycle) throws Exception {
                    return fileUploading.putFileIntoStorage(taskLifeCycle);
                }

                @Override
                public void done(FileDescriptor result) {
                    FileDescriptor descriptor = commitFileDescriptor(result);
                    setValue(descriptor);
                }
            };
            long fileSize = fileUploading.getFile(fileId).length();
            BackgroundWorkProgressWindow.show(uploadProgress, messages.getMainMessage("FileUploadField.uploadingFile"), null, fileSize, true, true);
            break;
    }
}
Also used : TaskLifeCycle(com.haulmont.cuba.gui.executors.TaskLifeCycle) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) BackgroundTask(com.haulmont.cuba.gui.executors.BackgroundTask)

Example 12 with FileDescriptor

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

the class DesktopFileUploadField method initImpl.

protected void initImpl() {
    impl = new CubaFileUploadWrapper(uploadButton);
    impl.setFileNameButtonClickListener(() -> {
        FileDescriptor value = getValue();
        if (value == null)
            return;
        switch(mode) {
            case MANUAL:
                String name = getFileName();
                String fileName1 = StringUtils.isEmpty(name) ? value.getName() : name;
                exportDisplay.show(DesktopFileUploadField.this::getFileContent, fileName1);
                break;
            case IMMEDIATE:
                exportDisplay.show(value);
        }
    });
    impl.setClearButtonListener(this::clearButtonClicked);
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 13 with FileDescriptor

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

the class FileDownloadController method getFileDescriptor.

protected FileDescriptor getFileDescriptor(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UUID fileId;
    try {
        fileId = UUID.fromString(request.getParameter("f"));
    } catch (Exception e) {
        log.error("Error parsing fileId from URL param", e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    FileDescriptor fileDescriptor = dataService.load(new LoadContext<>(FileDescriptor.class).setId(fileId));
    if (fileDescriptor == null)
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return fileDescriptor;
}
Also used : LoadContext(com.haulmont.cuba.core.global.LoadContext) UUID(java.util.UUID) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 14 with FileDescriptor

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

the class FileDownloadController method download.

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null)
        return;
    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        File file = null;
        FileDescriptor fd = null;
        if (request.getParameter("p") != null)
            file = getFile(request, response);
        else
            fd = getFileDescriptor(request, response);
        if (fd == null && file == null)
            return;
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setIntHeader("Expires", -1);
        response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE);
        InputStream is = null;
        ServletOutputStream os = null;
        try {
            is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file);
            os = response.getOutputStream();
            IOUtils.copy(is, os);
            os.flush();
        } catch (FileStorageException e) {
            log.error("Unable to download file", e);
            response.sendError(e.getType().getHttpStatus());
        } catch (Exception ex) {
            log.error("Unable to download file", ex);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) File(java.io.File) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with FileDescriptor

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

the class TestListenerUsingEntityManager method onBeforeInsert.

@Override
public void onBeforeInsert(Server entity, EntityManager entityManager) {
    EntityManager em = persistence.getEntityManager();
    FileDescriptor related = new FileDescriptor();
    related.setName("Related");
    System.out.println(">>>>> persist related: " + related.getId());
    em.persist(related);
    entity.setData(related.getId().toString());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

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