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()); builder.field("length", file.getLength());
@Override public String getContentType() { return mongoFile.getContentType(); }
public void writeFileTo(String key, HttpServletResponse resp) throws IOException { GridFSDBFile out = getFileService().findOne(new ObjectId(key)); if (out == null) { throw new FileNotFoundException(key); } resp.setContentType(out.getContentType()); // This might be a conflict out.writeTo(resp.getOutputStream());
@RequestMapping(value = "/image", method = RequestMethod.GET) @ResponseBody public ResponseEntity<InputStreamResource> getImage() { GridFSDBFile gridFsFile = App.getImageResponse(); return ResponseEntity.ok() .contentLength(gridFsFile.getLength()) .contentType(MediaType.parseMediaType(gridFsFile.getContentType())) .body(new InputStreamResource(gridFsFile.getInputStream())); }
public ResponseEntity<InputStreamResource> downloadUserAvatarImage(@PathVariable Long userId) { GridFSDBFile gridFsFile = fileService.findUserAccountAvatarById(userId); return ResponseEntity.ok() .contentLength(gridFsFile.getLength()) .contentType(MediaType.parseMediaType(gridFsFile.getContentType())) .body(new InputStreamResource(gridFsFile.getInputStream())); }
@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"); } } }
@Override public List<BinaryData> findByFilename(String filename) { return gridFs.find(findByFilenameQuery(filename)).stream() .map(file -> new BinaryData(file.getContentType(), file.getLength(), file.getInputStream())) .collect(Collectors.toList()); }
@RequestMapping(value=URI_EVENTS + "/image", method=RequestMethod.GET) public @ResponseBody HttpEntity<byte[]> getEventImage(@RequestParam String id) { GridFSDBFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(id))); String contentType = null; Long fileLength = null; InputStream is = null; if(null != file) { is = file.getInputStream(); fileLength = file.getLength(); contentType = file.getContentType(); } byte[] bytes = null; try { bytes = IOUtils.readFully(is, -1, true); } catch (Exception ex) { ex.printStackTrace(); } return ResponseEntity.ok() .contentLength(fileLength) .contentType(MediaType.parseMediaType(contentType)) .body(bytes); }
@Override public Artifact getArtifact(DBKey dbKey, String objectID) { Artifact artifact = null; GridFS gfs = getGridFS(dbKey); BasicDBObject query = new BasicDBObject(); query.put(ID_FIELD_NAME, new ObjectId(objectID)); GridFSDBFile file = gfs.findOne(query); if (file != null) { artifact = new Artifact(file.getInputStream(), file.getContentType()); } return artifact; }
@Override public Artifact getArtifact(DBKey dbKey, String objectID) { Artifact artifact = null; GridFS gfs = getGridFS(dbKey); BasicDBObject query = new BasicDBObject(); query.put(ID_FIELD_NAME, new ObjectId(objectID)); GridFSDBFile file = gfs.findOne(query); if (file != null) { artifact = new Artifact(file.getInputStream(), file.getContentType()); } return artifact; }
if (file != null) { exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData())); exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType()); exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength()); exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
@Override public BinaryData fetchData(String dataId) { GridFSDBFile file = gridFs.findOne(findByIdQuery(new ObjectId(dataId))); return null == file ? null : new BinaryData(file.getContentType(), file.getLength(), file.getInputStream()); }
@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; } }
exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType()); exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength()); exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
@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); }