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, final String bucket) 

Source Link

Document

Creates a GridFS instance for the specified bucket in the given database.

Usage

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public String saveFile(byte[] fileData, String fileName, String contentType) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    GridFSInputFile gfsFile = gridFs.createFile(fileData);

    gfsFile.setFilename(fileName);//w w w .  j av a  2  s  .com
    gfsFile.setContentType(contentType);
    gfsFile.save();

    LOG.info("Saved new file :" + fileName);

    return gfsFile.getId().toString();
}

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public InputStream getFileAsStream(String fileId) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    GridFSDBFile savedFile = gridFs.find(new ObjectId(fileId));

    return savedFile.getInputStream();
}

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public GridFSDBFile getFile(String fileId) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    return gridFs.find(new ObjectId(fileId));
}

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

@RequestMapping(value = "/files/{id}", method = RequestMethod.GET)
public String getFile(@PathVariable("id") String id, ModelMap map, HttpServletRequest request,
        HttpServletResponse response) throws IOException, Exception {

    String _id = request.getSession().getAttribute("id").toString();
    MongoData data = new MongoData();
    GridFS collection = new GridFS(data.getDB(), _id + "_files");

    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(id));
    GridFSDBFile file = collection.findOne(query);

    DBCollection metaFileCollection = data.getDB().getCollection(_id + "_files_meta");
    BasicDBObject metaQuery = new BasicDBObject();
    metaQuery.put("file-id", new ObjectId(id));
    DBObject metaFileDoc = metaFileCollection.findOne(metaQuery);
    MongoFile metaFile = new MongoFile(metaFileDoc);

    ServletOutputStream out = response.getOutputStream();
    String mimeType = metaFile.getType();
    response.setContentType(mimeType);//w  ww .j a  v a 2  s.c o  m
    response.setContentLength((int) file.getLength());
    String headerKey = "Content-Disposition";

    File f = File.createTempFile(file.getFilename(),
            metaFile.getType().substring(metaFile.getType().indexOf("/") + 1));
    String headerValue = String.format("attachment; filename=\"%s\"", file.getFilename());
    response.setHeader(headerKey, headerValue);
    file.writeTo(f);

    FileInputStream inputStream = new FileInputStream(f);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;

    while (inputStream.read(buffer, 0, 4096) != -1) {
        out.write(buffer, 0, 4096);
    }
    inputStream.close();
    out.flush();
    out.close();

    return "mydrive/temp";
}

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  . j  ava 2 s .  c om*/
    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:com.spring.tutorial.entitites.FileUploader.java

public String upload() throws IOException, ServletException, FacebookException {
    OutputStream output = null;/*from w  ww. j  ava2s  .c  o m*/
    InputStream fileContent = null;
    final Part filePart;

    final File file;
    try {
        filePart = request.getPart("file");

        fileContent = filePart.getInputStream();

        MongoClient mongoClient = new MongoClient();
        mongoClient = new MongoClient();
        DB db = mongoClient.getDB("fou");

        char[] pass = "mongo".toCharArray();
        boolean auth = db.authenticate("admin", pass);

        file = File.createTempFile("fileToStore", "tmp");

        file.deleteOnExit();
        FileOutputStream fout = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = fileContent.read(bytes)) != -1) {
            fout.write(bytes, 0, read);
        }

        GridFS gridFS = new GridFS(db, request.getSession().getAttribute("id") + "_files");
        GridFSInputFile gfsInputFile = gridFS.createFile(file);
        gfsInputFile.setFilename(filePart.getSubmittedFileName());
        gfsInputFile.save();

        DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_files_meta");
        BasicDBObject metaDocument = new BasicDBObject();
        metaDocument.append("name", filePart.getSubmittedFileName());
        metaDocument.append("size", filePart.getSize());
        metaDocument.append("content-type", filePart.getContentType());
        metaDocument.append("file-id", gfsInputFile.getId());
        metaDocument.append("tags", request.getParameter("tags"));
        metaDocument.append("description", request.getParameter("description"));

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        metaDocument.append("last_modified", dateFormat.format(new Date()));

        collection.insert(metaDocument);

    } catch (Exception e) {
        return "message:" + e.getMessage();
    } finally {
        if (output != null) {
            output.close();
        }
        if (fileContent != null) {
            fileContent.close();
        }
    }

    return "success";
}

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

License:Open Source License

public MongoFileSystem(DB db, String bucket) {
    Assert.notNull(db, "DB must not be null");
    Assert.notNull(bucket, "Bucket must not be null");
    this.fs = new GridFS(db, bucket);
}

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

License:Open Source License

/**
 * Create a new {@link MongoFolder} using the specified mongo database and bucket.
 * //from   ww w. j a  v  a 2  s .  c  o m
 * @param db the mongo database
 * @param bucket the bucket
 */
public MongoFolder(DB db, String bucket) {
    Assert.notNull(db, "DB must not be null");
    Assert.notNull(bucket, "Bucket must not be null");
    GridFS fs = new GridFS(db, bucket);
    this.store = new MongoFolderStore(fs, new JailedResourcePath());
}

From source file:DataBase.JavaMongoDB.java

/**
 *
 * @param path/*  www .  j a v a  2s.c  om*/
 * @param Basededatos
 * @param cubo
 * @param nombre
 */
public void InsertarAudio(String path, DB Basededatos, String cubo, String nombre) {
    GridFS gfsAudio;
    GridFSInputFile gfsFile;
    File imageFile;
    imageFile = new File(path);
    gfsAudio = new GridFS(Basededatos, cubo);
    try {
        gfsFile = gfsAudio.createFile(imageFile);
        String newFileName = nombre;
        gfsFile.setFilename(newFileName);
        gfsFile.save();

    } catch (IOException ex) {
        Logger.getLogger(JavaMongoDB.class.getName()).log(Level.SEVERE, null, ex);
    }

    //DBCollection collection = db.getCollection("Audio_meta");
    // collection.insert(info, WriteConcern.SAFE);
    //GridFS gfsAudioConsulta = new GridFS(db, "audio");
    //GridFSDBFile imageForOutput = gfsAudioConsulta.findOne("1073");
    //System.out.println(imageForOutput);
}

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

License:Open Source License

@Override
public VLargeCollection getLargeCollection(String name) {
    DB db = _db.getDB();//w  w w.  j a v  a  2s. c  om
    return new MongoDBVLargeCollection(db.getCollection(name), new GridFS(db, name), this, _db.getCounter());
}