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 ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserSession userSession = getSession(request, response);
if (userSession == null) {
error(response);
return null;
}
AppContext.setSecurityContext(new SecurityContext(userSession));
try {
UUID fileId;
try {
fileId = UUID.fromString(request.getParameter("f"));
} catch (Exception e) {
log.error(e.toString());
error(response);
return null;
}
FileDescriptor fd = dataService.load(LoadContext.create(FileDescriptor.class).setId(fileId));
if (fd == null) {
log.warn("Unable to find file with id {}", fileId);
error(response);
return null;
}
String fileName = URLEncodeUtils.encodeUtf8(fd.getName());
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
response.setDateHeader(HttpHeaders.EXPIRES, 0);
response.setHeader(HttpHeaders.CONTENT_TYPE, getContentType(fd));
response.setHeader(HttpHeaders.PRAGMA, "no-cache");
boolean attach = Boolean.valueOf(request.getParameter("a"));
response.setHeader("Content-Disposition", (attach ? "attachment" : "inline") + "; filename=" + fileName);
downloadFromMiddlewareAndWriteResponse(fd, response);
} finally {
AppContext.setSecurityContext(null);
}
return null;
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileDownloadController method downloadFile.
@GetMapping("/{fileDescriptorId}")
public void downloadFile(@PathVariable String fileDescriptorId, @RequestParam(required = false) Boolean attachment, HttpServletResponse response) {
UUID uuid;
try {
uuid = UUID.fromString(fileDescriptorId);
} catch (IllegalArgumentException e) {
throw new RestAPIException("Invalid entity ID", String.format("Cannot convert %s into valid entity ID", fileDescriptorId), HttpStatus.BAD_REQUEST);
}
LoadContext<FileDescriptor> ctx = LoadContext.create(FileDescriptor.class).setId(uuid);
FileDescriptor fd = dataService.load(ctx);
if (fd == null) {
throw new RestAPIException("File not found", "File not found. Id: " + fileDescriptorId, HttpStatus.NOT_FOUND);
}
try {
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", getContentType(fd));
response.setHeader("Content-Disposition", (BooleanUtils.isTrue(attachment) ? "attachment" : "inline") + "; filename=\"" + fd.getName() + "\"");
downloadFromMiddlewareAndWriteResponse(fd, response);
} catch (Exception e) {
log.error("Error on downloading the file {}", fileDescriptorId, e);
throw new RestAPIException("Error on downloading the file", "", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileUploadController method createFileDescriptor.
protected FileDescriptor createFileDescriptor(@Nullable String fileName, long size) {
FileDescriptor fd = metadata.create(FileDescriptor.class);
if (Strings.isNullOrEmpty(fileName)) {
fileName = fd.getId().toString();
}
fd.setName(fileName);
fd.setExtension(FilenameUtils.getExtension(fileName));
fd.setSize(size);
fd.setCreateDate(timeSource.currentTimestamp());
return fd;
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileUploadController method uploadFile.
/**
* Method for simple file upload. File contents are placed in the request body. Optional file name parameter is
* passed as a query param.
*/
@PostMapping(consumes = "!multipart/form-data")
public ResponseEntity<FileInfo> uploadFile(HttpServletRequest request, @RequestParam(required = false) String name) {
try {
String contentLength = request.getHeader("Content-Length");
long size = 0;
try {
size = Long.parseLong(contentLength);
} catch (NumberFormatException ignored) {
}
FileDescriptor fd = createFileDescriptor(name, size);
ServletInputStream is = request.getInputStream();
uploadToMiddleware(is, fd);
saveFileDescriptor(fd);
return createFileInfoResponseEntity(request, fd);
} catch (Exception e) {
log.error("File upload failed", e);
throw new RestAPIException("File upload failed", "File upload failed", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project documentation by cuba-platform.
the class FileLoaderScreen method onButtonInClick.
public void onButtonInClick() {
byte[] bytes = textAreaIn.getRawValue().getBytes();
fileDescriptor = metadata.create(FileDescriptor.class);
fileDescriptor.setName("Input.txt");
fileDescriptor.setExtension("txt");
fileDescriptor.setSize((long) bytes.length);
fileDescriptor.setCreateDate(new Date());
try {
fileLoader.saveStream(fileDescriptor, () -> new ByteArrayInputStream(bytes));
} catch (FileStorageException e) {
throw new RuntimeException(e);
}
dataManager.commit(fileDescriptor);
}
Aggregations