Search in sources :

Example 1 with DataSize

use of cn.taketoday.util.DataSize in project today-infrastructure by TAKETODAY.

the class NumberToDataSizeConverterTests method convert.

@SuppressWarnings({ "rawtypes", "unchecked" })
private DataSize convert(ConversionService conversionService, Integer source, DataUnit defaultUnit) {
    TypeDescriptor targetType = mock(TypeDescriptor.class);
    if (defaultUnit != null) {
        DataSizeUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", defaultUnit), DataSizeUnit.class, null);
        given(targetType.getAnnotation(DataSizeUnit.class)).willReturn(unitAnnotation);
    }
    given(targetType.getType()).willReturn((Class) DataSize.class);
    return (DataSize) conversionService.convert(source, TypeDescriptor.fromObject(source), targetType);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) DataSize(cn.taketoday.util.DataSize) DataSizeUnit(cn.taketoday.format.annotation.DataSizeUnit)

Example 2 with DataSize

use of cn.taketoday.util.DataSize in project today-infrastructure by TAKETODAY.

the class AbstractMultipartResolver method resolveInternal.

/**
 * @throws FileSizeExceededException upload file size exceeded
 * @see MultipartConfig#getMaxRequestSize()
 */
@Override
protected Object resolveInternal(RequestContext context, ResolvableMethodParameter parameter) throws Throwable {
    if (context.isMultipart()) {
        DataSize maxRequestSize = getMultipartConfig().getMaxRequestSize();
        // exceed max size?
        if (maxRequestSize.toBytes() < context.getContentLength()) {
            throw new FileSizeExceededException(maxRequestSize, null, DataSize.ofBytes(context.getContentLength()));
        }
        MultiValueMap<String, MultipartFile> multipartFiles = context.getMultipartRequest().getMultipartFiles();
        return resolveInternal(context, parameter, multipartFiles);
    }
    return null;
}
Also used : MultipartFile(cn.taketoday.web.multipart.MultipartFile) DataSize(cn.taketoday.util.DataSize) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException)

Example 3 with DataSize

use of cn.taketoday.util.DataSize in project today-framework by TAKETODAY.

the class MultipartIterator method obtainNext.

/**
 * @throws NotMultipartRequestException if this request is not of type multipart/form-data
 * @throws MultipartException multipart parse failed
 */
public RequestPart obtainNext(LightHttpConfig config, MultipartConfiguration multipartConfig) throws IOException {
    hasNext = false;
    // 先解析 header
    // Content-Disposition
    final String contentDispositionString = Utils.readLine(inputStream);
    // Content-Type
    final String contentType = Utils.readLine(inputStream);
    // final HttpHeaders httpHeaders = Utils.readHeaders(inputStream, config);
    final MultipartInputStream inputStream = this.inputStream;
    final int partSize = inputStream.tail - inputStream.head;
    final DataSize maxFileSize = multipartConfig.getMaxFileSize();
    if (partSize > maxFileSize.toBytes()) {
        throw new FileSizeExceededException(maxFileSize, null).setActual(DataSize.ofBytes(partSize));
    }
    final ContentDisposition contentDisposition = ContentDisposition.parse(contentDispositionString);
    // if (httpHeaders.containsKey(Constant.CONTENT_TYPE)) {
    if (StringUtils.isNotEmpty(contentType)) {
        if (partSize > config.getMaxMultipartInMemSize()) {
            final String tempLocation = multipartConfig.getLocation();
            final File tempFileDir = new File(tempLocation);
            final String randomString = StringUtils.generateRandomString(10);
            final File tempFile = new File(tempFileDir, randomString);
            // save to temp file
            try (final FileOutputStream fileOutput = new FileOutputStream(tempFile)) {
                final int bufferSize = config.getMultipartBufferSize();
                // readTimes > 1
                final int readTimes = partSize / bufferSize;
                if (readTimes == 0) {
                    // part size 太小了 直接一次性读完
                    byte[] buffer = Utils.readBytes(inputStream, partSize);
                    fileOutput.write(buffer, 0, partSize);
                    final LightMultipartFile multipartFile = new LightMultipartFile(tempFile, contentDisposition, contentType, partSize);
                    multipartFile.setCachedBytes(buffer);
                    return multipartFile;
                } else {
                    // 分次读取
                    byte[] buffer = new byte[bufferSize];
                    int bytesRead = 0;
                    for (int i = 0; i < readTimes; i++) {
                        bytesRead += inputStream.read(buffer, 0, bufferSize);
                        fileOutput.write(buffer, 0, bytesRead);
                    }
                    // 读取剩余字节
                    buffer = Utils.readBytes(inputStream, partSize - bytesRead);
                    fileOutput.write(buffer);
                    return new LightMultipartFile(tempFile, contentDisposition, contentType, partSize);
                }
            }
        } else {
            final byte[] bytes = Utils.readBytes(inputStream, partSize);
            // inputStream memory
            return new LightMultipartFile(bytes, contentDisposition, contentType, partSize);
        }
    } else {
        final String name = contentDisposition.getName();
        final byte[] bytes = Utils.readBytes(inputStream, partSize);
        return new FieldRequestPart(bytes, name);
    }
}
Also used : ContentDisposition(cn.taketoday.http.ContentDisposition) DataSize(cn.taketoday.util.DataSize) FileOutputStream(java.io.FileOutputStream) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException) File(java.io.File)

