public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
@Override public Mono<ReactiveGridFsResource> getResource(String location) { Assert.notNull(location, "Filename must not be null!"); return findOne(query(whereFilename().is(location))).flatMap(this::getResource) .defaultIfEmpty(ReactiveGridFsResource.absent(location)); }
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) }; }
@Override public Flux<ReactiveGridFsResource> getResources(String locationPattern) { if (!StringUtils.hasText(locationPattern)) { return Flux.empty(); } AntPath path = new AntPath(locationPattern); if (path.isPattern()) { Flux<GridFSFile> files = find(query(whereFilename().regex(path.toRegex()))); return files.flatMap(this::getResource); } return getResource(locationPattern).flux(); }
public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
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) }; }
/** * Find by name query * * @param filename Filename * @return Mongo's Query */ private Query findByFilenameQuery(String filename) { return query(whereFilename().is(filename)); } }
@Override public void delete() { if (gridfs.findOne(query(whereFilename().is(location))) == null) { return; } gridfs.delete(query(whereFilename().is(location))); } }
public long lastModified() throws IOException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return -1L; } return file.getUploadDate().getTime(); }
public boolean exists() { return gridfs.findOne(query(whereFilename().is(location))) != null; }
public long contentLength() throws IOException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return 0L; } return file.getLength(); }
public Object getId() { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return file.getId(); }
public String getContentType() { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return file.getContentType(); }
protected void deleteOnFirstWrite() { if (firstWrite) { try { gridfs.delete(query(whereFilename().is(resource.getFilename()))); } finally { firstWrite = false; } } }
@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())));