Example usage for io.vertx.core.file FileProps size

List of usage examples for io.vertx.core.file FileProps size

Introduction

In this page you can find the example usage for io.vertx.core.file FileProps size.

Prototype

long size();

Source Link

Document

The size of the file, in bytes

Usage

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private void sendFile(RoutingContext context, String file, FileProps fileProps) {
    HttpServerRequest request = context.request();

    Long offset = null;//from ww w  .  j  a  va2s.  c  o m
    Long end = null;
    MultiMap headers = null;

    if (rangeSupport) {
        // check if the client is making a range request
        String range = request.getHeader("Range");
        // end byte is length - 1
        end = fileProps.size() - 1;

        if (range != null) {
            Matcher m = RANGE.matcher(range);
            if (m.matches()) {
                try {
                    String part = m.group(1);
                    // offset cannot be empty
                    offset = Long.parseLong(part);
                    // offset must fall inside the limits of the file
                    if (offset < 0 || offset >= fileProps.size()) {
                        throw new IndexOutOfBoundsException();
                    }
                    // length can be empty
                    part = m.group(2);
                    if (part != null && part.length() > 0) {
                        // ranges are inclusive
                        end = Long.parseLong(part);
                        // offset must fall inside the limits of the file
                        if (end < offset || end >= fileProps.size()) {
                            throw new IndexOutOfBoundsException();
                        }
                    }
                } catch (NumberFormatException | IndexOutOfBoundsException e) {
                    context.fail(REQUESTED_RANGE_NOT_SATISFIABLE.code());
                    return;
                }
            }
        }

        // notify client we support range requests
        headers = request.response().headers();
        headers.set("Accept-Ranges", "bytes");
        // send the content length even for HEAD requests
        headers.set("Content-Length", Long.toString(end + 1 - (offset == null ? 0 : offset)));
    }

    writeCacheHeaders(request, fileProps);

    if (request.method() == HttpMethod.HEAD) {
        request.response().end();
    } else {
        if (rangeSupport && offset != null) {
            // must return content range
            headers.set("Content-Range", "bytes " + offset + "-" + end + "/" + fileProps.size());
            // return a partial response
            request.response().setStatusCode(PARTIAL_CONTENT.code());

            // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set
            // classloader (if any).
            final Long finalOffset = offset;
            final Long finalEnd = end;
            wrapInTCCLSwitch(() -> request.response().sendFile(file, finalOffset, finalEnd + 1, res2 -> {
                if (res2.failed()) {
                    context.fail(res2.cause());
                }
            }), null);
        } else {
            // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set
            // classloader (if any).
            wrapInTCCLSwitch(() -> request.response().sendFile(file, res2 -> {
                if (res2.failed()) {
                    context.fail(res2.cause());
                }
            }), null);
        }
    }
}

From source file:org.entcore.common.storage.impl.FileStorage.java

License:Open Source License

@Override
public void fileStats(String id, Handler<AsyncResult<FileStats>> handler) {
    try {/*  w  ww  .java  2s. c  o  m*/
        fs.props(getPath(id), res -> {
            if (res.succeeded()) {
                FileProps props = res.result();
                handler.handle(new DefaultAsyncResult<>(
                        new FileStats(props.creationTime(), props.lastModifiedTime(), props.size())));
            } else {
                handler.handle(new DefaultAsyncResult<>(res.cause()));
                log.error(res.cause().getMessage(), res.cause());
            }
        });
    } catch (FileNotFoundException e) {
        handler.handle(new DefaultAsyncResult<>(e));
        log.warn(e.getMessage(), e);
    }
}