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);
}
}
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);
}
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);
}
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;
}
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);
}
Aggregations