builder.startObject("content"); builder.field("_content_type", file.getContentType()); builder.field("_title", file.getFilename()); builder.field("_content", encodedContent); builder.endObject(); builder.field("filename", file.getFilename()); builder.field("contentType", file.getContentType()); builder.field("md5", file.getMD5());
f.writeTo(f.getFilename()); return;
GridFSDBFile file = grid.findOne(new ObjectId(objectId)); if (file != null) { logger.trace("Caught file: {} - {}", file.getId(), file.getFilename()); object = file; } else {
DBObject query; //write appropriate query List<GridFSDBFile> fileList = gfs.find(query); for (GridFSDBFile f : fileList) { gfs.remove(f.getFilename()); // if you have not set fileName and // your _id is of ObjectId type, then you can use //gfs.remove((ObjectId) file.getId()); }
@Override public String getFileId() { return mongoFile.getFilename(); }
@RequestMapping(value = "/getFileById/{id}", method = RequestMethod.GET) public @ResponseBody void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws IOException { organizationFileAttachmentService.setUser(getUser()); GridFSDBFile file = organizationFileAttachmentService.getGridFSDBFileById(new ObjectId(id), "File"); if (file != null) { try { response.setContentType(file.getContentType()); response.setContentLength((new Long(file.getLength()).intValue())); response.setHeader("content-Disposition", "attachment; filename=" + file.getFilename());// "attachment;filename=test.xls" // copy it to response's OutputStream IOUtils.copyLarge(file.getInputStream(), response.getOutputStream()); } catch (IOException ex) { _logger.info("Error writing file to output stream. Filename was '" + id + "'"); throw new RuntimeException("IOError writing file to output stream"); } } }
jsonText.append("/"+jsonObject.toString()+"/"); } else if (jsonObject instanceof GridFSDBFile) { jsonText.append("/"+((GridFSDBFile)jsonObject).getFilename()+"/"); } else { throw new RuntimeException("couldn't pretty print "+jsonObject.getClass().getName());
jsonText.append("/"+jsonObject.toString()+"/"); } else if (jsonObject instanceof GridFSDBFile) { jsonText.append("/"+((GridFSDBFile)jsonObject).getFilename()+"/"); } else { throw new RuntimeException("couldn't pretty print "+jsonObject.getClass().getName());
@Override public List<String> getDocumentFileNames(DatabaseDocument<MongoType> d) { MongoDocument md = (MongoDocument)d; DBObject query = QueryBuilder.start(DOCUMENT_KEY).is(md.getID().getID()).get(); ArrayList<String> list = new ArrayList<String>(); List<GridFSDBFile> files = documentfs.find(query); for(GridFSDBFile file : files) { list.add(file.getFilename()); } return list; }
@GET @Path("/download/{id}/") @Produces("application/epub") public Response getImagefile(@PathParam("content") String content, @PathParam("id") String imageID) throws UnknownHostException, IOException { GridFSDBFile findImage; DB db = getConnection().getDB(content); GridFS gfsImage = new GridFS(db, "image"); findImage = gfsImage.findOne(new ObjectId(imageID)); String filename = findImage.getFilename(); ResponseBuilder response = Response.ok((Object) findImage.getInputStream()); response.header("Content-Disposition", "attachment; filename=" + filename+".jpg"); return response.build(); }
f.writeTo(f.getFilename()); return;
@Override public MutableBlobMetadata apply(GridFSDBFile input) { MutableBlobMetadata metadata = new MutableBlobMetadataImpl(); MutableContentMetadata contentMetadata = metadata.getContentMetadata(); String contentType = input.getContentType(); if (contentType != null) { contentMetadata.setContentType(contentType); } contentMetadata.setContentLength(input.getLength()); metadata.setETag(input.getMD5()); metadata.setLastModified(input.getUploadDate()); metadata.setName(input.getFilename()); metadata.getUserMetadata().putAll(input.getMetaData().toMap()); // TODO: support populating metadata.getContentMetadata().setContentMD5() return metadata; } }
@Override public ISObject get(String key) { GridFSDBFile file = findFile(key); if (null == file) { // try legacy file = findFile(BlobStorageService.getLegacyKey(key)); if (null != file) { Logger.warn("You have legacy blob data, please consider migrating them to new version"); } } if (null == file) { return null; } ISObject sobj = SObject.of(key, IO.readContent(file.getInputStream())); String fn = file.getFilename(); if (S.empty(fn)) { fn = S.random(8); } String type = file.getContentType(); if (S.empty(type)) { type = "application/octet-stream"; } return sobj.setAttribute(Blob.FILENAME, fn) .setAttribute(Blob.CONTENT_TYPE, type); }
private DocumentFile<MongoType> getDocumentFile(DocumentID<MongoType> id, String filename) { DBObject query = QueryBuilder.start(DOCUMENT_KEY).is(id.getID()).and(FILENAME_KEY).is(filename).get(); GridFSDBFile file = documentfs.findOne(query); if(file==null) { return null; } DocumentFile<MongoType> df = new DocumentFile<MongoType>(id, file.getFilename(), file.getInputStream(), (String)file.get(STAGE_KEY), file.getUploadDate()); if(file.containsField(MIMETYPE_KEY)) { df.setMimetype((String) file.get(MIMETYPE_KEY)); } if(file.containsField(ENCODING_KEY)) { df.setEncoding((String) file.get(ENCODING_KEY)); } return df; }