Java Code Examples for com.mongodb.gridfs.GridFSDBFile

The following examples show how to use com.mongodb.gridfs.GridFSDBFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: pampas   Source File: MongoTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void storeFile() throws IOException {
    File file = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.jar");
    GridFS gridFS = new GridFS(datastore.getDB());

    GridFSInputFile gridFSInputFile = gridFS.createFile(file);
    if (gridFS.findOne(file.getName()) == null) {
        gridFSInputFile.setId(file.getName());
        gridFSInputFile.setMetaData(new BasicDBObject("version", "1.1.2"));
        gridFSInputFile.save();
    }

    GridFSDBFile fsdbFile = gridFS.findOne(file.getName());
    File newfile = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.new.jar");
    if (newfile.exists()) {
        newfile.delete();
    }
    newfile.createNewFile();
    newfile.setWritable(true);

    fsdbFile.writeTo(newfile);
    System.out.println("done : " + fsdbFile.getFilename());
}
 
Example 2
Source Project: beam   Source File: MongoDbGridFSIO.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public long getEstimatedSizeBytes(PipelineOptions options) throws Exception {
  Mongo mongo = spec.connectionConfiguration().setupMongo();
  try {
    GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
    DBCursor cursor = createCursor(gridfs);
    long size = 0;
    while (cursor.hasNext()) {
      GridFSDBFile file = (GridFSDBFile) cursor.next();
      size += file.getLength();
    }
    return size;
  } finally {
    mongo.close();
  }
}
 
Example 3
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<GridFSDBFile> findAll(OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find();
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}
 
Example 4
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<GridFSDBFile> find(Query query, OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find(query.toBson());
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}
 
Example 5
Source Project: BLELocalization   Source File: MongoService.java    License: MIT License 6 votes vote down vote up
private boolean readFile(GridFSDBFile dbFile, OutputStream os) throws IOException {
	InputStream is = null;
	try {
		is = dbFile.getInputStream();
		byte data[] = new byte[4096];
		int len = 0;
		while ((len = is.read(data, 0, data.length)) > 0) {
			os.write(data, 0, len);
		}
		return true;
	} finally {
		if (is != null) {
			is.close();
		}
	}
}
 
Example 6
Source Project: BLELocalization   Source File: MongoService.java    License: MIT License 6 votes vote down vote up
public void sendFile(String path, HttpServletResponse response) throws IOException {
	GridFSDBFile file = getFile(path);
	if (file == null) {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("File %s does not exist", path));
	} else {
		String contentType = file.getContentType();
		if (contentType != null) {
			response.setContentType(contentType);
		}
		OutputStream os = null;
		try {
			readFile(file, os = response.getOutputStream());
		} catch (Exception e) {
			System.err.println("Send error: " + path);
		} finally {
			if (os != null) {
				os.close();
			}
		}
	}
}
 
Example 7
Source Project: openbd-core   Source File: FindOne.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a GridFSDBFile object into a struct
 * 
 * @param filefs
 * @return
 */
protected cfStructData	toStruct( GridFSDBFile filefs ){
	cfStructData	s	= new cfStructData();
	if ( filefs == null )
		return s;
	
	s.put("_id", 				new cfStringData(filefs.getId().toString()) );
	s.put("chunkSize", 	new cfNumberData(filefs.getChunkSize()) );
	s.put("length", 		new cfNumberData(filefs.getLength()) );
	s.put("md5",			 	new cfStringData(filefs.getMD5()) );
	s.put("filename", 	new cfStringData(filefs.getFilename()) );
	s.put("contentType", new cfStringData(filefs.getContentType()) );
	s.put("uploadDate", new cfDateData(filefs.getUploadDate()) );
	
	s.put("metadata", 	tagUtils.convertToCfData( filefs.getMetaData() ) );
	
	return s;
}
 
Example 8
Source Project: lumongo   Source File: MongoConstants.java    License: Apache License 2.0 6 votes vote down vote up
public static byte[] readFileFromGridFS(GridFSDBFile file) {
	InputStream is = file.getInputStream();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {

		byte bytes[] = new byte[BUFFER_SIZE];
		int read = -1;

		while ((read = is.read(bytes)) != -1) {
			baos.write(bytes, 0, read);
		}

		return baos.toByteArray();
	}
	catch (Exception e) {
	}
	return null;
}
 