Example 4 with DataSize

use of cn.taketoday.util.DataSize in project today-framework by TAKETODAY.

the class UndertowWebServerFactoryCustomizer method mapUndertowProperties.

private void mapUndertowProperties(ConfigurableUndertowWebServerFactory factory, ServerOptions serverOptions) {
    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
    ServerProperties.Undertow properties = serverProperties.getUndertow();
    map.from(properties::getBufferSize).whenNonNull().asInt(DataSize::toBytes).to(factory::setBufferSize);
    ServerProperties.Undertow.Threads threadProperties = properties.getThreads();
    map.from(threadProperties::getIo).to(factory::setIoThreads);
    map.from(threadProperties::getWorker).to(factory::setWorkerThreads);
    map.from(properties::getDirectBuffers).to(factory::setUseDirectBuffers);
    map.from(properties::getMaxHttpPostSize).as(DataSize::toBytes).when(this::isPositive).to(serverOptions.option(UndertowOptions.MAX_ENTITY_SIZE));
    map.from(properties::getMaxParameters).to(serverOptions.option(UndertowOptions.MAX_PARAMETERS));
    map.from(properties::getMaxHeaders).to(serverOptions.option(UndertowOptions.MAX_HEADERS));
    map.from(properties::getMaxCookies).to(serverOptions.option(UndertowOptions.MAX_COOKIES));
    map.from(properties::isAllowEncodedSlash).to(serverOptions.option(UndertowOptions.ALLOW_ENCODED_SLASH));
    map.from(properties::isDecodeUrl).to(serverOptions.option(UndertowOptions.DECODE_URL));
    map.from(properties::getUrlCharset).as(Charset::name).to(serverOptions.option(UndertowOptions.URL_CHARSET));
    map.from(properties::isAlwaysSetKeepAlive).to(serverOptions.option(UndertowOptions.ALWAYS_SET_KEEP_ALIVE));
    map.from(properties::getNoRequestTimeout).asInt(Duration::toMillis).to(serverOptions.option(UndertowOptions.NO_REQUEST_TIMEOUT));
    map.from(properties.getOptions()::getServer).to(serverOptions.forEach(serverOptions::option));
    SocketOptions socketOptions = new SocketOptions(factory);
    map.from(properties.getOptions()::getSocket).to(socketOptions.forEach(socketOptions::option));
}
Also used : ServerProperties(cn.taketoday.framework.web.server.ServerProperties) DataSize(cn.taketoday.util.DataSize) PropertyMapper(cn.taketoday.util.PropertyMapper)

Example 5 with DataSize

use of cn.taketoday.util.DataSize in project today-framework by TAKETODAY.

the class UndertowWebServerFactoryCustomizer method customize.

@Override
public void customize(ConfigurableUndertowWebServerFactory factory) {
    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
    ServerOptions options = new ServerOptions(factory);
    map.from(serverProperties::getMaxHttpHeaderSize).asInt(DataSize::toBytes).when(this::isPositive).to(options.option(UndertowOptions.MAX_HEADER_SIZE));
    mapUndertowProperties(factory, options);
    mapAccessLogProperties(factory);
    map.from(this::getOrDeduceUseForwardHeaders).to(factory::setUseForwardHeaders);
}
Also used : DataSize(cn.taketoday.util.DataSize) PropertyMapper(cn.taketoday.util.PropertyMapper)

Aggregations

DataSize (cn.taketoday.util.DataSize)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)2 DataSizeUnit (cn.taketoday.format.annotation.DataSizeUnit)2 FileSizeExceededException (cn.taketoday.http.FileSizeExceededException)2 PropertyMapper (cn.taketoday.util.PropertyMapper)2 ServerProperties (cn.taketoday.framework.web.server.ServerProperties)1 ContentDisposition (cn.taketoday.http.ContentDisposition)1 MultipartFile (cn.taketoday.web.multipart.MultipartFile)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1