Search in sources :

Example 36 with FileDescriptor

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

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

the class TestListenerUsingEntityManager method onBeforeUpdate.

@Override
public void onBeforeUpdate(Server entity, EntityManager entityManager) {
    EntityManager em = persistence.getEntityManager();
    UUID relatedId = UUID.fromString(entity.getData());
    FileDescriptor related = em.find(FileDescriptor.class, relatedId);
    if (related != null) {
        related.setName("Related updated");
        System.out.println(">>>>> update related: " + relatedId);
    } else
        throw new RuntimeException("Related not found: " + relatedId);
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 38 with FileDescriptor

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

the class TestListenerUsingEntityManager method onBeforeDelete.

@Override
public void onBeforeDelete(Server entity, EntityManager entityManager) {
    EntityManager em = persistence.getEntityManager();
    UUID relatedId = UUID.fromString(entity.getData());
    FileDescriptor related = em.find(FileDescriptor.class, relatedId);
    if (related != null) {
        System.out.println(">>>>> remove related: " + relatedId);
        em.remove(related);
    } else
        throw new RuntimeException("Related entity not found" + relatedId);
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 39 with FileDescriptor

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

the class Emailer method createAttachmentFileDescriptor.

protected FileDescriptor createAttachmentFileDescriptor(SendingAttachment attachment) {
    FileDescriptor contentFile = metadata.create(FileDescriptor.class);
    contentFile.setCreateDate(timeSource.currentTimestamp());
    contentFile.setName(attachment.getName());
    contentFile.setExtension(FilenameUtils.getExtension(attachment.getName()));
    contentFile.setSize((long) attachment.getContent().length);
    return contentFile;
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 40 with FileDescriptor

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

the class Emailer method migrateMessage.

protected void migrateMessage(EntityManager em, SendingMessage msg) {
    msg = em.merge(msg);
    byte[] bodyBytes = bodyTextToBytes(msg);
    FileDescriptor bodyFile = createBodyFileDescriptor(msg, bodyBytes);
    try {
        fileStorage.saveFile(bodyFile, bodyBytes);
    } catch (FileStorageException e) {
        throw new RuntimeFileStorageException("Unable to save file to storage", e);
    }
    em.persist(bodyFile);
    msg.setContentTextFile(bodyFile);
    msg.setContentText(null);
}
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