Example 9
Source Project: incubator-retired-wave   Source File: MongoDbStore.java    License: Apache License 2.0 6 votes vote down vote up
private AttachmentData fileToAttachmentData(final GridFSDBFile attachmant) {
  if (attachmant == null) {
    return null;
  } else {
    return new AttachmentData() {

      @Override
      public InputStream getInputStream() throws IOException {
        return attachmant.getInputStream();
      }

      @Override
      public long getSize() {
        return attachmant.getLength();
      }
    };
  }
}
 
Example 10
Source Project: beam   Source File: MongoDbGridFSIO.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends BoundedSource<ObjectId>> split(
    long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
  Mongo mongo = spec.connectionConfiguration().setupMongo();
  try {
    GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
    DBCursor cursor = createCursor(gridfs);
    long size = 0;
    List<BoundedGridFSSource> list = new ArrayList<>();
    List<ObjectId> objects = new ArrayList<>();
    while (cursor.hasNext()) {
      GridFSDBFile file = (GridFSDBFile) cursor.next();
      long len = file.getLength();
      if ((size + len) > desiredBundleSizeBytes && !objects.isEmpty()) {
        list.add(new BoundedGridFSSource(spec, objects));
        size = 0;
        objects = new ArrayList<>();
      }
      objects.add((ObjectId) file.getId());
      size += len;
    }
    if (!objects.isEmpty() || list.isEmpty()) {
      list.add(new BoundedGridFSSource(spec, objects));
    }
    return list;
  } finally {
    mongo.close();
  }
}
 
Example 11
Source Project: jsflight   Source File: MongoDbStorageService.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private InputStream getStreamByFilename(String fname)
{
    GridFSDBFile file = getGridFsTemplate().findOne(new Query().addCriteria(Criteria.where("filename").is(fname)));

    return file != null ? file.getInputStream() : null;
}
 
Example 12
Source Project: XBDD   Source File: Attachment.java    License: Apache License 2.0 5 votes vote down vote up
@GET
@Path("/{id}")
public Response getAttachment(@PathParam("id") final String id) throws IOException {
	final GridFS gridFS = new GridFS(this.mongoLegacyGrid);
	final GridFSDBFile file = gridFS.findOne(id);
	// log.info(file);
	if (file == null) {
		throw new WebApplicationException(404);
	}
	return Response.ok(org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType()).build();

}
 
Example 13
Source Project: osiris   Source File: MapFileRepositoryCustomImpl.java    License: Apache License 2.0 5 votes vote down vote up
private InputStream getMapFileByAppId(GridFS gridFS,String appIdentifier) throws MapFileNotExistsException{
	InputStream fileMap=null;
	GridFSDBFile gridFSFileMap = gridFS.findOne(appIdentifier);
	if(gridFSFileMap==null){
		throw new MapFileNotExistsException();
	}
	fileMap=gridFSFileMap.getInputStream();
	return fileMap;
}
 
Example 14
Source Project: openbd-core   Source File: Find.java    License: GNU General Public License v3.0 5 votes vote down vote up
private cfArrayData toArray( List<GridFSDBFile> list ) throws cfmRunTimeException{
	cfArrayData arr	= cfArrayData.createArray(1);
	if ( list == null || list.size() == 0 )
		return arr;
	
	Iterator<GridFSDBFile> it	= list.iterator();
	while ( it.hasNext() ){
		GridFSDBFile f	= it.next();
		arr.addElement( toStruct(f) );
	}
	
	return arr;
}
 
Example 15
Source Project: kurento-java   Source File: MongoRepository.java    License: Apache License 2.0 5 votes vote down vote up
private MongoRepositoryItem createRepositoryItem(GridFSDBFile dbFile) {

    MongoRepositoryItem item = new MongoRepositoryItem(this, dbFile);

    Map<String, String> metadata = new HashMap<>();
    DBObject object = dbFile.getMetaData();
    for (String key : object.keySet()) {
      metadata.put(key, object.get(key).toString());
    }
    item.setMetadata(metadata);
    return item;
  }
 
