Example usage for com.mongodb.gridfs GridFS remove

List of usage examples for com.mongodb.gridfs GridFS remove

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS remove.

Prototype

public void remove(final DBObject query) 

Source Link

Document

Removes all files matching the given query.

Usage

From source file:org.craftercms.social.services.impl.SupportDataAccessImpl.java

License:Open Source License

@Override
public void removeAttachment(ObjectId attachmentId) {
    try {/*from  w  w w.  java 2  s  .  co  m*/
        GridFS gFS = new GridFS(mongoTemplate.getDb());
        gFS.remove(attachmentId);
    } catch (Exception e) {
        log.error("Can not stream file.", e);
    }
}

From source file:org.exist.mongodb.xquery.gridfs.Remove.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/* w  w  w .  j a  v a 2 s  .co  m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String bucket = args[2].itemAt(0).getStringValue();
        String documentId = args[3].itemAt(0).getStringValue();

        // Get database
        DB db = client.getDB(dbname);

        // Create a GridFS instance for the specified bucket
        GridFS gfs = new GridFS(db, bucket);

        // Remove document by id or by filename
        if (isCalledAs(REMOVE_BY_OBJECTID)) {
            gfs.remove(new ObjectId(documentId));
        } else {
            gfs.remove(documentId);
        }

        return new StringValue(documentId);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage());
    }

}

From source file:org.obiba.magma.datasource.mongodb.MongoDBValueTable.java

License:Open Source License

/**
 * Drop the files from the {@link com.mongodb.gridfs.GridFS} for this table.
 *///from   w  w  w  .  ja  v a2s  .c o  m
private void dropFiles() {
    GridFS gridFS = getMongoDBDatasource().getMongoDBFactory().getGridFS();
    BasicDBObjectBuilder metaDataQuery = BasicDBObjectBuilder.start() //
            .add("metadata.datasource", getDatasource().getName()) //
            .add("metadata.table", getName());
    gridFS.remove(metaDataQuery.get());
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public FileStorage.FileWriteBean createFileWriteBean(Id id, FileStorage.FileMetaBean meta) {
    meta.setLastModified(new Date());
    meta.setCreated(new Date());
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    fs.remove(id.getEntity());

    GridFSInputFile gfsFile = fs.createFile(id.getEntity());
    gfsFile.setContentType(meta.getContentType());
    gfsFile.setMetaData(MongoDBFormat.fromMap(meta.toMap()));

    GridFSFileWriteBean gridFSFileWriteBean = new GridFSFileWriteBean(id, gfsFile.getOutputStream(), meta);
    gridFSFileWriteBean.gfsFile = gfsFile;
    return gridFSFileWriteBean;
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public void remove(Id id) {
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    fs.remove(id.getEntity());
    if (LOG.isDebugEnabled())
        LOG.debug("File removed successfully:" + id);
}

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

License:Open Source License

@Override
public void deleteAvatar(String userName) {
    GridFS avatarFS = new GridFS(m_template.getDb());
    avatarFS.remove(String.format(AVATAR_NAME, userName));
}

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;/*w ww.  j  ava2s . co 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:rapture.blob.mongodb.GridFSBlobHandler.java

License:Open Source License

@Override
public Boolean storeBlob(CallingContext context, String docPath, InputStream newContent, Boolean append) {
    GridFS gridFS = getGridFS();
    GridFSInputFile file;/*from   w w  w .  j  a v  a  2  s .co  m*/
    if (!append) {
        gridFS.remove(docPath);
        file = createNewFile(docPath, newContent);
    } else {
        GridFSDBFile existing = gridFS.findOne(docPath);
        if (existing != null) {
            try {
                file = updateExisting(context, docPath, newContent, gridFS, existing);
            } catch (IOException e) {
                file = null;
                log.error(String.format("Error while appending to docPath %s: %s", docPath,
                        ExceptionToString.format(e)));
            }

        } else {
            file = createNewFile(docPath, newContent);
        }
    }
    return file != null;
}

From source file:rapture.blob.mongodb.GridFSBlobHandler.java

License:Open Source License

protected GridFSInputFile updateExisting(CallingContext context, String docPath, InputStream newContent,
        GridFS gridFS, GridFSDBFile existing) throws IOException {
    GridFSInputFile file;//from  w ww . j  ava 2s.c o  m
    String lockKey = createLockKey(gridFS, docPath);
    LockHandle lockHandle = grabLock(context, lockKey);
    try {
        File tempFile = File.createTempFile("rapture", "blob");
        existing.writeTo(tempFile);
        FileInputStream tempIn = FileUtils.openInputStream(tempFile);
        SequenceInputStream sequence = new SequenceInputStream(tempIn, newContent);
        try {
            gridFS.remove(docPath);
            file = createNewFile(docPath, sequence);
            if (!tempFile.delete()) {
                log.warn(String.format("Unable to delete temp file created while appending docPath %s, at %s",
                        docPath, tempFile.getAbsolutePath()));
            }
        } finally {
            try {
                sequence.close();
            } catch (IOException e) {
                log.error(
                        String.format("Error closing sequence input stream: %s", ExceptionToString.format(e)));
            }
            try {
                tempIn.close();
            } catch (IOException e) {
                log.error(
                        String.format("Error closing sequence input stream: %s", ExceptionToString.format(e)));
            }
        }
    } finally {
        releaseLock(context, lockKey, lockHandle);
    }
    return file;
}

From source file:rapture.blob.mongodb.GridFSBlobHandler.java

License:Open Source License

@Override
public Boolean deleteBlob(CallingContext context, String docPath) {
    GridFS gridFS = getGridFS();
    String lockKey = createLockKey(gridFS, docPath);
    LockHandle lockHandle = grabLock(context, lockKey);
    boolean retVal = false;
    try {//from w  ww .  ja v  a2 s  .  c o  m
        if (gridFS.findOne(docPath) != null) {
            gridFS.remove(docPath);
            retVal = true;
        }
    } finally {
        releaseLock(context, lockKey, lockHandle);
    }
    return retVal;
}