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:org.nuxeo.ecm.core.storage.mongodb.GridFSBinaryManager.java

License:Apache License

@Override
public Binary getBinary(Blob blob) throws IOException {
    if (!(blob instanceof FileBlob)) {
        return super.getBinary(blob); // just open the stream and call getBinary(InputStream)
    }/*ww w .j  a v  a 2 s  . co  m*/
    // we already have a file so can compute the length and digest efficiently
    File file = ((FileBlob) blob).getFile();
    String digest;
    try (InputStream in = new FileInputStream(file)) {
        digest = DigestUtils.md5Hex(in);
    }
    // if the digest is not already known then save to GridFS
    GridFSDBFile dbFile = gridFS.findOne(digest);
    if (dbFile == null) {
        try (InputStream in = new FileInputStream(file)) {
            GridFSInputFile inputFile = gridFS.createFile(in, digest);
            inputFile.save();
        }
    }
    return new GridFSBinary(digest, blobProviderId);
}

From source file:org.nuxeo.ecm.core.storage.mongodb.GridFSBinaryManager.java

License:Apache License

@Override
protected Binary getBinary(InputStream in) throws IOException {
    // save the file to GridFS
    GridFSInputFile inputFile = gridFS.createFile(in, true);
    inputFile.save();
    // now we know length and digest
    String digest = inputFile.getMD5();
    // if the digest is already known then reuse it instead
    GridFSDBFile dbFile = gridFS.findOne(digest);
    if (dbFile == null) {
        // no existing file, set its filename as the digest
        inputFile.setFilename(digest);//from ww  w  . j  a v a  2 s  .  co m
        inputFile.save();
    } else {
        // file already existed, no need for the temporary one
        gridFS.remove(inputFile);
    }
    return new GridFSBinary(digest, blobProviderId);
}

From source file:org.rhq.enterprise.server.plugins.drift.mongodb.dao.FileDAO.java

License:Open Source License

public void save(File file) throws IOException {
    GridFSInputFile inputFile = gridFS.createFile(new BufferedInputStream(new FileInputStream(file)));
    inputFile.put("_id", file.getName());
    inputFile.save();
}

From source file:org.sakaiproject.nakamura.lite.storage.mongo.GridFSContentHelper.java

License:Apache License

