Example usage for com.mongodb.gridfs GridFSInputFile put

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

Introduction

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

Prototype

@Override
    public Object put(final String key, final Object v) 

Source Link

Usage

From source file:com.foodtruckdata.mongodb.UsersInput.java

private void storeFile(String filepath, String filetype, ObjectId truck_id) {
    try {/*from www .java2s. co  m*/
        GridFS gridFS = new GridFS(mongoDB);
        InputStream file_stream = getFTPInputStream(filepath);
        StringWriter writer = new StringWriter();
        Charset par = null;
        IOUtils.copy(file_stream, writer, par);
        GridFSInputFile in = gridFS.createFile(file_stream);
        in.setFilename(filepath);
        in.put("TruckID", truck_id);
        in.put("FileType", filetype);
        in.save();
    } catch (IOException ex) {
        Logger.getLogger(UsersInput.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.wavemaker.tools.io.filesystem.mongo.MongoFileSystem.java

License:Open Source License

private GridFSInputFile create(MongoFileSystemKey key, ResourceType type) {
    Assert.notNull(type, "Type must not be null");
    Assert.state(type != ResourceType.DOES_NOT_EXIST);
    GridFSInputFile file = this.fs.createFile(getFilename(key));
    JailedResourcePath parent = key.getPath().getParent();
    if (parent != null) {
        file.put(PARENT, parent.getUnjailedPath().toString());
    }//from  ww  w  . j av  a 2s  . co  m
    file.put(RESOURCE_TYPE, type.name());
    return file;
}

From source file:com.wavemaker.tools.io.mongo.MongoResourceStore.java

License:Open Source License

protected GridFSInputFile create(Type type, boolean createEmptyFile) {
    Assert.notNull(type, "Type must not be null");
    GridFSInputFile file = this.fs.createFile(getFilename(getPath()));
    JailedResourcePath parent = this.path.getParent();
    if (parent != null) {
        file.put(PARENT, parent.getUnjailedPath().toString());
    }/*  w w w  .  j a v a 2 s .  co  m*/
    file.put(RESOURCE_TYPE, type.name());
    if (createEmptyFile) {
        try {
            file.getOutputStream().close();
        } catch (IOException e) {
            throw new ResourceException(e);
        }
    }
    return file;
}

From source file:de.fhg.igd.mongomvcc.impl.DefaultConvertStrategy.java

License:Open Source License

@Override
public long convert(Object data) {
    GridFSInputFile file;
    if (data instanceof byte[]) {
        file = _gridFS.createFile((byte[]) data);
        file.put(BINARY_TYPE, BYTEARRAY);
    } else if (data instanceof float[]) {
        file = _gridFS.createFile(new FloatArrayInputStream((float[]) data));
        file.put(BINARY_TYPE, FLOATARRAY);
    } else if (data instanceof InputStream) {
        file = _gridFS.createFile((InputStream) data);
        file.put(BINARY_TYPE, INPUTSTREAM);
    } else if (data instanceof ByteBuffer) {
        ByteBuffer bb = (ByteBuffer) data;
        byte[] buf;
        if (bb.hasArray()) {
            buf = bb.array();/*from   w w  w .  ja  v a  2 s  .  c  om*/
        } else {
            bb.rewind();
            buf = new byte[bb.remaining()];
            bb.get(buf);
        }
        file = _gridFS.createFile(buf);
        file.put(BINARY_TYPE, BYTEBUFFER);
    } else if (data instanceof FloatBuffer) {
        FloatBuffer bb = (FloatBuffer) data;
        float[] buf;
        if (bb.hasArray()) {
            buf = bb.array();
        } else {
            bb.rewind();
            buf = new float[bb.remaining()];
            bb.get(buf);
        }
        file = _gridFS.createFile(new FloatArrayInputStream(buf));
        file.put(BINARY_TYPE, FLOATBUFFER);
    } else {
        return 0;
    }

    long oid = _counter.getNextId();
    file.put(MongoDBVLargeCollection.OID, oid);
    _convertedFiles.add(file);
    return oid;
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVLargeCollection.java

License:Open Source License

@Override
public void insert(Map<String, Object> obj) {
    DefaultConvertStrategy cs = new DefaultConvertStrategy(_gridFS, getCounter());
    _accessStrategy.setConvertStrategy(cs);
    _accessStrategy.onInsert(obj);//w w  w.j  a  v  a 2 s  . c  o m

    //save original object
    super.insert(obj);

    //save GridFS files
    for (GridFSInputFile file : cs.getConvertedFiles()) {
        file.put(PARENT, obj.get(OID));
        file.save();
    }
}

From source file:io.liveoak.mongo.gridfs.GridFSBlobResource.java

License:Open Source License

private GridFSFilesPathItemResource pushToDB(RequestContext ctx, MediaType contentType, GridFSDBObject fileInfo,
        Supplier contentProvider) throws IOException {
    ObjectId currentId = fileInfo.getId();
    boolean fileExists = currentId != null;

    // update the targeted userspace - hopefully current user has rights to do that
    GridFS gridfs = getUserspace().getGridFS();

    Object content = contentProvider.get();
    GridFSInputFile blob;
    if (fileExists) {
        // here is a time gap when file doesn't exist for a while when being updated.
        // making the switch instantaneous would require another layer of indirection
        // - not using file_id as canonical id, but some other id, mapped to a file.
        // there would still remain a moment between mapping from old file to new file
        // involving two separate file items and a moment in time when during a switch
        // no file would match a filename, nor file id.
        gridfs.remove(currentId);/*from   ww  w. j av  a  2  s  .co m*/
    }
    if (content instanceof File) {
        blob = gridfs.createFile((File) content);
    } else if (content instanceof InputStream) {
        blob = gridfs.createFile((InputStream) content);
    } else if (content instanceof ByteBuf) {
        blob = gridfs.createFile(((ByteBuf) content).array());
    } else {
        throw new IllegalArgumentException("Unsupported value supplied: " + content.getClass());
    }

    // meta data
    if (fileExists) {
        blob.setId(currentId);
    }
    blob.setFilename(fileInfo().getString("filename"));
    blob.setContentType(contentType != null ? contentType.toString() : "application/octet-stream");
    blob.put("parent", fileInfo().getParentId());
    blob.save();

    String oid = blob.getId().toString();

    return new GridFSFilesPathItemResource(ctx, getFilesRoot(), oid, new GridFSDBObject(blob),
            GridFSResourcePath.fromContext(ctx));
}

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

License:Apache License

private void setParams(GridFSInputFile f, Map<String, Object> params) {
    if (params != null) {
        for (Entry<String, Object> entry : params.entrySet()) {
            f.put(entry.getKey(), entry.getValue());
        }//from  w  w w .  j a  v a2 s .c o m
    }
}

From source file:net.ymate.platform.persistence.mongodb.support.GridFSFileBuilder.java

License:Apache License

public GridFSInputFile build(IGridFSSession gridFS) throws Exception {
    GridFSInputFile _inFile = null;
    switch (__type) {
    case 1: // is File
        _inFile = gridFS.getGridFS().createFile((File) __targetObject);
        break;//  w w  w . ja v a  2 s . c o  m
    case 2: // is InputStream
        _inFile = gridFS.getGridFS().createFile((InputStream) __targetObject);
        break;
    case 3: // is Array
        _inFile = gridFS.getGridFS().createFile((byte[]) __targetObject);
    }
    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;
}

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

License:Apache License

private void setParams(GridFSInputFile f, String folderName, Map<String, Object> params) {
    if (folderName != null) {
        if (params == null) {
            params = new HashMap<String, Object>();
        }//from ww w  .j  ava2s  .  c  o m
        params.put(FOLDER, folderName);
    }
    if (params != null) {
        Set<String> keys = params.keySet();
        for (String key : keys) {
            f.put(key, params.get(key));
        }
    }
}

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();/*from w ww  . j  av a2s . co  m*/
    DBObject dbObject = BasicDBObjectBuilder.start("type", "serialized").add("id", f.getId()).get();
    return dbObject;
}