Search in sources :

Example 1 with UploadFinishedHandler

use of com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler in project catma by forTEXT.

the class UploadStep method initComponents.

private void initComponents() {
    HorizontalLayout uploadPanel = new HorizontalLayout();
    uploadPanel.setWidth("100%");
    uploadPanel.setSpacing(true);
    uploadPanel.setMargin(false);
    addComponent(uploadPanel);
    UploadStateWindow uploadStateWindow = new UploadStateWindow();
    uploadStateWindow.setModal(true);
    uploadStateWindow.setWindowPosition(WindowPosition.CENTER);
    upload = new MultiFileUpload(new UploadFinishedHandler() {

        @Override
        public void handleFile(InputStream stream, String fileName, String mimeType, long length, int filesLeftInQueue) {
            try {
                String tempDir = ((CatmaApplication) UI.getCurrent()).accquirePersonalTempFolder();
                final String fileId = idGenerator.generateDocumentId();
                File tempFile = new File(new File(tempDir), fileId);
                if (tempFile.exists()) {
                    tempFile.delete();
                }
                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                    IOUtils.copy(stream, fos);
                }
                try (FileInputStream fis = new FileInputStream(tempFile)) {
                    String type = tika.detect(fis, fileName);
                    UploadFile uploadFile = new UploadFile(fileId, tempFile.toURI(), fileName, type, length);
                    if (type.toLowerCase().trim().equals(FileType.ZIP.getMimeType())) {
                        handleZipFile(uploadFile);
                    } else {
                        fileList.add(uploadFile);
                        fileDataProvider.refreshAll();
                        stepChangeListener.stepChanged(UploadStep.this);
                    }
                }
            } catch (Exception e) {
                String errorMsg = e.getMessage();
                if ((errorMsg == null) || (errorMsg.trim().isEmpty())) {
                    errorMsg = "";
                }
                Notification.show("Error", String.format("Error uploading %1$s, file will be skipped!\n%2$s", fileName, errorMsg), Type.ERROR_MESSAGE);
            }
        }
    }, uploadStateWindow) {

        @Override
        protected UploadStatePanel createStatePanel(UploadStateWindow uploadStateWindow) {
            return new MyUploadStatePanel(uploadStateWindow);
        }
    };
    uploadPanel.addComponent(upload);
    // 100 MB
    int maxFileSize = 104857600;
    upload.setMaxFileSize(maxFileSize);
    String errorMsgPattern = "File is too big (max = {0}): {2} ({1})";
    upload.setSizeErrorMsgPattern(errorMsgPattern);
    upload.setCaption("Upload files from your local computer");
    upload.setPanelCaption("Uploading file");
    upload.setMaxFileCount(300);
    upload.getSmartUpload().setUploadButtonCaptions("", "");
    upload.getSmartUpload().setUploadButtonIcon(VaadinIcons.UPLOAD);
    urlInputField = new TextField("or add a URL");
    urlInputField.setValueChangeMode(ValueChangeMode.BLUR);
    urlInputField.setWidth("100%");
    uploadPanel.addComponent(urlInputField);
    btFetch = new Button(VaadinIcons.CLOUD_DOWNLOAD);
    uploadPanel.addComponent(btFetch);
    uploadPanel.setComponentAlignment(btFetch, Alignment.BOTTOM_LEFT);
    progressBar = new ProgressBar();
    progressBar.setVisible(false);
    uploadPanel.addComponent(progressBar);
    fileGrid = new Grid<UploadFile>("or drag and drop some files to the list", fileDataProvider);
    fileGrid.setSizeFull();
    fileGrid.addColumn(UploadFile::getOriginalFilename).setCaption("File");
    fileGrid.addColumn(UploadFile::getMimetype).setCaption("Type");
    addComponent(fileGrid);
    setExpandRatio(fileGrid, 1.0f);
}
Also used : MultiFileUpload(com.wcs.wcslib.vaadin.widget.multifileupload.ui.MultiFileUpload) UploadStateWindow(com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadStateWindow) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UploadFinishedHandler(com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler) UploadStatePanel(com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadStatePanel) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) HorizontalLayout(com.vaadin.ui.HorizontalLayout) CatmaApplication(de.catma.ui.CatmaApplication) Button(com.vaadin.ui.Button) FileOutputStream(java.io.FileOutputStream) TextField(com.vaadin.ui.TextField) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) Html5File(com.vaadin.ui.Html5File) File(java.io.File) ProgressBar(com.vaadin.ui.ProgressBar)

Aggregations

Button (com.vaadin.ui.Button)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 Html5File (com.vaadin.ui.Html5File)1 ProgressBar (com.vaadin.ui.ProgressBar)1 TextField (com.vaadin.ui.TextField)1 MultiFileUpload (com.wcs.wcslib.vaadin.widget.multifileupload.ui.MultiFileUpload)1 UploadFinishedHandler (com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler)1 UploadStatePanel (com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadStatePanel)1 UploadStateWindow (com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadStateWindow)1 CatmaApplication (de.catma.ui.CatmaApplication)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1