Example usage for com.mongodb.gridfs GridFSInputFile put

List of usage examples for com.mongodb.gridfs GridFSInputFile put

Introduction

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

Prototype

@Override
    public Object put(final String key, final Object v) 

Source Link

Usage

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

protected void writeFile(InputStream ins, MFile mf, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();/*from   ww w. j  a v a  2 s.  c  o m*/
    GridFSInputFile gfsInput;
    gfsInput = new GridFS(db, tableName).createFile(ins);
    DBCollection col = db.getCollection(tableName + ".files");
    col.setWriteConcern(WriteConcern.SAFE);
    gfsInput.setFilename(mf.getFilename());
    gfsInput.put(FIELD_STORAGE_KEY, mf.getStorageKey());
    gfsInput.put(FIELD_DOC_ID, mf.getDocid());
    gfsInput.put(FILED_VERSION_ID, mf.getVid());
    gfsInput.put(FIELD_APP_ID, mf.getAppid());
    gfsInput.put(FILED_IS_PUB, getIsPubStatus(mf.getIsPub()));
    gfsInput.setContentType(mf.getContentType());
    gfsInput.save();
    db.requestDone();
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

protected void writeTrivialFile(InputStream ins, MTrivialFile mtf, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();/*w w w .ja v  a  2 s  . c o  m*/
    GridFSInputFile gfsInput;
    DBCollection col = db.getCollection(tableName + ".files");
    col.setWriteConcern(WriteConcern.SAFE);
    gfsInput = new GridFS(db, tableName).createFile(ins);
    gfsInput.setFilename(mtf.getFileName());
    gfsInput.put(FIELD_STORAGE_KEY, mtf.getStorageKey());
    gfsInput.put(FIELD_SPACE_NAME, mtf.getSpaceName());
    gfsInput.setContentType(mtf.getContentType());
    gfsInput.save();
    db.requestDone();
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

private void setAttributes(GridFSInputFile f, Map<String, Object> attributes) {
    if (attributes != null) {
        for (Entry<String, Object> entry : attributes.entrySet()) {
            f.put(entry.getKey(), entry.getValue());
        }/*from ww  w. j  a  v a2 s .  c o m*/
    }
}

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

@Override
public void Insert(String userid, String header, String content, Double lat, Double lng, String tags,
        String[] files) {/* w  w  w.j  a va 2  s.  c om*/
    BasicDBObject document = new BasicDBObject();
    document.put("userid", userid);
    document.put("header", header);
    document.put("content", content);
    document.put("tags", tags.split(","));
    document.put("created", (new Date()));
    document.put("modified", (new Date()));
    BasicDBObject lng_obj = new BasicDBObject();
    lng_obj.put("lng", lng);
    BasicDBObject lat_obj = new BasicDBObject();
    lat_obj.put("lat", lat);
    document.put("loc", (new Double[] { lng, lat }));
    document.put("FilesCount", files.length);
    DBCollection coll = mongoDB.getCollection("BuzzInfo");
    coll.insert(document);
    ObjectId buzz_id = (ObjectId) document.get("_id");
    int i = 0;
    for (String file : files) {
        try {
            GridFS gridFS = new GridFS(mongoDB);
            InputStream file_stream = getFTPInputStream(file);
            String caption_filename = FilenameUtils.removeExtension(file) + "_caption.txt";
            InputStream caption_stream = getFTPInputStream(caption_filename);
            StringWriter writer = new StringWriter();
            Charset par = null;
            IOUtils.copy(caption_stream, writer, par);
            String caption = writer.toString();
            GridFSInputFile in = gridFS.createFile(file_stream);
            in.setFilename(file);
            in.put("BuzzID", buzz_id);
            in.put("Caption", caption);
            in.put("PicNum", i);
            in.save();
        } catch (IOException ex) {
            Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex);
        }
        i++;
    }
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public boolean addFile(LearningElement le) throws UnknownHostException, IOException {
    File file = new File(AppConfig.USER_VARIABLE + le.getFilePath() + le.getFilename());
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);

    GridFS gf = new GridFS(db, "le.meta");
    GridFSInputFile gfsFile = gf.createFile(file);
    gfsFile.setFilename(le.getFilename());
    gfsFile.setContentType(le.getContentType());
    gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement");
    gfsFile.put("title", le.getTitle());
    gfsFile.put("filePath", le.getFilePath());
    gfsFile.put("subject", le.getSubject());
    gfsFile.put("description", le.getDescription());
    gfsFile.put("downloads", le.getDownloads());
    gfsFile.put("rating", le.getRating());
    gfsFile.put("comments", le.getComments());
    gfsFile.put("uploadedBy", le.getUploadedBy());
    gfsFile.put("status", le.getStatus());
    gfsFile.put("rev", le.getRev());
    gfsFile.put("type", le.getType());
    gfsFile.save();//from  w w w . ja  va  2  s . c  o  m

    // Let's store our document to MongoDB
    /*   System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta"));
       if(search(gfsFile.getMD5(), "le.meta") > 1){            
    deleteLE(le.getFileName(),"le.meta");
       }*/
    //
    //   collection.insert(info, WriteConcern.SAFE);
    return true;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public boolean addFile(LearningElement le, File file) throws UnknownHostException, IOException {
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);

    GridFS gf = new GridFS(db, "le.meta");
    GridFSInputFile gfsFile = gf.createFile(file);
    gfsFile.setFilename(le.getFilename());
    gfsFile.setContentType(le.getContentType());
    gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement");
    gfsFile.put("title", le.getTitle());
    gfsFile.put("filePath", le.getFilePath());
    gfsFile.put("subject", le.getSubject());
    gfsFile.put("description", le.getDescription());
    gfsFile.put("downloads", le.getDownloads());
    gfsFile.put("rating", le.getRating());
    gfsFile.put("comments", le.getComments());
    gfsFile.put("uploadedBy", le.getUploadedBy());
    gfsFile.put("status", le.getStatus());
    gfsFile.put("rev", le.getRev());
    gfsFile.put("type", le.getType());
    gfsFile.save();// w  w  w  . ja v a2  s  .co m

    // Let's store our document to MongoDB
    /*   System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta"));
       if(search(gfsFile.getMD5(), "le.meta") > 1){            
    deleteLE(le.getFileName(),"le.meta");
       }*/
    //
    //   collection.insert(info, WriteConcern.SAFE);
    return true;
}

From source file:com.fileoperations.CopyClass.java

public Boolean forSingleFile() {
    try {/*from ww  w  .java2  s.c o m*/
        if (name.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + name);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", newPath + pathMerger + name);
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }
                DBObject copyFile = cursor.next();
                GridFS fileDB = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = fileDB.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", newPath + pathMerger + name);
                document.append("folder", "0");
                document.append("parent", newPath);
                document.append("name", name);
                document.append("type", copyFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = fileDB.createFile(data);
                inputFile.setId(newPath + pathMerger + name);
                inputFile.put("path", newPath);
                inputFile.setFilename(name);
                inputFile.save();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.FolderDownload.java

public Boolean copyFolder(String newPath) throws IOException {
    try {//from   w w w. j a v  a 2s  . co  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 {//from   ww  w  .j a  v a2s  .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", 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.fileoperations.RenameFolder.java

public Boolean forSingleFile() {
    try {//from  w w w. ja  v  a  2  s .c o  m
        if (oldName.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + oldName);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                DBObject renameFile = cursor.next();
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }

                GridFS file = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = file.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", parentPath + pathMerger + newName + renameFile.get("type").toString());
                document.append("folder", "0");
                document.append("parent", parentPath);
                document.append("name", newName + renameFile.get("type").toString());
                document.append("type", renameFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = file.createFile(data);
                inputFile.setId(parentPath + pathMerger + newName + renameFile.get("type").toString());
                inputFile.put("path", parentPath);
                inputFile.setFilename(newName + renameFile.get("type").toString());
                inputFile.save();
                file.remove(file.findOne(query));
                collection.remove(renameFile);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}