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

The following examples show how to use com.mongodb.gridfs.GridFSInputFile#save() . 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: 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 4
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 5
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 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: 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;
}
 
Example 8
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 9
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 10
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 11
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 12
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");
  }

}