Search in sources :

Example 26 with 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 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;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with FileDescriptor

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);
    }
}
Also used : RestAPIException(com.haulmont.restapi.exception.RestAPIException) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RestAPIException(com.haulmont.restapi.exception.RestAPIException) IOException(java.io.IOException)

Example 28 with FileDescriptor

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;
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 29 with FileDescriptor

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);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) RestAPIException(com.haulmont.restapi.exception.RestAPIException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RestAPIException(com.haulmont.restapi.exception.RestAPIException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 30 with FileDescriptor

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) Date(java.util.Date)

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