List of usage examples for com.mongodb.gridfs GridFSInputFile setId
public void setId(final Object id)
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; 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);/*from w w w.j a v a 2 s .co m*/ } 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:org.craftercms.commons.mongo.AbstractJongoRepository.java
License:Open Source License
@Override public FileInfo saveFile(final InputStream inputStream, final String storeName, final String contentType, final ObjectId fileId) throws MongoDataException, FileExistsException { try {/*www .j a va 2 s. c o m*/ if (gridfs.findOne(storeName) != null) { log.error("A file named {} already exists", storeName); throw new FileExistsException("File with name " + storeName + " already Exists"); } GridFSInputFile savedFile = gridfs.createFile(inputStream, storeName, true); savedFile.setContentType(contentType); if (fileId != null) { log.debug("Saving file with given Id {} probably a update", fileId); savedFile.setId(fileId); } savedFile.save(); FileInfo fileInfo = new FileInfo(savedFile, false); log.debug("File {} was saved " + fileInfo); return fileInfo; } catch (MongoException ex) { log.error("Unable to save file"); throw new MongoDataException("Unable to save file to GridFs", ex); } }
From source file:org.kurento.repository.internal.repoimpl.mongo.MongoRepository.java
License:Apache License
@Override public RepositoryItem createRepositoryItem(String id) { // TODO The file is not written until outputstream is closed. There is a // potentially data race with this unique test if (!gridFS.find(id).isEmpty()) { throw new DuplicateItemException(id); }/*from w w w . j av a 2s .c om*/ GridFSInputFile dbFile = gridFS.createFile(id); dbFile.setId(id); return createRepositoryItem(dbFile); }
From source file:org.seadpdt.impl.ROServicesImpl.java
License:Apache License
@POST @Path("/oremap") @Consumes(MediaType.APPLICATION_JSON)//from w ww . j a va 2 s . 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); gfsFile.save();//from w w w .ja v a 2s .c om } return Response.ok().build(); }
From source file:rmi_video.VideoServer.java
@Override public boolean addVideo(VideoData d) throws RemoteException { try {//w w w .j a va 2s . c o m //Conexao com mongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + d.getFileName())) { fos.write(d.getData()); } File videoFile = new File("/Users/philcr/Documents/" + d.getFileName()); GridFS gfsVideo = new GridFS(db, "video"); //Cria e salva o arquivo no DB pelo GridFS GridFSInputFile gfsFile = gfsVideo.createFile(videoFile); gfsFile.setId(new ObjectId()); //Utiliza a criao de ID do mongo gfsFile.put("videoId", d.getId()); //Utiliza nosso metodo de ID gfsFile.setFilename(d.getFileName()); gfsFile.save(); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("File could not be deleted!"); } mongoClient.close(); return true; } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } }
From source file:streamflow.datastore.mongodb.impl.MongoGridFsFileContentDao.java
License:Apache License
@Override public FileContent save(FileContent entity) { GridFSInputFile inputFile = gridFs.createFile(entity.getData()); inputFile.setId(entity.getId()); inputFile.save();// www . ja v a2 s. c o m return entity; }
From source file:streamflow.datastore.mongodb.impl.MongoGridFsFileContentDao.java
License:Apache License
@Override public FileContent update(FileContent entity) { GridFSInputFile inputFile = gridFs.createFile(entity.getData()); inputFile.setId(entity.getId()); inputFile.save();// w w w . ja va2 s . co m return entity; }