Example usage for com.mongodb.gridfs GridFS find

List of usage examples for com.mongodb.gridfs GridFS find

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS find.

Prototype

public List<GridFSDBFile> find(final DBObject query) 

Source Link

Document

Finds a list of files matching the given query.

Usage

From source file:com.bluedragon.mongo.gridfs.Find.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    // Get the necessary Mongo references
    DB db = getDB(_session, argStruct);//from  w w  w  . j av  a 2 s  .  c o  m
    GridFS gridfs = getGridFS(_session, argStruct, db);

    // Get the file information
    String filename = getNamedStringParam(argStruct, "filename", null);
    if (filename != null) {
        return toArray(gridfs.find(filename));
    } else {

        cfData mTmp = getNamedParam(argStruct, "query", null);
        if (mTmp != null)
            return toArray(gridfs.find(getDBObject(mTmp)));
    }

    throwException(_session, "Please specify file or a query");
    return null;
}

From source file:com.buzz.buzzdata.MongoBuzz.java

@Override
public InputStream GetImgByBuzz(String buzz_id, int pic_num) {
    InputStream retval = null;/*from  w w  w. j  av  a  2s.  c om*/
    GridFS gridFS = new GridFS(mongoDB);
    ObjectId _id = new ObjectId(buzz_id);
    BasicDBObject buzz_query = new BasicDBObject("BuzzID", _id);
    GridFSDBFile file = null;
    List<GridFSDBFile> files = gridFS.find(buzz_query);
    for (GridFSDBFile file_buzz : files) {
        if ((int) file_buzz.get("PicNum") == pic_num) {
            file = file_buzz;
        }
    }
    retval = file.getInputStream();
    return retval;
}

From source file:com.buzz.buzzdata.MongoBuzz.java

private String getBuzz(BasicDBObject query, Double lat, Double lng) {
    String retval = "";
    DBCollection buzzCollection = mongoDB.getCollection("BuzzInfo");
    DBObject sort = new BasicDBObject();
    sort.put("modified", -1);
    DBCursor cursor = buzzCollection.find(query).sort(sort);
    try {//from  w w w. j  a v  a 2 s . co  m
        while (cursor.hasNext()) {
            //get buzzid
            DBObject buzz_obj = cursor.next();
            ObjectId buzz_id = (ObjectId) buzz_obj.get("_id");
            //get images for buzzid
            GridFS gridFS = new GridFS(mongoDB);
            BasicDBObject check_images = new BasicDBObject("BuzzID", buzz_id);
            List<GridFSDBFile> dbfiles = gridFS.find(check_images);
            String image_links = "";
            for (GridFSDBFile file : dbfiles) {
                String _buzz_id = buzz_id.toString();
                String pic_num = file.get("PicNum").toString();

                if (!image_links.equals("")) {
                    image_links += ",http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                } else {
                    image_links += "http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                }
            }
            String imgs = "\"Images\": " + "\"" + image_links + "\"";
            retval += buzz_obj;
            retval = retval.substring(0, retval.length() - 1);
            retval += ", " + imgs;
            double lat2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(0);
            double lng2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(1);
            double dist = this.haversine(lat, lng, lat2, lng2);
            retval += ", \"Distance\": " + dist;
            String directions_url = "https://maps.google.com/maps?saddr=" + lat + "," + lng + "&daddr=" + lat2
                    + "," + lng2 + "&hl=en&sll=" + lat + "," + lng + "&sspn=" + lat2 + "," + lng2
                    + "&t=m&mra=mift&mrsp=1&sz=5&z=18";
            retval += ", \"Directions\": \"" + directions_url + "\"";
            retval += "},";
        }
    } catch (Exception exc) {
    } finally {
        cursor.close();
    }
    retval = retval.substring(0, retval.length() - 1);
    retval = "callback({\"Buzzes\": [" + retval + "]})";
    return retval;
}

From source file:com.fileoperations.FolderDownload.java

public File makeFolder() throws IOException {
    try {//  w ww.jav  a 2  s.  c om
        String mongoFolder = parentPath + pathMerger + folderName;
        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);
            File zip = new File(folderName + ".zip");
            ZipOutputStream folderToZip = new ZipOutputStream(new FileOutputStream(zip));

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                InputStream in = indivFile.getInputStream();
                int data = in.read();
                while (data >= 0) {
                    out.write((char) data);
                    data = in.read();
                }

                out.flush();
                String zipPath;
                zipPath = indivFile.get("path").toString() + pathMerger + indivFile.getFilename();
                zipPath = zipPath.replaceFirst(parentPath, "");
                zipPath = zipPath.replaceFirst(pathMerger, "");
                ZipEntry add = new ZipEntry(zipPath);
                folderToZip.putNextEntry(add);
                folderToZip.write(out.toByteArray(), 0, out.toByteArray().length);

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor emptyFolder = collection.find(toFindAllEmptyFilesInFolder);
            List<String> emptyFolderPathToAdd = new ArrayList<String>();
            while (emptyFolder.hasNext()) {
                DBObject temp = emptyFolder.next();
                BasicDBObject isEmpty = new BasicDBObject();
                isEmpty.put("parent", temp.get("_id").toString());

                if (!collection.find(isEmpty).hasNext()) {
                    if (!temp.get("_id").toString().contains(".")) {
                        emptyFolderPathToAdd.add(temp.get("_id").toString());
                    }
                }

            }
            for (int i = 0; i < emptyFolderPathToAdd.size(); i++) {
                String temp = emptyFolderPathToAdd.get(i).replaceFirst(parentPath, "");
                temp = temp.replaceFirst(pathMerger, "");
                ZipEntry add = new ZipEntry(temp + pathMerger);
                folderToZip.putNextEntry(add);

            }

            folderToZip.closeEntry();
            folderToZip.close();
            return zip;
        } else {
            return null;
        }

    } finally {
        mymongo.closeConnection();
    }

}

