Example usage for com.mongodb.gridfs GridFS GridFS

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

Introduction

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

Prototype

public GridFS(final DB db) 

Source Link

Document

Creates a GridFS instance for the default bucket "fs" in the given database.

Usage

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

License:Apache License

@PostConstruct
public void init() {
    DB db = mongoTemplate.getDb();
    fs = new GridFS(db);
    files = db.getCollection(FS_FILES);
}

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

License:Open Source License

public MongoFilesImpl(DB db) {
    this.myFS = (db != null ? new GridFS(db) : null);
}

From source file:org.chimi.s4s.storage.mongofs.MongoFSFileStorage.java

License:Apache License

@Override
public FileData find(FileId fileId) {
    DB storageDb = mongo.getDB(db);//  ww  w . j  a  v a2  s .  c  om
    GridFS gridFS = new GridFS(storageDb);
    ObjectId id = new ObjectId(fileId.toString());
    GridFSDBFile foundFile = gridFS.find(id);

    if (foundFile == null) {
        return null;
    }
    return new MongoFSFileData(id, foundFile);
}

From source file:org.chimi.s4s.storage.mongofs.MongoFSFileStorage.java

License:Apache License

@Override
public FileId save(File file) throws SaveFailureStorageException {
    try {//from w  w  w. ja  v  a 2s  .c o  m
        DB storageDb = mongo.getDB(db);
        GridFS gridFS = new GridFS(storageDb);
        GridFSInputFile gridFile = gridFS.createFile(file);
        gridFile.save();
        return new FileId(gridFile.getId().toString());
    } catch (IOException e) {
        throw new SaveFailureStorageException(e);
    }
}

From source file:org.cleaner.main.GridFSDupeFinder.java

License:Open Source License

public static void main(String[] args) {
    try (MongoClient mc = new MongoClient()) {
        MongoDatabase database = mc.getDatabase("gridfs");
        FindDuplicatesByMd5 fd = new FindDuplicatesByMd5(new DuplicateLoggerStrategy(),
                new GridFS(mc.getDB("gridfs")));
        fd.find(database.getCollection("fs.files"));
    }//  w  ww . j a  v a2  s  .  c  o  m

}

From source file:org.cleaner.main.GridFSFileLoader.java

License:Open Source License

@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
    try (MongoClient mc = new MongoClient()) {
        DB db = mc.getDB("gridfs");
        GridFS gfs = new GridFS(db);
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(DIRECTORY_TO_LOAD))) {
            for (Path entry : stream) {
                File file = entry.toFile();
                if (file.isDirectory())
                    continue;
                else
                    gfs.createFile(file).save();
            }/*from ww  w.  j  a  v  a2  s.  c  om*/
        }

    }

}

From source file:org.craftercms.commons.mongo.AbstractJongoRepository.java

License:Open Source License

@Required
public void setJongo(final Jongo jongo) {
    this.jongo = jongo;
    this.gridfs = new GridFS(jongo.getDatabase());
}

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

License:Open Source License

@Override
public ObjectId saveFile(MultipartFile file) throws IOException {
    GridFS gFS = new GridFS(mongoTemplate.getDb());
    GridFSInputFile gFSInputFile = gFS.createFile(file.getInputStream());
    gFSInputFile.setFilename(file.getOriginalFilename());
    gFSInputFile.setContentType(file.getContentType());
    gFSInputFile.save();//from  w  w w .  j  a v a 2  s.  c o m
    return (ObjectId) gFSInputFile.getId();
}

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

License:Open Source License

@Override
public void streamAttachment(ObjectId attachmentId, HttpServletResponse response) {
    try {// w ww . j  a  v a  2  s  .com
        GridFS gFS = new GridFS(mongoTemplate.getDb());
        GridFSDBFile file = gFS.find(attachmentId);
        if (file != null) {
            response.setContentType(file.getContentType());
            response.setContentLength((int) file.getLength());
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getFilename());
            file.writeTo(response.getOutputStream());

        } else {
            log.error("Attachment with id {} does not exist", attachmentId);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }

    } catch (Exception e) {
        log.error("Can not stream file.", e);
    }
}

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

License:Open Source License

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