Search in sources :

Example 21 with FileStorageException

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

the class DesktopFileDescriptorResource method createResource.

@Override
protected void createResource() {
    try {
        InputStream stream = AppBeans.get(FileLoader.class).openStream(fileDescriptor);
        resource = ImageIO.read(stream);
    } catch (FileStorageException e) {
        throw new RuntimeException(FILE_STORAGE_EXCEPTION_MESSAGE, e);
    } catch (IOException e) {
        throw new RuntimeException("An error occurred while loading an image.", e);
    }
}
Also used : InputStream(java.io.InputStream) FileLoader(com.haulmont.cuba.core.global.FileLoader) IOException(java.io.IOException) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 22 with FileStorageException

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

the class FileUploadController method upload.

@RequestMapping(value = "/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 {
            fileStorage.saveStream(fd, is);
        } 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 : 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) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with FileStorageException

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

the class WebFileUploadField method saveFile.

protected void saveFile(FileDescriptor fileDescriptor) {
    switch(mode) {
        case MANUAL:
            internalValueChangedOnUpload = true;
            setValue(fileDescriptor);
            internalValueChangedOnUpload = false;
            break;
        case IMMEDIATE:
            try {
                fileUploading.putFileIntoStorage(fileId, fileDescriptor);
                FileDescriptor committedDescriptor = commitFileDescriptor(fileDescriptor);
                setValue(committedDescriptor);
            } catch (FileStorageException e) {
                log.error("Error has occurred during file saving", e);
            }
            break;
    }
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

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