Example usage for com.mongodb.gridfs GridFSInputFile save

List of usage examples for com.mongodb.gridfs GridFSInputFile save

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSInputFile save.

Prototype

@Override
public void save() 

Source Link

Document

Calls GridFSInputFile#save(long) with the existing chunk size.

Usage

From source file:net.tooan.ynpay.third.mongodb.fs.BuguFS.java

License:Apache License

public void save(InputStream is, String filename, Map<String, Object> params) {
    GridFSInputFile f = fs.createFile(is);
    f.setChunkSize(chunkSize);/*from w w  w  .  j  a  v a  2 s .c o m*/
    f.setFilename(filename);
    setParams(f, params);
    f.save();
}

From source file:net.tooan.ynpay.third.mongodb.fs.BuguFS.java

License:Apache License

public void save(byte[] data, String filename, Map<String, Object> params) {
    GridFSInputFile f = fs.createFile(data);
    f.setChunkSize(chunkSize);/*from ww  w  .  ja  v a2s . c o m*/
    f.setFilename(filename);
    setParams(f, params);
    f.save();
}

From source file:net.ymate.platform.persistence.mongodb.impl.MongoGridFSSession.java

License:Apache License

public GridFSFile upload(GridFSFileBuilder inputFile) throws Exception {
    GridFSInputFile _inFile = inputFile.build(this);
    _inFile.save();
    return _inFile;
}

From source file:oecp.framework.fs.gridfs.GridxFS.java

License:Apache License

public GridFSInputFile save(File file, String filename, String folderName, Map<String, Object> params) {
    GridFSInputFile f = null;
    FileInputStream fileInputStream = null;
    try {/*  www  .j a va 2 s.  co  m*/
        fileInputStream = new FileInputStream(file);
        f = fs.createFile(fileInputStream, file.getName());
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    f.setFilename(filename);
    setParams(f, folderName, params);
    f.save();
    if (fileInputStream != null) {//?
        try {
            fileInputStream.close();
        } catch (IOException e) {
        }
    }
    return f;
}

From source file:oecp.framework.fs.gridfs.GridxFS.java

License:Apache License

public GridFSInputFile save(InputStream is, String filename, String folderName, Map<String, Object> params) {
    GridFSInputFile f = fs.createFile(is);
    f.setFilename(filename);//from  w w  w.j  a v a2  s  .c  o  m
    setParams(f, folderName, params);
    f.save();
    return f;
}

From source file:oecp.framework.fs.gridfs.GridxFS.java

License:Apache License

public GridFSInputFile save(byte[] data, String filename, String folderName, Map<String, Object> params) {
    GridFSInputFile f = fs.createFile(data);
    f.setFilename(filename);//w w w . j  a  v  a 2  s  .c  o m
    setParams(f, folderName, params);
    f.save();
    return f;
}

From source file:org.alfresco.serializers.MongoFilesImpl.java

License:Open Source License

@Override
public DBObject createFile(long nodeId, long nodeVersion, String propertyName, byte[] bytes) {
    GridFSInputFile f = myFS.createFile(bytes);
    f.put("nodeId", nodeId);
    f.put("nodeVersion", nodeVersion);
    f.put("propertyQName", propertyName);
    f.save();
    DBObject dbObject = BasicDBObjectBuilder.start("type", "serialized").add("id", f.getId()).get();
    return dbObject;
}

From source file:org.apache.camel.component.gridfs.GridFsProducer.java

License:Apache License

public void process(Exchange exchange) throws Exception {
    String operation = endpoint.getOperation();
    if (operation == null) {
        operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
    }/*from   w  ww.j  av  a2 s  .  c o m*/
    if (operation == null || "create".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class);

        InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
        GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
        if (chunkSize != null && chunkSize > 0) {
            gfsFile.setChunkSize(chunkSize);
        }
        final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        if (ct != null) {
            gfsFile.setContentType(ct);
        }
        String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class);
        DBObject dbObject = (DBObject) JSON.parse(metaData);
        gfsFile.setMetaData(dbObject);
        gfsFile.save();
        exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename());
    } else if ("remove".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        endpoint.getGridFs().remove(filename);
    } else if ("findOne".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        GridFSDBFile file = endpoint.getGridFs().findOne(filename);
        if (file != null) {
            exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
            exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
            exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
            exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
            exchange.getIn().setBody(file.getInputStream(), InputStream.class);
        } else {
            throw new FileNotFoundException("No GridFS file for " + filename);
        }
    } else if ("listAll".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class);
    } else if ("count".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(cursor.count(), Integer.class);
    }

}

From source file:org.aw20.mongoworkbench.command.GridFSPutFileCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);
    DB db = mdb.getDB(sDb);/*from w  ww  .  j  a  va  2s. c o m*/

    GridFS gfs = new GridFS(db, sColl.substring(0, sColl.lastIndexOf(".")));

    GridFSInputFile gridFSInputFile = gfs.createFile(getFile);
    gridFSInputFile.setContentType(MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(getFile));
    gridFSInputFile.save();

    setMessage("fileLoaded=" + getFile + "; size=" + getFile.length());
}

From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java

License:Apache License

/**
 * Saves a file to the database by file name. This is used during a form upload. We use tika to
 * determine the content type./*from w w w .  j  a  v  a 2s .  c  om*/
 *
 * TODO: Refactor this mess
 *
 * @param databaseName the name of the database
 * @param bucketName the name of the bucket
 * @param fileName the name of the file
 * @param overwrite whether to overwrite an existing file with the same name
 * @param stream the file byte stream
 * @return the Mongo ID of the file
 * @throws DatasourceException
 * @throws ExistsException
 * @throws NotFoundException
 */
@Override
public String addByForm(String databaseName, String bucketName, String fileName, boolean overwrite,
        InputStream stream) throws DatasourceException, ExistsException, NotFoundException {
    String fileId = null;
    try {
        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        GridFS gfsBucket = new GridFS(mongoDatabase, bucketName);
        GridFSDBFile gfsFile = gfsBucket.findOne(fileName);
        if (gfsFile == null) {
            // the file does not exist -- create
            GridFSInputFile dbFile = gfsBucket.createFile(stream, fileName);
            dbFile.setContentType(tika.detect(fileName));
            dbFile.save();
            fileId = dbFile.getId().toString();
        } else {
            // the file exists
            if (overwrite) {
                // overwrite the existing file
                gfsBucket.remove(gfsFile);
                GridFSInputFile inputFile = gfsBucket.createFile(stream, fileName);
                inputFile.setContentType(tika.detect(fileName));
                inputFile.save();
                fileId = inputFile.getId().toString();
            } else {
                throw new ExistsException("The file already exists in the bucket");
            }
        }
    } catch (MongoException ex) {
        logger.error("Could not persist entity to bucket", ex);
        throw new DatasourceException("Could not persist file to bucket");
    }
    if (fileId == null || fileId.isEmpty()) {
        throw new DatasourceException("Could not persist file to bucket");
    }
    return fileId;
}