use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileUploadController method upload.
@RequestMapping(value = "/api/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 {
uploadToMiddleware(userSession, is, fd);
saveFileDescriptor(fd);
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
writer.write(fd.getId().toString());
writer.close();
} 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 FileUploadController method getFileDescriptor.
protected FileDescriptor getFileDescriptor(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
if (name == null) {
log.error("'name' request parameter is not specified");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
String ext = request.getParameter("ext");
if (ext == null) {
log.error("'ext' request parameter is not specified");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
String size = request.getParameter("size");
FileDescriptor fd = metadata.create(FileDescriptor.class);
fd.setName(name);
fd.setExtension(ext);
fd.setCreateDate(timeSource.currentTimestamp());
if (size != null) {
fd.setSize(Long.parseLong(size));
}
return fd;
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class RestFileDownloadController method download.
@RequestMapping(value = "/api/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(new LoadContext<>(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("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-Type", getContentType(fd));
response.setHeader("Pragma", "no-cache");
boolean attach = Boolean.valueOf(request.getParameter("a"));
response.setHeader("Content-Disposition", (attach ? "attachment" : "inline") + "; filename=" + fileName);
writeResponse(response, userSession, fd);
} finally {
AppContext.setSecurityContext(null);
}
return null;
}
use of com.haulmont.cuba.core.entity.FileDescriptor in project cuba by cuba-platform.
the class FileUploadController method uploadFile.
/**
* Method for multipart file upload. It expects the file contents to be passed in the part called 'file'
*/
@PostMapping(consumes = "multipart/form-data")
public ResponseEntity<FileInfo> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name, HttpServletRequest request) {
try {
if (Strings.isNullOrEmpty(name)) {
name = file.getOriginalFilename();
}
long size = file.getSize();
FileDescriptor fd = createFileDescriptor(name, size);
InputStream is = file.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 cuba by cuba-platform.
the class AmazonS3FileStorageTest method setUp.
@Before
public void setUp() throws Exception {
fileDescr = new FileDescriptor();
fileDescr.setCreateDate(new Date());
fileDescr.setSize((long) FILE_CONTENT.length());
fileDescr.setName("AmazonFileStorageTest");
fileDescr.setExtension("txt");
fileDescr2 = new FileDescriptor();
fileDescr2.setCreateDate(new Date());
fileDescr2.setSize((long) FILE_CONTENT.length());
fileDescr2.setName("AmazonFileStorageTest");
fileStorageAPI = new AmazonS3FileStorage();
fileStorageAPI.amazonS3Config = AppBeans.get(Configuration.class).getConfig(AmazonS3Config.class);
}
Aggregations