public GridFsResource[] getResources(String locationPattern) { if (!StringUtils.hasText(locationPattern)) { return new GridFsResource[0]; } AntPath path = new AntPath(locationPattern); if (path.isPattern()) { GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); List<GridFsResource> resources = new ArrayList<>(); for (GridFSFile file : files) { resources.add(getResource(file)); } return resources.toArray(new GridFsResource[0]); } return new GridFsResource[] { getResource(locationPattern) }; }
public GridFsResource[] getResources(String locationPattern) { if (!StringUtils.hasText(locationPattern)) { return new GridFsResource[0]; } AntPath path = new AntPath(locationPattern); if (path.isPattern()) { GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); List<GridFsResource> resources = new ArrayList<>(); for (GridFSFile file : files) { resources.add(getResource(file)); } return resources.toArray(new GridFsResource[0]); } return new GridFsResource[] { getResource(locationPattern) }; }
@RequestMapping(method = RequestMethod.GET) ResponseEntity<Resource> get(String userId) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.IMAGE_JPEG); return new ResponseEntity<>( this.gridFsTemplate.getResource(userId), httpHeaders, HttpStatus.OK); } }
@Override public byte[] getImage(String filename) { GridFsResource gridFsResource = gridFsTemplate.getResource(filename); if (gridFsResource != null) { try { return IOUtils.toByteArray(gridFsResource.getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } } return null; }
@Override public InputStream getContent(S entity) { if (entity == null) return null; Object contentId = BeanUtils.getFieldWithAnnotation(entity, ContentId.class); if (contentId == null) return null; String location = converter.convert(contentId, String.class); Resource resource = gridFs.getResource(location); try { if (resource != null && resource.exists()) { return resource.getInputStream(); } } catch (IOException e) { logger.error(format("Unexpected error getting content for entityt %s", entity), e); throw new StoreAccessException(format("Getting content for entity %s", entity), e); } return null; }
public Photo getDogePhoto(User user, String dogeId) throws IOException { DogePhoto dogePhoto = this.dogePhotoRepository.findOneByIdAndUser(dogeId, user); return () -> this.fs.getResource(dogePhoto.getFileRef()).getInputStream(); }
@Override public void setContent(S property, InputStream content) { Object contentId = BeanUtils.getFieldWithAnnotation(property, ContentId.class); if (contentId == null) { contentId = UUID.randomUUID(); BeanUtils.setFieldWithAnnotation(property, ContentId.class, contentId.toString()); } String location = converter.convert(contentId, String.class); Resource resource = gridFs.getResource(location); if (resource != null && resource.exists()) { gridFs.delete(query(whereFilename().is(resource.getFilename()))); } try { gridFs.store(content, location); resource = gridFs.getResource(location); } catch (Exception e) { logger.error(format("Unexpected error setting content for entity %s", property), e); throw new StoreAccessException(format("Setting content for entity %s", property), e); } long contentLen = 0L; try { contentLen = resource.contentLength(); } catch (IOException ioe) { logger.debug(format("Unable to retrieve content length for %s", contentId)); } BeanUtils.setFieldWithAnnotation(property, ContentLength.class, contentLen); }
public InputStream getInputStream() throws IOException, IllegalStateException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return gridfs.getResource(location).getInputStream(); }
Resource resource = gridFs.getResource(location); if (resource != null && resource.exists()) { gridFs.delete(query(whereFilename().is(resource.getFilename())));