Example usage for com.mongodb.gridfs GridFSInputFile setFilename

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

Introduction

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

Prototype

public void setFilename(final String filename) 

Source Link

Document

Sets the file name on the GridFS entry.

Usage

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();/*from  w  ww .  j a v a2 s. c  om*/

    // 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.edgytech.umongo.DbPanel.java

License:Apache License

public void uploadFile(final ButtonBase button) {
    final DbNode dbNode = getDbNode();
    final DB db = dbNode.getDb();

    final String path = getStringFieldValue(Item.uploadFilePath);
    if (path.isEmpty()) {
        return;// ww  w .  java  2s .  c  o  m
    }
    final File src = new File(path);
    final String fileName = getStringFieldValue(Item.uploadFileName);
    final String contentType = getStringFieldValue(Item.uploadContentType);
    final DBObject metadata = ((DocBuilderField) getBoundUnit(Item.uploadMetadata)).getDBObject();

    new DbJob() {

        @Override
        public Object doRun() throws IOException {
            final GridFSInputFile file = getGridFS().createFile(src);
            if (!fileName.isEmpty()) {
                file.setFilename(fileName);
            }
            if (!contentType.isEmpty()) {
                file.setContentType(contentType);
            }
            if (metadata != null) {
                file.setMetaData(metadata);
            }
            file.save();
            return file;
        }

        @Override
        public String getNS() {
            return db.getName();
        }

        @Override
        public String getShortName() {
            return "Upload File";
        }

        @Override
        public DBObject getRoot(Object result) {
            return new BasicDBObject("path", path);
        }

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            // may have new collections
            dbNode.structureComponent();
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

From source file:com.englishtown.integration.java.IntegrationTestHelper.java

License:Open Source License

public static String createFile(JsonObject config, String bucket) {

    GridFS gridFS = IntegrationTestHelper.getGridFS(config, bucket);
    GridFSInputFile inputFile = gridFS
            .createFile(IntegrationTestHelper.class.getResourceAsStream("/EF_Labs_ENG_logo.JPG"));

    inputFile.setContentType(DEFAULT_CONTENT_TYPE);
    inputFile.setFilename(DEFAULT_FILENAME);
    inputFile.setChunkSize(DEFAULT_CHUNK_SIZE);
    inputFile.setMetaData(DEFAULT_METADATA);
    inputFile.save();//from  ww w  . jav a2  s  .c  o  m

    return inputFile.getId().toString();
}

From source file:com.fileoperations.CopyClass.java

public Boolean forSingleFile() {
    try {//  w  w w. j av  a 2  s .  co 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 ww  . j  ava  2 s . 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 {/* w  w  w .  jav  a 2 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", 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 {//  w  w w.j  a  v a 2  s . c  om
        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();
    }
}

From source file:com.foodtruckdata.mongodb.UsersInput.java

private void storeFile(String filepath, String filetype, ObjectId truck_id) {
    try {// www.  j av a  2s  .c o  m
        GridFS gridFS = new GridFS(mongoDB);
        InputStream file_stream = getFTPInputStream(filepath);
        StringWriter writer = new StringWriter();
        Charset par = null;
        IOUtils.copy(file_stream, writer, par);
        GridFSInputFile in = gridFS.createFile(file_stream);
        in.setFilename(filepath);
        in.put("TruckID", truck_id);
        in.put("FileType", filetype);
        in.save();
    } catch (IOException ex) {
        Logger.getLogger(UsersInput.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.glaf.core.test.MongoDBGridFSThread.java

License:Apache License

public void run() {
    if (file.exists() && file.isFile()) {
        String path = file.getAbsolutePath();
        path = path.replace('\\', '/');
        if (StringUtils.contains(path, "/temp/") || StringUtils.contains(path, "/tmp/")
                || StringUtils.contains(path, "/logs/") || StringUtils.contains(path, "/work/")
                || StringUtils.endsWith(path, ".log") || StringUtils.endsWith(path, ".class")) {
            return;
        }//from   w  w  w.  j  a  va2 s. co m
        int retry = 0;
        boolean success = false;
        byte[] bytes = null;
        GridFSInputFile inputFile = null;
        while (retry < 1 && !success) {
            try {
                retry++;
                bytes = FileUtils.getBytes(file);
                if (bytes != null) {
                    inputFile = gridFS.createFile(bytes);
                    DBObject metadata = new BasicDBObject();
                    metadata.put("path", path);
                    metadata.put("filename", file.getName());
                    metadata.put("size", bytes.length);
                    inputFile.setMetaData(metadata);
                    inputFile.setId(path);
                    inputFile.setFilename(file.getName());// ??
                    inputFile.save();// ?
                    bytes = null;
                    success = true;
                    logger.debug(file.getAbsolutePath() + " save ok.");
                }
            } catch (Exception ex) {
                logger.error(ex);
                ex.printStackTrace();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                bytes = null;
                inputFile = null;
            }
        }
    }
}

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * insert gridfs//www.ja v a  2  s  . com
 * 
 * @param userDB
 * @param strBucket
 * @param fileList
 * @throws Exception
 */
public static void insertGridFS(UserDBDAO userDB, String strBucket, List<String> fileList) throws Exception {

    DB mongoDb = findDB(userDB);
    GridFS gridFs = null;

    if ("".equals(strBucket))
        gridFs = new GridFS(mongoDb);
    else
        gridFs = new GridFS(mongoDb, strBucket);

    for (String strFile : fileList) {
        String saveFileName = strFile.substring(strFile.lastIndexOf(File.separator) + 1);

        GridFSInputFile gfsFile = gridFs.createFile(new File(strFile));
        gfsFile.setFilename(saveFileName);
        gfsFile.save();
    }

}