Package com.mongodb.gridfs

Examples of com.mongodb.gridfs.GridFS


        setEnumBinding(Item.values(), this);
    }

    public GridFS getGridFS() {
        if (gridFS == null) {
            gridFS = new GridFS(getDbNode().getDb());
        }
        return gridFS;
    }
View Full Code Here


            String uploadCollection = morphiaConf.getString(ConfigKey.COLLECTION_UPLOADS.getKey());
            if (StringUtils.isBlank(dbName)) {
                uploadCollection = "uploads";
                MorphiaLogger.warn("Missing Morphia configuration key [%s]. Use default value instead [%s]", ConfigKey.COLLECTION_UPLOADS, "uploads");
            }
            gridfs = new GridFS(ds.getDB(), uploadCollection);
            MorphiaLogger.debug("GridFS created", "");
            MorphiaLogger.debug("Add Interceptor...", "");
            morphia.getMapper().addInterceptor(new AbstractEntityInterceptor() {

                @Override
View Full Code Here

  private static final Log LOG = LogFactory.getLog();
  private static ThreadLocal<MongoClient> clientLocal = new ThreadLocal<MongoClient>();
  private static ThreadLocal<DB> dbLocal = new ThreadLocal<DB>();

  public static void saveFile(String fileName, byte[] data) {
    GridFS fs = new GridFS(db());
    GridFSInputFile in = fs.createFile(data);
    in.setId(fileName);
    in.save();
  }
View Full Code Here

    in.save();
  }

  public static byte[] getFile(String fileName) {
    try {
      GridFS fs = new GridFS(db());
      GridFSDBFile out = fs.findOne(new BasicDBObject("_id", fileName));
      ByteArrayOutputStream temp = new ByteArrayOutputStream();
      out.writeTo(temp);
      return temp.toByteArray();
    } catch (Exception e) {
      // quiet
View Full Code Here

    if (repository instanceof MongoRepository) {

      MongoRepository mongoRepository = (MongoRepository) repository;

      GridFS gridFS = mongoRepository.getGridFS();

      GridFSInputFile file = gridFS.createFile(new File(
          "test-files/sample.txt"));

      file.setId("sample.txt");

      file.save();

      List<GridFSDBFile> files = gridFS.find((DBObject) JSON
          .parse("{ _id : 'sample.txt' }"));

      assertNotNull(files);
      assertEquals(1, files.size());
    }
View Full Code Here

        }
        if (getAllBuckets(dbName).contains(bucketName)) {
            throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS, "Collection [" + bucketName + "] already exists in Database [" + dbName + "]");
        } else {
            try {
                new GridFS(mongoInstance.getDB(dbName), bucketName);
            } catch (MongoException e) {
                throw new GridFSException(ErrorCodes.GRIDFS_CREATION_EXCEPTION, e.getMessage());
            }
            return "GridFS bucket [" + bucketName + "] added to database [" + dbName + "].";
        }
View Full Code Here

        }
        if (!databaseService.getDbList().contains(dbName)) {
            throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, "Database with dbName [ " + dbName + "] does not exist");
        }
        DB db = mongoInstance.getDB(dbName);
        GridFS gridFS = new GridFS(db, bucketName);
        DBCollection filesCollection = getGridFSCollection(gridFS, bucketType);
        try {
            if (command.equals("find")) {
                return executeFind(filesCollection, query, sortBy, limit, skip);
            } else if (command.equals("drop")) {
View Full Code Here

                    "Database with dbName [ " + dbName + "] does not exist");
            }
            Object docId = JSON.parse(_id);
            BasicDBObject objectId = new BasicDBObject("_id", docId);
            GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);
            GridFSDBFile gridFSDBFile = gridFS.findOne(objectId);
            String tempDir = System.getProperty("java.io.tmpdir");
            tempFile = new File(tempDir + "/" + gridFSDBFile.getFilename());
            gridFSDBFile.writeTo(tempFile);

        } catch (MongoException m) {
View Full Code Here

        try {
            if (!databaseService.getDbList().contains(dbName)) {
                throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, "DB [" + dbName + "] DOES NOT EXIST");
            }

            GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);
            GridFSInputFile fsInputFile = gridFS.createFile(inputStream, fileData.getFileName());
            fsInputFile.setContentType(formData.getMediaType().toString());
            fsInputFile.save();
            String objectId = JSON.serialize(fsInputFile.getId());
            JSONObject obj = new JSONObject();
            obj.put("name", fsInputFile.getFilename());
View Full Code Here

            }
            if (_id == null) {
                throw new DocumentException(ErrorCodes.DOCUMENT_EMPTY, "File is empty");
            }

            GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);
            Object docId = JSON.parse(_id);
            BasicDBObject objectId = new BasicDBObject("_id", docId);
            gridFSDBFile = gridFS.findOne(objectId);

            if (gridFSDBFile == null) {
                throw new DocumentException(ErrorCodes.DOCUMENT_DOES_NOT_EXIST, "Document does not exist !");
            }

            gridFS.remove(objectId);

        } catch (MongoException e) {
            throw new DocumentException(ErrorCodes.DOCUMENT_DELETION_EXCEPTION, e.getMessage());
        }
        result = "File [" + gridFSDBFile.getFilename() + "] has been deleted.";
View Full Code Here

TOP

Related Classes of com.mongodb.gridfs.GridFS

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.