From source file:com.fileoperations.FolderDownload.java

public Boolean copyFolder(String newPath) throws IOException {
    try {/*  ww  w .  ja va2 s . c o m*/
        String mongoFolder = parentPath + pathMerger + folderName;

        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {
            BasicDBObject newquery = new BasicDBObject();
            newquery.put("_id", newPath + pathMerger + folderName);
            if (collection.find(newquery).hasNext()) {
                return false;
            }

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                InputStream data = indivFile.getInputStream();
                String zipPath;
                zipPath = indivFile.get("path").toString();
                String tempFileName = indivFile.getFilename();

                zipPath = zipPath.replaceFirst(parentPath, newPath);
                BasicDBObject document = new BasicDBObject();
                document.append("_id", zipPath + pathMerger + tempFileName);
                document.append("folder", "0");
                document.append("parent", zipPath);
                document.append("name", tempFileName);
                int index = tempFileName.lastIndexOf(".");
                document.append("type", tempFileName.substring(index));
                collection.insert(document);
                GridFSInputFile inputFile = fileStore.createFile(data);
                inputFile.setId(zipPath + pathMerger + tempFileName);
                inputFile.put("path", zipPath);
                inputFile.setFilename(tempFileName);
                inputFile.save();

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder);
            while (allFolders.hasNext()) {
                DBObject temp = allFolders.next();
                if (temp.get("folder").toString().equals("1")) {
                    String tempPath = temp.get("parent").toString().replaceFirst(parentPath, newPath);
                    BasicDBObject document = new BasicDBObject();
                    document.put("_id", tempPath + pathMerger + temp.get("name"));
                    document.put("folder", "1");
                    document.put("name", temp.get("name"));
                    document.put("parent", tempPath);
                    document.put("type", "1");
                    collection.insert(document);

                }
            }

            return true;
        } else {
            return false;
        }

    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.FolderDownload.java

public Boolean renameFolder(String newName) throws IOException {
    try {// w ww  .  j  a  v a  2s.c  om
        String mongoFolder = parentPath + pathMerger + folderName;
        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {
            BasicDBObject newquery = new BasicDBObject();
            newquery.put("_id", parentPath + pathMerger + newName);
            if (collection.find(newquery).hasNext()) {
                return false;
            }
            BasicDBObject doc = new BasicDBObject();
            doc.put("_id", parentPath + pathMerger + newName);
            doc.put("folder", "1");
            doc.put("name", newName);
            doc.put("parent", parentPath);
            doc.put("type", "1");

            collection.insert(doc);

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                InputStream data = indivFile.getInputStream();
                String zipPath;
                zipPath = indivFile.get("path").toString();
                String tempFileName = indivFile.getFilename();
                zipPath = zipPath.replaceFirst(parentPath + pathMerger + folderName,
                        parentPath + pathMerger + newName);
                BasicDBObject document = new BasicDBObject();
                document.append("_id", zipPath + pathMerger + tempFileName);
                document.append("folder", "0");
                document.append("parent", zipPath);
                document.append("name", tempFileName);
                int index = tempFileName.lastIndexOf(".");
                document.append("type", tempFileName.substring(index));
                collection.insert(document);
                GridFSInputFile inputFile = fileStore.createFile(data);
                inputFile.setId(zipPath + pathMerger + tempFileName);
                inputFile.put("path", zipPath);
                inputFile.setFilename(tempFileName);
                inputFile.save();

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder);
            while (allFolders.hasNext()) {
                DBObject temp = allFolders.next();
                if (temp.get("folder").toString().equals("1")) {
                    String tempPath = temp.get("parent").toString();
                    tempPath = tempPath.replaceFirst(parentPath + pathMerger + folderName,
                            parentPath + pathMerger + newName);
                    BasicDBObject updocument = new BasicDBObject();
                    updocument.put("_id", tempPath + pathMerger + temp.get("name"));
                    updocument.put("folder", "1");
                    updocument.put("name", temp.get("name"));
                    updocument.put("parent", tempPath);
                    updocument.put("type", "1");
                    collection.insert(updocument);

                }
            }

            return true;
        } else {
            return false;
        }

    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_LibraryJars.java

License:Apache License

protected static void copyFile(final String binary_id, final String path, final IStorageService aleph2_fs,
        final GridFS share_fs) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        final GridFSDBFile file = share_fs.find(new ObjectId(binary_id));
        file.writeTo(out);/*from ww w . java  2s  .co  m*/
        final FileContext fs = aleph2_fs.getUnderlyingPlatformDriver(FileContext.class, Optional.empty()).get();
        final Path file_path = fs.makeQualified(new Path(path));
        try (FSDataOutputStream outer = fs.create(file_path,
                EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE),
                org.apache.hadoop.fs.Options.CreateOpts.createParent())) {
            outer.write(out.toByteArray());
        }
    }
}

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public InputStream getFileAsStream(String fileId) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    GridFSDBFile savedFile = gridFs.find(new ObjectId(fileId));

    return savedFile.getInputStream();
}

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public GridFSDBFile getFile(String fileId) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    return gridFs.find(new ObjectId(fileId));
}

From source file:de.iew.imageread.Main.java

License:Apache License

public void run() {
    try {/*from  www.j ava 2 s  . c o m*/
        this.imageOutputDir = System.getProperty("java.io.tmpdir");

        File outputBase = new File(this.imageOutputDir);
        testAndCreateDirectory(outputBase);

        MongoClient mongoClient = new MongoClient(this.mongohost, this.mongoport);

        DB db = mongoClient.getDB(this.mongodb);

        GridFS gridFS = new GridFS(db);

        DBCursor cursor = gridFS.getFileList(this.queryFromOptions()).sort(new BasicDBObject("uploadDate", -1));

        int imageNumber = 0;
        while (cursor.hasNext()) {
            DBObject fileObject = cursor.next();

            GridFSDBFile file = gridFS.find((ObjectId) fileObject.get("_id"));
            printGridFSDBFile(file);

            if (writeImageFile(outputBase, imageNumber, file)) {
                imageNumber++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}