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

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

Introduction

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

Prototype

public BsonValue getId() 

Source Link

Document

The BsonValue id for 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  a2  s  . 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.hibernate.ogm.datastore.mongodb.binarystorage.GridFSStorageManager.java

License:LGPL

private void deleteExistingContent(String fieldName, Object documentId, GridFSBucket gridFSFilesBucket) {
    GridFSFindIterable results = gridFSFilesBucket
            .find(Filters.and(Filters.eq("filename", fileName(fieldName, documentId))));
    try (MongoCursor<GridFSFile> iterator = results.iterator()) {
        while (iterator.hasNext()) {
            GridFSFile next = iterator.next();
            gridFSFilesBucket.delete(next.getId());
        }//from w ww.  java 2 s  .  co m
    }
}

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 {//w w w.j  a  v  a 2s. co m
        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();
}

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

License:Open Source License

private String extractFilename(final GridFSFile dbsfile) {
    return dbsfile.getFilename() != null ? dbsfile.getFilename() : dbsfile.getId().toString();
}