private void verifyExists() throws FileNotFoundException { if (!exists()) { throw new FileNotFoundException(String.format("%s does not exist.", getDescription())); } } }
@Override public String getDescription() { return String.format("GridFs resource [%s]", this.getFilename()); }
/** * Obtain an absent {@link ReactiveGridFsResource}. * * @param filename filename of the absent resource, must not be {@literal null}. * @return never {@literal null}. * @since 2.1 */ public static ReactiveGridFsResource absent(String filename) { Assert.notNull(filename, "Filename must not be null"); return new ReactiveGridFsResource(filename, Flux.empty()); }
@Override public long contentLength() throws IOException { verifyExists(); return file.getLength(); }
@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 Mono<ReactiveGridFsResource> getResource(GridFSFile file) { Assert.notNull(file, "GridFSFile must not be null!"); return Mono.fromSupplier(() -> { GridFSDownloadStream stream = getGridFs().openDownloadStream(file.getObjectId()); return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, dataBufferFactory)); }); }
@Override public long lastModified() throws IOException { verifyExists(); return file.getUploadDate().getTime(); }
/** * Retrieve the download stream. * * @return */ public Flux<DataBuffer> getDownloadStream() { if (!exists()) { return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription()))); } return content; }
/** * Returns the {@link Resource}'s id. * * @return never {@literal null}. * @throws IllegalStateException if the file does not {@link #exists()}. */ public Object getId() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return file.getId(); }