Search in sources :

Example 1 with CubaFileDownloader

use of com.haulmont.cuba.web.widgets.CubaFileDownloader in project cuba by cuba-platform.

the class WebExportDisplay method show.

/**
 * Show/Download resource at client side
 *
 * @param dataProvider ExportDataProvider
 * @param resourceName ResourceName for client side
 * @param exportFormat ExportFormat
 * @see com.haulmont.cuba.gui.export.FileDataProvider
 * @see com.haulmont.cuba.gui.export.ByteArrayDataProvider
 */
@Override
public void show(ExportDataProvider dataProvider, String resourceName, final ExportFormat exportFormat) {
    backgroundWorker.checkUIAccess();
    boolean showNewWindow = this.newWindow;
    if (useViewList) {
        String fileExt;
        if (exportFormat != null) {
            fileExt = exportFormat.getFileExt();
        } else {
            fileExt = FilenameUtils.getExtension(resourceName);
        }
        WebConfig webConfig = configuration.getConfig(WebConfig.class);
        showNewWindow = webConfig.getViewFileExtensions().contains(StringUtils.lowerCase(fileExt));
    }
    if (exportFormat != null) {
        if (StringUtils.isEmpty(FilenameUtils.getExtension(resourceName))) {
            resourceName += "." + exportFormat.getFileExt();
        }
    }
    CubaFileDownloader fileDownloader = AppUI.getCurrent().getFileDownloader();
    fileDownloader.setFileNotFoundExceptionListener(this::handleFileNotFoundException);
    StreamResource resource = new StreamResource(dataProvider::provide, resourceName);
    if (exportFormat != null && StringUtils.isNotEmpty(exportFormat.getContentType())) {
        resource.setMIMEType(exportFormat.getContentType());
    } else {
        resource.setMIMEType(FileTypesHelper.getMIMEType(resourceName));
    }
    if ((showNewWindow && isBrowserSupportsPopups()) || isIOS()) {
        fileDownloader.viewDocument(resource);
    } else {
        fileDownloader.downloadFile(resource);
    }
}
Also used : CubaFileDownloader(com.haulmont.cuba.web.widgets.CubaFileDownloader) StreamResource(com.vaadin.server.StreamResource) WebConfig(com.haulmont.cuba.web.WebConfig)

Aggregations

WebConfig (com.haulmont.cuba.web.WebConfig)1 CubaFileDownloader (com.haulmont.cuba.web.widgets.CubaFileDownloader)1 StreamResource (com.vaadin.server.StreamResource)1