use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class DesktopFileUploadField method saveFile.
protected void saveFile(FileDescriptor fileDescriptor) {
switch(mode) {
case MANUAL:
setValue(fileDescriptor);
break;
case IMMEDIATE:
BackgroundTask<Long, FileDescriptor> uploadProgress = new BackgroundTask<Long, FileDescriptor>(2400, getFrame()) {
@Override
public Map<String, Object> getParams() {
return ParamsMap.of("fileId", fileId, "fileName", getFileName());
}
@Override
public FileDescriptor run(final TaskLifeCycle<Long> taskLifeCycle) throws Exception {
return fileUploading.putFileIntoStorage(taskLifeCycle);
}
@Override
public void done(FileDescriptor result) {
FileDescriptor descriptor = commitFileDescriptor(result);
setValue(descriptor);
}
};
long fileSize = fileUploading.getFile(fileId).length();
BackgroundWorkProgressWindow.show(uploadProgress, messages.getMainMessage("FileUploadField.uploadingFile"), null, fileSize, true, true);
break;
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class DesktopFileUploadField method initImpl.
protected void initImpl() {
impl = new CubaFileUploadWrapper(uploadButton);
impl.setFileNameButtonClickListener(() -> {
FileDescriptor value = getValue();
if (value == null)
return;
switch(mode) {
case MANUAL:
String name = getFileName();
String fileName1 = StringUtils.isEmpty(name) ? value.getName() : name;
exportDisplay.show(DesktopFileUploadField.this::getFileContent, fileName1);
break;
case IMMEDIATE:
exportDisplay.show(value);
}
});
impl.setClearButtonListener(this::clearButtonClicked);
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileDownloadController method getFileDescriptor.
protected FileDescriptor getFileDescriptor(HttpServletRequest request, HttpServletResponse response) throws IOException {
UUID fileId;
try {
fileId = UUID.fromString(request.getParameter("f"));
} catch (Exception e) {
log.error("Error parsing fileId from URL param", e);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
FileDescriptor fileDescriptor = dataService.load(new LoadContext<>(FileDescriptor.class).setId(fileId));
if (fileDescriptor == null)
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return fileDescriptor;
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileDownloadController method download.
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserSession userSession = getSession(request, response);
if (userSession == null)
return;
AppContext.setSecurityContext(new SecurityContext(userSession));
try {
File file = null;
FileDescriptor fd = null;
if (request.getParameter("p") != null)
file = getFile(request, response);
else
fd = getFileDescriptor(request, response);
if (fd == null && file == null)
return;
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setIntHeader("Expires", -1);
response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE);
InputStream is = null;
ServletOutputStream os = null;
try {
is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (FileStorageException e) {
log.error("Unable to download file", e);
response.sendError(e.getType().getHttpStatus());
} catch (Exception ex) {
log.error("Unable to download file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
} finally {
AppContext.setSecurityContext(null);
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class TestListenerUsingEntityManager method onBeforeInsert.
@Override
public void onBeforeInsert(Server entity, EntityManager entityManager) {
EntityManager em = persistence.getEntityManager();
FileDescriptor related = new FileDescriptor();
related.setName("Related");
System.out.println(">>>>> persist related: " + related.getId());
em.persist(related);
entity.setData(related.getId().toString());
}
Aggregations