Java Code Examples for com.mongodb.gridfs.GridFSInputFile#setMetaData()

The following examples show how to use com.mongodb.gridfs.GridFSInputFile#setMetaData() . 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: 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 3
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;
	}
}