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:com.imaginea.mongodb.services.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for dropping all files from a GridFS bucket.
 *
 * @param dbName     Name of Database/*  www .j av  a  2 s .  c o  m*/
 * @param bucketName Name of GridFS Bucket
 * @returns Status message.
 */
public String dropBucket(String dbName, String bucketName)
        throws DatabaseException, DocumentException, ValidationException {
    mongoInstance = mongoInstanceProvider.getMongoInstance();
    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database name is null");

    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }

    if (bucketName == null) {
        throw new EmptyCollectionNameException("Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new EmptyCollectionNameException("Bucket Name Empty");
    }

    String result = null;
    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST");
        }

        GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);

        gridFS.remove(new BasicDBObject());

    } catch (MongoException e) {
        throw new DeleteDocumentException("FILES_DELETION_EXCEPTION");
    }
    result = "All files in [" + bucketName + "] have been deleted";
    return result;
}

From source file:com.spring.tutorial.controllers.DefaultController.java

@RequestMapping(value = "/my-drive/delete", method = RequestMethod.POST)
public @ResponseBody String delete(HttpServletRequest request) throws UnknownHostException, Exception {
    MongoData mongoData = new MongoData();
    DB db = mongoData.getDB();/*from  w  w  w  . ja  v a2 s.c o m*/
    DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_files_meta");

    BasicDBObject document = new BasicDBObject();
    String fileID = request.getParameter("fileID").toString();
    document.put("file-id", new ObjectId(fileID));
    collection.remove(document);

    document = new BasicDBObject();
    GridFS gfs = new GridFS(db, request.getSession().getAttribute("id").toString() + "_files.files");
    document.put("_id", new ObjectId(fileID));
    gfs.remove(document);

    collection.remove(document);

    return fileID;
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Removes a file (with all its versions) from the specified name space.
 * @param namespace - (optional) name space where the file was stored under. When nothing specified, the default bucket is used
 * @param filename - filename to be removed from the database
 *//*from ww  w. j a va 2  s.  c  o  m*/
public void removeFile(final @Nullable String namespace, final String filename) {
    checkArgument(isNotBlank(filename), "Uninitialized or invalid filename");
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db);
    gfsNs.remove(filename);
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Deletes the latest version from the database. When a previous version exists for the file in the specified namespace, the latest
 * uploaded version will be the new latest version.
 * @param namespace - (optional) name space where the file was stored under. When nothing specified, the default bucket is used
 * @param filename - filename to be removed from the database
 *//*w  w  w .j  av  a  2 s .co m*/
public void undoLatestVersion(final @Nullable String namespace, final String filename) {
    checkArgument(isNotBlank(filename), "Uninitialized or invalid filename");
    final String filename2 = filename.trim();
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final GridFS gfsNs = isNotBlank(namespace) ? new GridFS(db, namespace.trim()) : new GridFS(db);
    try {
        // remove latest version from the database            
        gfsNs.remove(new BasicDBObject(FILE_VERSION_PROP, filename2));
    } finally {
        // enforce versioning property by always restoring the latest version in the database
        restoreLatestVersion(gfsNs, filename2);
    }
}

From source file:fr.wseduc.gridfs.GridFSPersistor.java

License:Apache License

private void removeFile(Message<Buffer> message, JsonObject json) {
    JsonObject query = json.getObject("query");
    if (query == null) {
        return;/*from  w  w w. j  av  a2 s.co m*/
    }
    GridFS fs = new GridFS(db, bucket);
    try {
        fs.remove(jsonToDBObject(query));
        replyOK(message, null);
    } catch (MongoException e) {
        replyError(message, e.getMessage());
    }
}

From source file:fr.wseduc.resizer.GridFsFileAccess.java

License:Apache License

@Override
public void write(String dest, ImageFile img, Handler<String> handler) {
    String[] path = parsePath(dest);
    if (path == null || path.length < 1) {
        handler.handle(null);//from w w w . j a  v a2  s  .c o m
        return;
    }
    String id;
    if (path.length == 2 && path[1] != null && !path[1].trim().isEmpty()) {
        id = path[1];
    } else {
        id = UUID.randomUUID().toString();
    }
    GridFS fs = new GridFS(db, path[0]);
    try {
        saveFile(img, id, fs);
    } catch (DuplicateKeyException e) {
        fs.remove(new BasicDBObject("_id", id));
        saveFile(img, id, fs);
    }
    handler.handle(id);
}

From source file:in.mtap.iincube.mongoapi.GridFsUpdater.java

License:Apache License

@Override
public Boolean execute() {
    GridFS gridFS = getGridFs();
    gridFS.remove(filename);
    GridFSInputFile file = gridFS.createFile(stream);
    file.setContentType(contentType);/*from  w  w  w . j  av a 2s. c  o  m*/
    file.setFilename(filename);
    file.save();
    return true;
}

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

License:Open Source License

public void delete(RequestContext ctx, Responder responder) throws Exception {
    GridFSDBObject info = fileInfo();//from   w w  w .j  av  a2  s.  c om
    if (info.getId() == null) {
        responder.noSuchResource(id());
        return;
    }
    GridFS gridfs = getUserspace().getGridFS();
    gridfs.remove(info.getId());
    info.dbObject().put("length", 0L);
    info.dbObject().put("contentType", null);
    responder.resourceDeleted(this);
}

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;//from w  w  w.j ava  2s  .  c  o m
    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);
    }
    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:me.yyam.mongodbutils.MongoDbOperater.java

/**
 * GridFS/*from  w  w w  .  j  av a2  s  . c o m*/
 * @param dbName ???
 * @param fsName GridFS???fsnull?
 * @param queryMap GridFS?
 */
public void removeFsFile(String dbName, String fsName, Map queryMap) {
    DB db = mongoClient.getDB(dbName);
    if (fsName == null) {
        fsName = "fs";
    }
    GridFS fs = new GridFS(db, fsName);
    DBObject query = new BasicDBObject(queryMap);
    fs.remove(query);
}