Search in sources :

Example 41 with FileDescriptor

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

the class Emailer method persistSendingMessage.

protected void persistSendingMessage(EntityManager em, SendingMessage message, MessagePersistingContext context) throws FileStorageException {
    boolean useFileStorage = config.isFileStorageUsed();
    if (useFileStorage) {
        byte[] bodyBytes = bodyTextToBytes(message);
        FileDescriptor contentTextFile = createBodyFileDescriptor(message, bodyBytes);
        fileStorage.saveFile(contentTextFile, bodyBytes);
        context.files.add(contentTextFile);
        em.persist(contentTextFile);
        message.setContentTextFile(contentTextFile);
        message.setContentText(null);
    }
    em.persist(message);
    for (SendingAttachment attachment : message.getAttachments()) {
        if (useFileStorage) {
            FileDescriptor contentFile = createAttachmentFileDescriptor(attachment);
            fileStorage.saveFile(contentFile, attachment.getContent());
            context.files.add(contentFile);
            em.persist(contentFile);
            attachment.setContentFile(contentFile);
            attachment.setContent(null);
        }
        em.persist(attachment);
    }
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 42 with FileDescriptor

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

the class FileUploading method getFileDescriptor.

@Override
public FileDescriptor getFileDescriptor(UUID fileId, String name) {
    File file = getFile(fileId);
    if (file == null) {
        return null;
    }
    Metadata metadata = AppBeans.get(Metadata.NAME);
    FileDescriptor fDesc = metadata.create(FileDescriptor.class);
    fDesc.setSize(file.length());
    fDesc.setExtension(FilenameUtils.getExtension(name));
    fDesc.setName(name);
    fDesc.setCreateDate(timeSource.currentTimestamp());
    return fDesc;
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 43 with FileDescriptor

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

the class FileUploading method putFileIntoStorage.

@Override
public FileDescriptor putFileIntoStorage(final TaskLifeCycle<Long> taskLifeCycle) throws FileStorageException, InterruptedException {
    checkNotNullArgument(taskLifeCycle);
    UUID fileId = (UUID) taskLifeCycle.getParams().get("fileId");
    String fileName = (String) taskLifeCycle.getParams().get("fileName");
    checkNotNull(fileId);
    checkNotNull(fileName);
    UploadToStorageProgressListener progressListener = (fileId1, uploadedBytes, totalBytes) -> taskLifeCycle.publish(uploadedBytes);
    FileDescriptor fileDescriptor = getFileDescriptor(fileId, fileName);
    uploadFileIntoStorage(fileId, fileDescriptor, progressListener);
    deleteFile(fileId);
    return fileDescriptor;
}
Also used : TaskLifeCycle(com.haulmont.cuba.gui.executors.TaskLifeCycle) java.util(java.util) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SimpleDateFormat(java.text.SimpleDateFormat) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) Supplier(java.util.function.Supplier) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) IOUtils(org.apache.commons.io.IOUtils) Component(org.springframework.stereotype.Component) java.io(java.io) LazySupplier(com.haulmont.bali.util.StreamUtils.LazySupplier) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) FilenameUtils(org.apache.commons.io.FilenameUtils) Nullable(javax.annotation.Nullable) DateFormat(java.text.DateFormat) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 44 with FileDescriptor

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

the class FileBrowser method init.

@Override
public void init(Map<String, Object> params) {
    super.init(params);
    filesTable.addAction(new ItemTrackingAction("download").withCaption(getMessage("download")).withHandler(event -> {
        FileDescriptor fileDescriptor = filesTable.getSingleSelected();
        if (fileDescriptor != null) {
            exportDisplay.show(fileDescriptor, null);
        }
    }));
    BaseAction multiUploadAction = new BaseAction("multiupload").withCaption(getMessage("multiupload")).withHandler(event -> {
        if (!security.isEntityOpPermitted(FileDescriptor.class, EntityOp.READ)) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, FileDescriptor.class.getSimpleName());
        }
        Window window = openWindow("multiuploadDialog", OpenType.DIALOG);
        window.addCloseListener(actionId -> {
            if (COMMIT_ACTION_ID.equals(actionId)) {
                Collection<FileDescriptor> items = ((MultiUploader) window).getFiles();
                for (FileDescriptor fdesc : items) {
                    boolean modified = filesDs.isModified();
                    filesDs.addItem(fdesc);
                    ((DatasourceImplementation) filesDs).setModified(modified);
                }
                filesTable.focus();
            }
        });
    });
    multiUploadAction.setEnabled(security.isEntityOpPermitted(FileDescriptor.class, EntityOp.CREATE));
    multiUploadBtn.setAction(multiUploadAction);
}
Also used : Security(com.haulmont.cuba.core.global.Security) Table(com.haulmont.cuba.gui.components.Table) Collection(java.util.Collection) Button(com.haulmont.cuba.gui.components.Button) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) PermissionType(com.haulmont.cuba.security.entity.PermissionType) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) Inject(javax.inject.Inject) AbstractLookup(com.haulmont.cuba.gui.components.AbstractLookup) EntityOp(com.haulmont.cuba.security.entity.EntityOp) Window(com.haulmont.cuba.gui.components.Window) Map(java.util.Map) BaseAction(com.haulmont.cuba.gui.components.actions.BaseAction) AccessDeniedException(com.haulmont.cuba.core.global.AccessDeniedException) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Window(com.haulmont.cuba.gui.components.Window) AccessDeniedException(com.haulmont.cuba.core.global.AccessDeniedException) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) BaseAction(com.haulmont.cuba.gui.components.actions.BaseAction) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 45 with FileDescriptor

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

the class SendingMessageBrowser method getFileDescriptor.

protected FileDescriptor getFileDescriptor(SendingAttachment attachment) throws FileStorageException {
    UUID uuid = fileUploading.saveFile(attachment.getContent());
    FileDescriptor fileDescriptor = fileUploading.getFileDescriptor(uuid, attachment.getName());
    fileUploading.putFileIntoStorage(uuid, fileDescriptor);
    return dataSupplier.commit(fileDescriptor);
}
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