Search in sources :

Example 6 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project documentation by cuba-platform.

the class EmployeeEdit method init.

@Override
public void init(Map<String, Object> params) {
    uploadField.addFileUploadSucceedListener(event -> {
        FileDescriptor fd = uploadField.getFileDescriptor();
        try {
            fileUploadingAPI.putFileIntoStorage(uploadField.getFileId(), fd);
        } catch (FileStorageException e) {
            throw new RuntimeException("Error saving file to FileStorage", e);
        }
        getItem().setImageFile(dataSupplier.commit(fd));
        displayImage();
    });
    uploadField.addFileUploadErrorListener(event -> showNotification("File upload error", NotificationType.HUMANIZED));
    employeeDs.addItemPropertyChangeListener(event -> {
        if ("imageFile".equals(event.getProperty()))
            updateImageButtons(event.getValue() != null);
    });
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 7 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException 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 8 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class FileLoaderImpl method checkIfFileDescriptorExists.

protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException {
    try (Transaction tx = persistence.getTransaction()) {
        FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId());
        if (existingFile == null || entityStates.isDeleted(existingFile)) {
            throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName());
        }
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 9 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class FileStorageExceptionHandler method doHandle.

@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) {
    String msg = null;
    if (throwable != null) {
        FileStorageException storageException = (FileStorageException) throwable;
        String fileName = storageException.getFileName();
        if (storageException.getType().equals(FileStorageException.Type.FILE_NOT_FOUND))
            msg = messages.formatMessage(getClass(), "fileNotFound.message", fileName);
        else if (storageException.getType().equals(FileStorageException.Type.STORAGE_INACCESSIBLE))
            msg = messages.getMessage(getClass(), "fileStorageInaccessible.message");
    }
    if (msg == null) {
        msg = messages.getMessage(getClass(), "fileStorageException.message");
    }
    windowManager.showNotification(msg, Frame.NotificationType.ERROR);
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 10 with FileStorageException

use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.

the class WebFileMultiUploadField method initComponent.

protected void initComponent(CubaFileUpload impl) {
    impl.setMultiSelect(true);
    Messages messages = beanLocator.get(Messages.NAME);
    impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
    impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
    impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
    impl.setCaption(messages.getMainMessage("upload.submit"));
    impl.setDropZonePrompt(messages.getMainMessage("upload.dropZonePrompt"));
    impl.setDescription(null);
    Configuration configuration = beanLocator.get(Configuration.NAME);
    int maxUploadSizeMb = configuration.getConfig(ClientConfig.class).getMaxUploadSizeMb();
    int maxSizeBytes = maxUploadSizeMb * BYTES_IN_MEGABYTE;
    impl.setFileSizeLimit(maxSizeBytes);
    impl.setReceiver((fileName, MIMEType) -> {
        FileOutputStream outputStream;
        try {
            FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
            tempFileId = fileInfo.getId();
            File tmpFile = fileInfo.getFile();
            outputStream = new FileOutputStream(tmpFile);
        } catch (Exception e) {
            throw new RuntimeException("Unable to receive file", e);
        }
        return outputStream;
    });
    impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
    impl.addQueueUploadFinishedListener(event -> fireQueueUploadComplete());
    impl.addSucceededListener(event -> {
        files.put(tempFileId, event.getFileName());
        fireFileUploadFinish(event.getFileName(), event.getContentLength());
    });
    impl.addFailedListener(event -> {
        try {
            // close and remove temp file
            fileUploading.deleteFile(tempFileId);
            tempFileId = null;
        } catch (Exception e) {
            if (e instanceof FileStorageException) {
                FileStorageException fse = (FileStorageException) e;
                if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
                    LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Could not remove temp file {} after broken uploading", tempFileId);
                }
            }
            LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Error while delete temp file {}", tempFileId);
        }
        fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
    });
    impl.addFileSizeLimitExceededListener(e -> {
        Notifications notifications = getScreenContext(this).getNotifications();
        notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("multiupload.filesizeLimitExceed", e.getFileName(), getFileSizeLimitString())).show();
    });
    impl.addFileExtensionNotAllowedListener(e -> {
        Notifications notifications = getScreenContext(this).getNotifications();
        notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName())).show();
    });
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Configuration(com.haulmont.cuba.core.global.Configuration) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) FileOutputStream(java.io.FileOutputStream) ClientConfig(com.haulmont.cuba.client.ClientConfig) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) File(java.io.File) Notifications(com.haulmont.cuba.gui.Notifications) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Aggregations

FileStorageException (com.haulmont.cuba.core.global.FileStorageException)23 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)9 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 File (java.io.File)4 HttpUtils (com.haulmont.cuba.core.app.filestorage.amazon.util.HttpUtils)3 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)3 FileUploadingAPI (com.haulmont.cuba.gui.upload.FileUploadingAPI)3 UserSession (com.haulmont.cuba.security.global.UserSession)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ClientConfig (com.haulmont.cuba.client.ClientConfig)2 Transaction (com.haulmont.cuba.core.Transaction)2 Configuration (com.haulmont.cuba.core.global.Configuration)2 Messages (com.haulmont.cuba.core.global.Messages)2 Notifications (com.haulmont.cuba.gui.Notifications)2 CubaFileUpload (com.haulmont.cuba.web.toolkit.ui.CubaFileUpload)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2