Example usage for org.springframework.data.mongodb.gridfs GridFsResource contentLength

List of usage examples for org.springframework.data.mongodb.gridfs GridFsResource contentLength

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.gridfs GridFsResource contentLength.

Prototype

@Override
    public long contentLength() throws IOException 

Source Link

Usage

From source file:piecework.util.ContentUtility.java

public static ContentResource toContent(GridFsResource resource) throws IOException {
    String resourceId = resource.getId().toString();

    return new BasicContentResource.Builder().contentId(resourceId).contentType(resource.getContentType())
            .filename(resource.getFilename()).location(resource.getFilename())
            .inputStream(resource.getInputStream()).lastModified(resource.lastModified())
            .length(Long.valueOf(resource.contentLength())).build();
}

From source file:com.greglturnquist.springagram.fileservice.mongodb.ApplicationController.java

@RequestMapping(method = RequestMethod.GET, value = "/files/{filename}")
public ResponseEntity<?> getFile(@PathVariable String filename) {

    GridFsResource file = this.fileService.findOne(filename);

    if (file == null) {
        return ResponseEntity.notFound().build();
    }/*w  ww.j a va2s .c o  m*/

    try {
        return ResponseEntity.ok().contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType(file.getContentType()))
                .body(new InputStreamResource(file.getInputStream()));
    } catch (IOException e) {
        return ResponseEntity.badRequest().body("Couldn't process the request");
    }
}