public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's metadata. * * @return */ public static GridFsCriteria whereMetaData() { return new GridFsCriteria("metadata"); }
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 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)); }
@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(); }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's name. * * @return */ public static GridFsCriteria whereFilename() { return new GridFsCriteria("filename"); }
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) }; }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's content type. * * @return */ public static GridFsCriteria whereContentType() { return new GridFsCriteria("metadata.".concat(GridFsResource.CONTENT_TYPE_FIELD)); } }
/** * Find by name query * * @param filename Filename * @return Mongo's Query */ private Query findByFilenameQuery(String filename) { return query(whereFilename().is(filename)); } }
/** * Creates a {@link GridFsCriteria} for restrictions on a single file's metadata item. * * @param metadataKey * @return */ public static GridFsCriteria whereMetaData(@Nullable String metadataKey) { String extension = metadataKey == null ? "" : "." + metadataKey; return new GridFsCriteria(String.format("metadata%s", extension)); }
@Override public void delete() { if (gridfs.findOne(query(whereFilename().is(location))) == null) { return; } gridfs.delete(query(whereFilename().is(location))); } }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's metadata. * * @return */ public static GridFsCriteria whereMetaData() { return new GridFsCriteria("metadata"); }
public boolean exists() { return gridfs.findOne(query(whereFilename().is(location))) != null; }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's name. * * @return */ public static GridFsCriteria whereFilename() { return new GridFsCriteria("filename"); }
public long lastModified() throws IOException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return -1L; } return file.getUploadDate().getTime(); }
/** * Creates a {@link GridFsCriteria} for restrictions on the file's content type. * * @return */ public static GridFsCriteria whereContentType() { return new GridFsCriteria("metadata.".concat(GridFsResource.CONTENT_TYPE_FIELD)); } }
public long contentLength() throws IOException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return 0L; } return file.getLength(); }
/** * Creates a {@link GridFsCriteria} for restrictions on a single file's metadata item. * * @param metadataKey * @return */ public static GridFsCriteria whereMetaData(@Nullable String metadataKey) { String extension = metadataKey == null ? "" : "." + metadataKey; return new GridFsCriteria(String.format("metadata%s", extension)); }
public String getContentType() { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return file.getContentType(); }