public Map<String, Object> writeBody(String keySpace, String columnFamily, String contentId,
        String contentBlockId, String streamId, Map<String, Object> content, InputStream in)
        throws IOException, StorageClientException {
    String path = getPath(keySpace, columnFamily, contentBlockId);
    GridFSInputFile file = contentBodies.createFile(in, path);
    file.save();
    LOGGER.debug("Wrote {} bytes to {} as body of {}:{}:{} stream {} ",
            new Object[] { file.getLength(), path, keySpace, columnFamily, contentBlockId, streamId });
    Map<String, Object> metadata = Maps.newHashMap();
    metadata.put(StorageClientUtils.getAltField(InternalContent.LENGTH_FIELD, streamId), file.getLength());
    metadata.put(StorageClientUtils.getAltField(InternalContent.BLOCKID_FIELD, streamId), contentBlockId);
    metadata.put(StorageClientUtils.getAltField(STORE_LOCATION_FIELD, streamId), path);
    return metadata;
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@POST
@Path("/oremap")
@Consumes(MediaType.APPLICATION_JSON)/*from   w  ww.j a v  a2s .c  om*/
@Produces(MediaType.APPLICATION_JSON)
public Response addOreMap(@QueryParam("objectId") String id, String oreMapString) {
    ObjectId objectId = new ObjectId(id);
    String fileName = "ore-file";
    ByteArrayInputStream inputStream = new ByteArrayInputStream(oreMapString.getBytes());
    GridFSInputFile gfsFile = oreMapBucket.createFile(inputStream, fileName, true);
    gfsFile.setId(objectId);
    gfsFile.save();
    return Response.ok().build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@PUT
@Path("/copyoremaps")
public Response copyOreMaps() {
    FindIterable<Document> iter = oreMapCollection.find();
    MongoCursor<Document> cursor = iter.iterator();
    while (cursor.hasNext()) {
        Document document = cursor.next();
        ObjectId objectId = new ObjectId((String) document.get("_id").toString());
        document.remove("_id");
        String fileName = "ore-file";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(document.toJson().getBytes());
        GridFSInputFile gfsFile = oreMapBucket.createFile(inputStream, fileName, true);
        gfsFile.setId(objectId);/*  w w w.j  a  va2  s. c  o  m*/
        gfsFile.save();
    }
    return Response.ok().build();
}

From source file:org.sipfoundry.commons.userdb.profile.UserProfileServiceImpl.java

License:Open Source License

@Override
public void saveAvatar(String userName, InputStream originalIs) throws AvatarUploadException {
    ByteArrayOutputStream os = null;
    InputStream is = null;//from  ww w  .  ja  va2s .c o m
    try {
        BufferedImage originalImage = ImageIO.read(originalIs);
        BufferedImage thumbnail = Thumbnails.of(originalImage).crop(Positions.CENTER).size(128, 128)
                .asBufferedImage();
        os = new ByteArrayOutputStream();
        ImageIO.write(thumbnail, "png", os);
        is = new ByteArrayInputStream(os.toByteArray());
        String fileName = String.format(AVATAR_NAME, userName);
        GridFS avatarFS = new GridFS(m_template.getDb());
        avatarFS.remove(fileName);
        GridFSInputFile gfsFile = avatarFS.createFile(is);
        gfsFile.setFilename(fileName);
        gfsFile.save();
    } catch (Exception ex) {
        throw new AvatarUploadException(ex);
    } finally {
        IOUtils.closeQuietly(originalIs);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.springframework.data.mongodb.gridfs.GridFsTemplate.java

License:Apache License

public GridFSFile store(InputStream content, String filename, String contentType, DBObject metadata) {

    Assert.notNull(content);/*from   w w  w . ja v a 2s  .c  o m*/

    GridFSInputFile file = getGridFs().createFile(content);

    if (filename != null) {
        file.setFilename(filename);
    }

    if (metadata != null) {
        file.setMetaData(metadata);
    }

    if (contentType != null) {
        file.setContentType(contentType);
    }

    file.save();
    return file;
}

From source file:org.teiid.translator.mongodb.MongoDBExecutionFactory.java

License:Open Source License

/**
 * Mongodb only supports certain data types, Teiid need to serialize them in other compatible
 * formats, and convert them back while reading them.
 * @param value//from  w  w w  .  j av a2s .com
 * @return
 */
public Object convertToMongoType(Object value, DB mongoDB, String fqn) throws TranslatorException {
    if (value == null) {
        return null;
    }

    try {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).doubleValue();
        } else if (value instanceof BigInteger) {
            return ((BigInteger) value).doubleValue();
        } else if (value instanceof Character) {
            return ((Character) value).toString();
        } else if (value instanceof java.sql.Date) {
            return new java.util.Date(((java.sql.Date) value).getTime());
        } else if (value instanceof java.sql.Time) {
            return new java.util.Date(((java.sql.Time) value).getTime());
        } else if (value instanceof java.sql.Timestamp) {
            return new java.util.Date(((java.sql.Timestamp) value).getTime());
        } else if (value instanceof BinaryType) {
            return new Binary(((BinaryType) value).getBytes());
        } else if (value instanceof byte[]) {
            return new Binary((byte[]) value);
        } else if (value instanceof Blob) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((Blob) value).getBinaryStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof Clob) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((Clob) value).getAsciiStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof SQLXML) {
            String uuid = UUID.randomUUID().toString();
            GridFS gfs = new GridFS(mongoDB, fqn);
            GridFSInputFile gfsFile = gfs.createFile(((SQLXML) value).getBinaryStream());
            gfsFile.setFilename(uuid);
            gfsFile.save();
            return uuid;
        } else if (value instanceof Object[]) {
            BasicDBList list = new BasicDBList();
            for (Object obj : (Object[]) value) {
                list.add(obj);
            }
            return list;
        }
        return value;
    } catch (SQLException e) {
        throw new TranslatorException(e);
    }
}

From source file:org.waveprotocol.box.server.persistence.mongodb.MongoDbStore.java

License:Apache License

@Override
public void storeAttachment(AttachmentId attachmentId, InputStream data) throws IOException {
    GridFSInputFile file = getAttachmentGrid().createFile(data, attachmentId.serialise());

    try {//from   ww  w  . j a  v a2 s . c  om
        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;
        }
    }
}