Example usage for com.mongodb.gridfs GridFS GridFS

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

Introduction

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

Prototype

public GridFS(final DB db, final String bucket) 

Source Link

Document

Creates a GridFS instance for the specified bucket in the given database.

Usage

From source file:mx.org.cedn.avisosconagua.mongo.MongoInterface.java

License:Open Source License

/**
 * Gets a GridFS object to store an image.
 * @return GridFS object
 */
public GridFS getGeneratedFS() {
    return new GridFS(mongoDB, GENERATED_COL);
}

From source file:mx.org.cedn.avisosconagua.mongo.MongoInterface.java

License:Open Source License

/**
 * Gets a GridFS object for the provided type of operation.
 * @param type type of operation (get or store the image)
 * @return GridFS object//from w  w w .j  a  va2  s  . c o m
 */
public GridFS getGridFS(String type) {
    String collection = GENERATED_COL;
    if ("getImage".equals(type)) {
        collection = IMAGES_COL;
    }
    return new GridFS(mongoDB, collection);
}

From source file:mytubermiserver.mongo.GridFileSystem.java

public void sendVideo(String fileName, InputStream videoStream)
        throws UnknownHostException, MongoException, IOException {
    Mongo mongo = new Mongo(hostAddress, portAddress);

    DB db = mongo.getDB("MyTube");

    // create a "video" namespace
    GridFS gfsPhoto = new GridFS(db, "video");

    //get the file from stream
    GridFSInputFile gfsFile = gfsPhoto.createFile(videoStream);

    // set a new filename for identify purpose
    gfsFile.setFilename(fileName);//w ww  .  ja v a  2s  .com

    // save the image file into mongoDB
    gfsFile.save();
    videoStream.close();
}

From source file:mytubermiserver.mongo.GridFileSystem.java

public InputStream receiveVideo(String fileName) throws UnknownHostException, MongoException, IOException {
    Mongo mongo = new Mongo(hostAddress, portAddress);
    DB db = mongo.getDB("MyTube");

    // create a "video" namespace
    GridFS gfsPhoto = new GridFS(db, "video");

    // get image file by it's filename
    GridFSDBFile videoForOutput = gfsPhoto.findOne(fileName);
    return videoForOutput.getInputStream();

}

From source file:mytubermiserver.mongo.GridFileSystem.java

public void RemoveVideo(String fileName) throws UnknownHostException, MongoException, IOException {
    Mongo mongo = new Mongo(hostAddress, portAddress);

    DB db = mongo.getDB("MyTube");
    // create a "video" namespace
    GridFS gfsVideo = new GridFS(db, "video");
    // remove the image file from mongoDB
    gfsVideo.remove(fileName);/*from  w  w  w. j  ava  2s  .  co m*/
}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPost()");

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*from w w w.  j a va 2s .com*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old == null) {
        error(res, SC_NOT_FOUND, Status.get("file doe not exists, use PUT"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
    }
    os.close();

    is.close();

    out_json(req, Status.OK);

}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPut()");

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*from   w w w  . j a  va 2s. com*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old != null) {
        error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
    }
    os.flush();
    os.close();

    is.close();

    out_json(req, Status.OK);

}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doDelete()");

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*from   ww  w .j a va  2s. c  om*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    fs.remove(file_name);

    out_json(req, Status.OK);

}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*from   ww w.  jav a 2  s.  c  o  m*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String op = req.getParameter("op");
    if (op == null)
        op = "get";

    StringBuilder buf = tl.get();
    // reset buf
    buf.setLength(0);

    // list
    if ("get".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("file does not exists"));
            return;
        }

        res.setContentLength((int) db_file.getLength());
        String ct = db_file.getContentType();
        if (ct != null)
            res.setContentType(ct);
        OutputStream os = res.getOutputStream();
        long l;
        while ((l = db_file.writeTo(os)) > 0)
            ;
        os.flush();
        os.close();

    }
    // list
    else if ("list".equals(op)) {

        DBCursor c = fs.getFileList();
        if (c == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        int no = 0;
        buf.append("[");
        while (c.hasNext()) {

            DBObject o = c.next();
            JSON.serialize(o, buf);
            buf.append(",");
            no++;

        }

        if (no > 0)
            buf.setCharAt(buf.length() - 1, ']');
        else
            buf.append(']');

        out_str(req, buf.toString(), "application/json");

    }
    // info
    else if ("info".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        buf.append("{");
        buf.append(String.format("\"ContentType\":%s,", db_file.getContentType()));
        buf.append(String.format("\"Length\":%d,", db_file.getLength()));
        buf.append(String.format("\"MD5\":%s", db_file.getMD5()));
        buf.append("}");

        out_str(req, buf.toString(), "application/json");

    } else
        res.sendError(SC_BAD_REQUEST);

}

From source file:net.tooan.ynpay.third.mongodb.fs.BuguFS.java

License:Apache License

private void init(String bucketName, long chunkSize) {
    this.bucketName = bucketName;
    this.chunkSize = chunkSize;
    DB db = null;//from   w  w  w  .j a  v  a2 s . c  o m
    try {
        db = BuguConnection.getInstance().getDB();
    } catch (DBConnectionException ex) {
        logger.error(ex.getMessage(), ex);
    }
    fs = new GridFS(db, bucketName);
    files = db.getCollection(bucketName + ".files");
}