Search in sources :

Example 21 with FileDescriptor

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

the class ResendMessage method retrieveContent.

protected byte[] retrieveContent(SendingAttachment sendingAttachment) {
    byte[] content = sendingAttachment.getContent();
    if (content != null) {
        return content;
    }
    FileDescriptor contentFile = dataManager.load(FileDescriptor.class).query("select e.contentFile from sys$SendingAttachment e where e.id = :id").parameter("id", sendingAttachment.getId()).one();
    try (InputStream inputStream = fileLoader.openStream(contentFile)) {
        content = IOUtils.toByteArray(inputStream);
    } catch (FileStorageException | IOException e) {
        throw new RuntimeException("Can't read content from message attachment", e);
    }
    return content;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 22 with FileDescriptor

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

the class Emailer method migrateAttachment.

protected void migrateAttachment(EntityManager em, SendingAttachment attachment) {
    attachment = em.merge(attachment);
    FileDescriptor contentFile = createAttachmentFileDescriptor(attachment);
    try {
        fileStorage.saveFile(contentFile, attachment.getContent());
    } catch (FileStorageException e) {
        throw new RuntimeException("Unable to save file to storage", e);
    }
    em.persist(contentFile);
    attachment.setContentFile(contentFile);
    attachment.setContent(null);
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 23 with FileDescriptor

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

the class Emailer method createBodyFileDescriptor.

protected FileDescriptor createBodyFileDescriptor(SendingMessage message, byte[] bodyBytes) {
    FileDescriptor contentTextFile = metadata.create(FileDescriptor.class);
    contentTextFile.setCreateDate(timeSource.currentTimestamp());
    contentTextFile.setName("Email_" + message.getId() + "." + BODY_FILE_EXTENSION);
    contentTextFile.setExtension(BODY_FILE_EXTENSION);
    contentTextFile.setSize((long) bodyBytes.length);
    return contentTextFile;
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 24 with FileDescriptor

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

the class WebFileUploadField method getFileContent.

@Override
public InputStream getFileContent() {
    if (contentProvider != null) {
        return contentProvider.get();
    }
    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 = beanLocator.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) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 25 with FileDescriptor

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

the class WebFileUploadField method initComponent.

protected void initComponent() {
    component.addFileNameClickListener(e -> {
        FileDescriptor value = getValue();
        if (value == null) {
            return;
        }
        switch(mode) {
            case MANUAL:
                String name = getFileName();
                String fileName = StringUtils.isEmpty(name) ? value.getName() : name;
                exportDisplay.show(this::getFileContent, fileName);
                break;
            case IMMEDIATE:
                exportDisplay.show(value);
                break;
        }
    });
    component.setClearButtonListener(this::clearButtonClicked);
    component.setRequiredError(null);
    applyPermissions();
}
Also used : 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