/** * Open a {@link java.nio.channels.FileChannel} from a path and stream * {@link ByteBuf} chunks with a given maximum size into the returned {@link ByteBufFlux} * * @param path the path to the resource to stream * @param maxChunkSize the maximum per-item ByteBuf size * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromPath(Path path, int maxChunkSize) { return fromPath(path, maxChunkSize, ByteBufAllocator.DEFAULT); }
/** * Open a {@link java.nio.channels.FileChannel} from a path and stream * {@link ByteBuf} chunks with a default maximum size of 500K into * the returned {@link ByteBufFlux} * * @param path the path to the resource to stream * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromPath(Path path) { return fromPath(path, MAX_CHUNK_SIZE); }
/** * Open a {@link java.nio.channels.FileChannel} from a path and stream * {@link ByteBuf} chunks with a default maximum size of 500K into the returned * {@link ByteBufFlux}, using the provided {@link ByteBufAllocator}. * * @param path the path to the resource to stream * @param allocator the channel {@link ByteBufAllocator} * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromPath(Path path, ByteBufAllocator allocator) { return fromPath(path, MAX_CHUNK_SIZE, allocator); }
/** * Listen for HTTP GET on the passed path to be used as a routing condition. The * file on the provided {@link Path} is served. * <p> * Additional regex matching is available e.g. * "/test/{param}". Params are resolved using {@link HttpServerRequest#param(CharSequence)} * * @param uri The {@link HttpPredicate} to use to trigger the route, pattern matching * and capture are supported * @param path the Path to the file to serve * @param interceptor a channel pre-intercepting handler e.g. for content type header * * @return this {@link HttpServerRoutes} */ default HttpServerRoutes file(Predicate<HttpServerRequest> uri, Path path, Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(path, "path"); return route(uri, (req, resp) -> { if (!Files.isReadable(path)) { return resp.send(ByteBufFlux.fromPath(path)); } if (interceptor != null) { return interceptor.apply(resp) .sendFile(path); } return resp.sendFile(path); }); }