com.mongodb.gridfs.GridFSInputFile Java Examples

The following examples show how to use com.mongodb.gridfs.GridFSInputFile. 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: 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 #2
Source File: MongoService.java    From BLELocalization with MIT License 6 votes vote down vote up
public String saveFile(InputStream is, String contentType) throws IOException {
	GridFSInputFile file = getNewFile();	
	String id = file.getId().toString();
	OutputStream os = getFileOutputStream(id, contentType);
	if (os != null) {
		try {
			byte data[] = new byte[4096];
			int len = 0;
			while ((len = is.read(data, 0, data.length)) > 0) {
				os.write(data, 0, len);
			}
			return id;
		} finally {
			os.close();
		}
	}
	return null;
}
 
Example #3
Source File: ImageDao.java    From XBDD with Apache License 2.0 6 votes vote down vote up
public String saveImageAndReturnFilename(final JUnitEmbedding embedding, final Coordinates coordinates, final String featureId,
		final String scenarioId) {
	final GridFS gridFS = getGridFS();

	try {
		final GridFSInputFile image = gridFS
				.createFile(Base64.decodeBase64((embedding.getData()).getBytes()));

		image.setFilename(UUID.randomUUID().toString());

		final BasicDBObject metadata = new BasicDBObject().append("product", coordinates.getProduct())
				.append("major", coordinates.getMajor()).append("minor", coordinates.getMinor())
				.append("servicePack", coordinates.getServicePack()).append("build", coordinates.getBuild())
				.append("feature", featureId)
				.append("scenario", scenarioId);
		image.setMetaData(metadata);
		image.setContentType(embedding.getMime_type());
		image.save();

		return image.getFilename();

	} catch (final ClassCastException e) {
		LOGGER.warn("Embedding was malformed and will be skipped");
		return null;
	}
}
 
Example #4
Source File: MongoRepositoryItem.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Override
public OutputStream createOutputStreamToWrite() {
  checkState(State.NEW);

  storingOutputStream = new FilterOutputStream(((GridFSInputFile) dbFile).getOutputStream()) {

    @Override
    public void close() throws IOException {
      putMetadataInGridFS(false);
      super.close();
      refreshAttributesOnClose();
    }
  };

  return storingOutputStream;
}
 
Example #5
Source File: GridFSFileBuilder.java    From ymate-platform-v2 with Apache License 2.0 5 votes vote down vote up
public GridFSInputFile build(IGridFSSession gridFS) throws Exception {
    GridFSInputFile _inFile = null;
    switch (__type) {
        case 1:
            // is File
            _inFile = gridFS.getGridFS().createFile((File) __targetObject);
            break;
        case 2:
            // is InputStream
            _inFile = gridFS.getGridFS().createFile((InputStream) __targetObject);
            break;
        case 3:
            // is Array
            _inFile = gridFS.getGridFS().createFile((byte[]) __targetObject);
            break;
        default:
    }
    if (_inFile != null) {
        _inFile.setFilename(__filename);
        _inFile.setContentType(__contentType);
        if (__chunkSize > 0) {
            _inFile.setChunkSize(__chunkSize);
        }
        if (!__attributes.isEmpty()) {
            for (Map.Entry<String, Object> _entry : __attributes.entrySet()) {
                _inFile.put(_entry.getKey(), _entry.getValue());
            }
        }
    }
    return _inFile;
}
 
Example #6
Source File: MongoDbStore.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private void saveFile(GridFSInputFile file) throws IOException {
  try {
    file.save();
  } catch (MongoException e) {
    // Unfortunately, file.save() wraps any IOException thrown in a
    // 'MongoException'. Since the interface explicitly throws IOExceptions,
    // we unwrap any IOExceptions thrown.
    Throwable innerException = e.getCause();
    if (innerException instanceof IOException) {
      throw (IOException) innerException;
    } else {
      throw e;
    }
  };
}
 
Example #7
Source File: MongoDbStore.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
@Override
public void storeMetadata(AttachmentId attachmentId, AttachmentMetadata metaData)
    throws IOException {
  AttachmentMetadataProtoImpl proto = new AttachmentMetadataProtoImpl(metaData);
  byte[] bytes = proto.getPB().toByteArray();
  GridFSInputFile file =
      metadataGrid.createFile(new ByteArrayInputStream(bytes), attachmentId.serialise());
  saveFile(file);
}
 
Example #8
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
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());
        }
    }
}
 
Example #9
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
public String save(byte[] data, String filename, Map<String, Object> attributes){
    GridFSInputFile f = fs.createFile(data);
    f.setChunkSize(chunkSize);
    f.setFilename(filename);
    setAttributes(f, attributes);
    f.save();
    return f.getId().toString();
}
 
Example #10
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
public String save(InputStream is, String filename, Map<String, Object> attributes){
    GridFSInputFile f = fs.createFile(is);
    f.setChunkSize(chunkSize);
    f.setFilename(filename);
    setAttributes(f, attributes);
    f.save();
    return f.getId().toString();
}
 
