com.mongodb.gridfs.GridFSDBFile Java Examples

The following examples show how to use com.mongodb.gridfs.GridFSDBFile. 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 File: MongoDbStore.java    From incubator-retired-wave with 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 #2
Source File: MongoGridFSSession.java    From ymate-platform-v2 with 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 #3
Source File: MongoGridFSSession.java    From ymate-platform-v2 with 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 #4
Source File: MongoService.java    From BLELocalization with 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 #5
Source File: MongoService.java    From BLELocalization with 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 #6
Source File: MongoTest.java    From pampas with 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 #7
Source File: FindOne.java    From openbd-core with 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 File: MongoDbGridFSIO.java    From beam with 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 #9
Source File: MongoConstants.java    From lumongo with 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 #10
Source File: BuguFS.java    From bugu-mongo with 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 #11
Source File: ImageUploader.java    From bugu-mongo with 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 #12
Source File: MongoRepository.java    From kurento-java with 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 #13
Source File: MongoRepository.java    From kurento-java with 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 #14
Source File: Find.java    From openbd-core with 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 File: BuguFS.java    From bugu-mongo with 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 #16
Source File: MapFileRepositoryCustomImpl.java    From osiris with 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 #17
Source File: BuguFS.java    From bugu-mongo with 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 #18
Source File: MongoDbStorageService.java    From jsflight with 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 #19
Source File: MongoDbStore.java    From incubator-retired-wave with 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 #20
Source File: Attachment.java    From XBDD with 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 #21
Source File: MongoDbGridFSIO.java    From beam with 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 #22
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public GridFSDBFile findOne(DBObject query){
    return fs.findOne(query);
}
 
Example #23
Source File: FilenameAsIdTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException {

  Repository repository = getRepository();

  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());
  } else {
    log.debug("Repository is not MongoDB");
  }

}
 
Example #24
Source File: MongoRepositoryItem.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
public MongoRepositoryItem(MongoRepository repository, GridFSDBFile dbFile) {
  this(repository, dbFile, State.STORED);
}
 
Example #25
Source File: MongoRepositoryItem.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream createInputStreamToRead() {
  checkState(State.STORED);
  return ((GridFSDBFile) dbFile).getInputStream();
}
 
Example #26
Source File: MongoRepository.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Override
public RepositoryItem findRepositoryItemById(String id) {

  List<GridFSDBFile> dbFiles = gridFS.find(id);

  if (dbFiles.size() > 0) {

    if (dbFiles.size() > 1) {
      log.warn("There are several files with the same " + "filename and should be only one");
    }

    return createRepositoryItem(dbFiles.get(0));
  }

  throw new NoSuchElementException("The repository item with id \"" + id + "\" does not exist");
}
 
Example #27
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public boolean exists(String filename){
    GridFSDBFile f = fs.findOne(filename);
    return f != null;
}
 
Example #28
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public boolean existsId(String id){
    GridFSDBFile f = fs.findOne(new ObjectId(id));
    return f != null;
}
 
Example #29
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public GridFSDBFile findOne(String filename){
    return fs.findOne(filename);
}
 
Example #30
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public GridFSDBFile findOneById(String id){
    return fs.findOne(new ObjectId(id));
}