Example usage for com.mongodb.client.gridfs.model GridFSFile getLength

List of usage examples for com.mongodb.client.gridfs.model GridFSFile getLength

Introduction

In this page you can find the example usage for com.mongodb.client.gridfs.model GridFSFile getLength.

Prototype

public long getLength() 

Source Link

Document

The length, in bytes of this file

Usage

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

private JSONObject executeFind(GridFSBucket gridFS, String query, String sortBy, String limit, String skip)
        throws JSONException {
    Document queryObj = Document.parse(query);
    Document sortObj = Document.parse(sortBy);
    int filesLimit = Integer.parseInt(limit);
    int filesSkip = Integer.parseInt(skip);
    // Partial Keys cant be fetched for a file

    MongoCursor<GridFSFile> it = gridFS.find(queryObj).sort(sortObj).skip(filesSkip).limit(filesLimit)
            .iterator();//from w ww  . j a v  a2s  . c  o  m

    JSONArray fileList = new JSONArray();
    while (it.hasNext()) {
        GridFSFile fsFile = it.next();

        JSONObject file = new JSONObject();

        file.put("_id", fsFile.getId().asObjectId().getValue());
        file.put("fileName", fsFile.getFilename());
        file.put("length", fsFile.getLength());
        file.put("chunkSize", fsFile.getChunkSize());
        file.put("uploadDate", fsFile.getUploadDate());
        file.put("md5", fsFile.getMD5());
        if (fsFile.getMetadata() != null) {
            file.put("metadata", fsFile.getMetadata());
        }

        fileList.put(file);

    }
    JSONObject result = new JSONObject();
    long count = fileList.length();
    result.put("documents", fileList);
    result.put("editable", true);
    result.put("count", count);
    return result;
}

From source file:org.restheart.handlers.files.GetFileBinaryHandler.java

License:Open Source License

private void sendBinaryContent(final GridFSBucket gridFSBucket, final GridFSFile file,
        final HttpServerExchange exchange) throws IOException {
    LOGGER.trace("Filename = {}", file.getFilename());
    LOGGER.trace("Content length = {}", file.getLength());

    if (file.getMetadata() != null && file.getMetadata().get("contentType") != null) {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
                file.getMetadata().get("contentType").toString());
    } else if (file.getExtraElements() != null && file.getExtraElements().get("contentType") != null) {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
                file.getExtraElements().get("contentType").toString());
    } else {//  ww  w.  jav  a2  s.c om
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, APPLICATION_OCTET_STREAM);
    }

    exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, file.getLength());

    exchange.getResponseHeaders().put(Headers.CONTENT_DISPOSITION,
            String.format("inline; filename=\"%s\"", extractFilename(file)));

    exchange.getResponseHeaders().put(Headers.CONTENT_TRANSFER_ENCODING, CONTENT_TRANSFER_ENCODING_BINARY);

    ResponseHelper.injectEtagHeader(exchange, file.getMetadata());

    exchange.setStatusCode(HttpStatus.SC_OK);

    gridFSBucket.downloadToStream(file.getId().asObjectId().getValue(), exchange.getOutputStream());

    exchange.endExchange();
}