Example #11
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
public String save(File file, String filename, Map<String, Object> attributes){
    GridFSInputFile f = null;
    try{
        f = fs.createFile(file);
    }catch(IOException ex){
        throw new BuguFSException(ex.getMessage());
    }
    if(f != null){
        f.setChunkSize(chunkSize);
        f.setFilename(filename);
        setAttributes(f, attributes);
        f.save();
    }
    return f.getId().toString();
}
 
Example #12
Source File: MongoRepository.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public RepositoryItem createRepositoryItem(String id) {

  // TODO The file is not written until outputstream is closed. There is a
  // potentially data race with this unique test
  if (!gridFS.find(id).isEmpty()) {
    throw new DuplicateItemException(id);
  }

  GridFSInputFile dbFile = gridFS.createFile(id);
  dbFile.setId(id);
  return createRepositoryItem(dbFile);
}
 
Example #13
Source File: MongoService.java    From BLELocalization with MIT License 5 votes vote down vote up
private OutputStream getFileOutputStream(String id, String contentType) {
	if (mFS != null) {
		try {
			deleteFile(id);
			GridFSInputFile dbFile = mFS.createFile(id);
			if (contentType != null) {
				dbFile.setContentType(contentType);
			}
			return dbFile.getOutputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
Example #14
Source File: MongoRepositoryItem.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
public MongoRepositoryItem(MongoRepository repository, GridFSInputFile dbFile) {
  this(repository, dbFile, State.NEW);
}
 
Example #15
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 #16
Source File: MongoRepository.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
private RepositoryItem createRepositoryItem(GridFSInputFile dbFile) {
  return new MongoRepositoryItem(this, dbFile);
}
 
Example #17
Source File: MongoRepository.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Override
public RepositoryItem createRepositoryItem() {
  GridFSInputFile dbFile = gridFS.createFile();
  dbFile.setFilename(dbFile.getId().toString());
  return createRepositoryItem(dbFile);
}
 
Example #18
Source File: Add.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	
	// Get the necessary Mongo references
	DB db	= getDB(_session,argStruct);
	
	GridFS	gridfs	= getGridFS(_session, argStruct, db);
	GridFSInputFile fsInputFile = null;
	
	// Get the file information
	String filename	= getNamedStringParam(argStruct, "filename", null);
	if ( filename == null )
		throwException(_session, "please specify a filename");
	
	try{
		
		cfData	ftmp	= getNamedParam(argStruct, "file", null);
		if ( ftmp.getDataType() == cfData.CFBINARYDATA ){
			fsInputFile	= gridfs.createFile( ((cfBinaryData)ftmp).getByteArray() );
		}else{
			// The 'file' parameter is a string, which means it is a path to a file
			
			File	inputFile	= new File( ftmp.getString() );
			if ( !inputFile.exists() )
				throwException(_session, "File:" + inputFile + " does not exist" );
			
			if ( !inputFile.isFile() )
				throwException(_session, "File:" + inputFile + " is not a valid file" );
			
			fsInputFile	= gridfs.createFile(inputFile);
		}
		
	} catch (IOException e) {
		throwException(_session, e.getMessage() );
	}
	
	fsInputFile.setFilename(filename);
	
	String contenttype	= getNamedStringParam(argStruct, "contenttype", null);		
	if ( contenttype != null )
		fsInputFile.setContentType(contenttype);

	
	String _id	= getNamedStringParam(argStruct, "_id", null);
	if ( _id != null )
		fsInputFile.setId( _id );

	
	// Get and set the metadata
	cfData mTmp	= getNamedParam(argStruct, "metadata", null);
	if ( mTmp != null )
		fsInputFile.setMetaData(getDBObject(mTmp));
	
	
	// Save the Object
	try{
		fsInputFile.save();
		return new cfStringData( fsInputFile.getId().toString() );
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #19
Source File: ImportFilesRepositoryCustomImpl.java    From osiris with Apache License 2.0 4 votes vote down vote up
private void saveFile(String appIdentifier, File file, GridFS gridFS) throws IOException{
	GridFSInputFile gridFSInputFile = gridFS.createFile(file);
	gridFSInputFile.setFilename(appIdentifier);
	gridFSInputFile.save();
}
 
Example #20
Source File: GetMapFile.java    From osiris with Apache License 2.0 4 votes vote down vote up
private void saveFile(String appIdentifier, File file, GridFS gridFS) throws IOException{
	GridFSInputFile gridFSInputFile = gridFS.createFile(file);
	gridFSInputFile.setFilename(appIdentifier);
	gridFSInputFile.save();
}
 
Example #21
Source File: GetMapFile.java    From osiris with Apache License 2.0 4 votes vote down vote up
private void saveFile(String appIdentifier, File file, GridFS gridFS) throws IOException{
	GridFSInputFile gridFSInputFile = gridFS.createFile(file);
	gridFSInputFile.setFilename(appIdentifier);
	gridFSInputFile.save();
}
 
Example #22
Source File: MongoService.java    From BLELocalization with MIT License 4 votes vote down vote up
private GridFSInputFile getNewFile() {
	return mFS != null ? mFS.createFile() : null;
}
 
Example #23
Source File: MongoGridFSSession.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public GridFSFile upload(GridFSFileBuilder inputFile) throws Exception {
    GridFSInputFile _inFile = inputFile.build(this);
    _inFile.save();
    return _inFile;
}