Example 16
Source Project: kurento-java   Source File: MongoRepository.java    License: Apache License 2.0 5 votes vote down vote up
private List<RepositoryItem> findRepositoryItemsByQuery(String query) {
  List<GridFSDBFile> files = gridFS.find((DBObject) JSON.parse(query));

  List<RepositoryItem> repositoryItems = new ArrayList<>();
  for (GridFSDBFile file : files) {
    repositoryItems.add(createRepositoryItem(file));
  }

  return repositoryItems;
}
 
Example 17
Source Project: bugu-mongo   Source File: ImageUploader.java    License: Apache License 2.0 5 votes vote down vote up
private InputStream getOriginalInputStream(){
    DBObject query = new BasicDBObject(BuguFS.FILENAME, filename);
    query.put(DIMENSION, null);
    BuguFS fs = BuguFSFactory.getInstance().create(connection, bucket, chunkSize);
    GridFSDBFile f = fs.findOne(query);
    return f.getInputStream();
}
 
Example 18
Source Project: bugu-mongo   Source File: BuguFS.java    License: Apache License 2.0 5 votes vote down vote up
public BuguFS(String connectionName, String bucket, int chunkSize){
    this.bucket = bucket;
    this.chunkSize = chunkSize;
    DB db = BuguFramework.getInstance().getConnection(connectionName).getDB();
    fs = new GridFS(db, bucket);
    files = db.getCollection(bucket + ".files");
    //ensure the DBCursor can be cast to GridFSDBFile
    files.setObjectClass(GridFSDBFile.class);
}
 
Example 19
Source Project: bugu-mongo   Source File: BuguFS.java    License: Apache License 2.0 5 votes vote down vote up
public void rename(GridFSDBFile file, String newName){
    ObjectId id = (ObjectId)file.getId();
    DBObject query = new BasicDBObject(Operator.ID, id);
    DBObject dbo = files.findOne(query);
    dbo.put(FILENAME, newName);
    files.save(dbo);
}
 
Example 20
Source Project: bugu-mongo   Source File: BuguFS.java    License: Apache License 2.0 5 votes vote down vote up
private List<GridFSDBFile> toFileList(DBCursor cursor){
    List<GridFSDBFile> list = new ArrayList<GridFSDBFile>();
    while(cursor.hasNext()){
        DBObject dbo = cursor.next();
        list.add((GridFSDBFile)dbo);
    }
    cursor.close();
    return list;
}
 
Example 21
Source Project: incubator-retired-wave   Source File: MongoDbStore.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public AttachmentMetadata getMetadata(AttachmentId attachmentId) throws IOException {
  final GridFSDBFile metadata = metadataGrid.findOne(attachmentId.serialise());

  if (metadata == null) {
    return null;
  }
  AttachmentProto.AttachmentMetadata protoMetadata =
      AttachmentProto.AttachmentMetadata.parseFrom(metadata.getInputStream());
  return new AttachmentMetadataProtoImpl(protoMetadata);
}
 
Example 22
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public GridFSDBFile findFirst(String filename) {
    return __gridFS.findOne(filename);
}
 
Example 23
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public GridFSDBFile findFirst(Query query) {
    return __gridFS.findOne(query.toBson());
}
 
Example 24
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public GridFSDBFile findById(String id) {
    return __gridFS.find(new ObjectId(id));
}
 
Example 25
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> findAll() {
    return findAll(null, null);
}
 
Example 26
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> findAll(OrderBy orderBy) {
    return findAll(orderBy, null);
}
 
Example 27
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> find(String filename) {
    return __gridFS.find(filename);
}
 
Example 28
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> find(String filename, OrderBy orderBy) {
    return __gridFS.find(filename, orderBy.toBson());
}
 
Example 29
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> find(Query query) {
    return __gridFS.find(query.toBson());
}
 
Example 30
Source Project: ymate-platform-v2   Source File: MongoGridFSSession.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<GridFSDBFile> find(Query query, OrderBy orderBy) {
    return __gridFS.find(query.toBson(), orderBy.toBson());
}