Search in sources :

Example 6 with FileDescriptor

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

the class CubaFileUploadWrapper method setInternalValue.

@Override
protected void setInternalValue(Object newValue) {
    // noinspection unchecked
    super.setInternalValue(newValue);
    if (newValue != null) {
        FileDescriptor fileDescriptor = (FileDescriptor) newValue;
        setFileNameButtonCaption(fileDescriptor.getName());
    } else {
        setFileNameButtonCaption(null);
    }
    onSetInternalValue(newValue);
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 7 with FileDescriptor

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

the class WebImage method createImageResource.

protected Resource createImageResource(final Object resourceObject) {
    if (resourceObject == null) {
        return null;
    }
    if (resourceObject instanceof FileDescriptor) {
        FileDescriptorResource imageResource = createResource(FileDescriptorResource.class);
        imageResource.setFileDescriptor((FileDescriptor) resourceObject);
        return imageResource;
    }
    if (resourceObject instanceof byte[]) {
        StreamResource imageResource = createResource(StreamResource.class);
        Supplier<InputStream> streamSupplier = () -> new ByteArrayDataProvider((byte[]) resourceObject).provide();
        imageResource.setStreamSupplier(streamSupplier);
        return imageResource;
    }
    throw new GuiDevelopmentException("The Image component supports only FileDescriptor and byte[] datasource property value binding", getFrame().getId());
}
Also used : ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) StreamResource(com.haulmont.cuba.gui.components.StreamResource) InputStream(java.io.InputStream) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) FileDescriptorResource(com.haulmont.cuba.gui.components.FileDescriptorResource) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 8 with FileDescriptor

use of com.haulmont.cuba.core.entity.FileDescriptor 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 9 with FileDescriptor

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

the class EmployeeBrowse method init.

@Override
public void init(Map<String, Object> params) {
    employeesTable.addGeneratedColumn("name", entity -> {
        Image image = componentsFactory.createComponent(Image.class);
        image.setScaleMode(ScaleMode.CONTAIN);
        image.setHeight("40");
        image.setWidth("40");
        FileDescriptor userImageFile = entity.getImageFile();
        image.setSource(FileDescriptorResource.class).setFileDescriptor(userImageFile);
        Label userLogin = componentsFactory.createComponent(Label.class);
        userLogin.setValue(entity.getName());
        userLogin.setAlignment(Alignment.MIDDLE_LEFT);
        HBoxLayout hBox = componentsFactory.createComponent(HBoxLayout.class);
        hBox.setSpacing(true);
        hBox.add(image);
        hBox.add(userLogin);
        return hBox;
    });
}
Also used : Image(com.haulmont.cuba.gui.components.Image) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 10 with FileDescriptor

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

the class DesktopFileUploadField method getFileContent.

@Override
public InputStream getFileContent() {
    if (contentProvider != null) {
        return contentProvider.provide();
    }
    FileDescriptor fileDescriptor = getValue();
    switch(mode) {
        case MANUAL:
            if (fileId == null) {
                return new FileDataProvider(fileDescriptor).provide();
            }
            File file = fileUploading.getFile(fileId);
            if (file != null) {
                try {
                    return new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    log.error("Unable to get content of {}", file, e);
                }
                return null;
            }
            FileStorageService fileStorageService = AppBeans.get(FileStorageService.NAME);
            try {
                if (fileStorageService.fileExists(fileDescriptor)) {
                    return new FileDataProvider(fileDescriptor).provide();
                }
            } catch (FileStorageException e) {
                log.error("Unable to get content of {}", fileDescriptor, e);
                return null;
            }
            break;
        case IMMEDIATE:
            if (fileDescriptor != null) {
                return new FileDataProvider(fileDescriptor).provide();
            }
    }
    return null;
}
Also used : FileDataProvider(com.haulmont.cuba.gui.export.FileDataProvider) FileStorageService(com.haulmont.cuba.core.app.FileStorageService) 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