Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.objectstorage.ObjectStorageException;
import io.micronaut.objectstorage.ObjectStorageOperations;
import io.micronaut.objectstorage.configuration.ToggeableCondition;
import io.micronaut.objectstorage.request.InputStreamUploadRequest;
import io.micronaut.objectstorage.response.UploadResponse;
import io.micronaut.objectstorage.request.BytesUploadRequest;
import io.micronaut.objectstorage.request.FileUploadRequest;
Expand Down Expand Up @@ -223,6 +224,9 @@ protected RequestBody getRequestBody(@NonNull UploadRequest uploadRequest) {
} else if (uploadRequest instanceof BytesUploadRequest) {
BytesUploadRequest request = (BytesUploadRequest) uploadRequest;
return RequestBody.fromBytes(request.getBytes());
} else if (uploadRequest instanceof InputStreamUploadRequest) {
InputStreamUploadRequest request = (InputStreamUploadRequest) uploadRequest;
return RequestBody.fromInputStream(request.getInputStream(), request.getContentSize().orElse(0L));
} else {
byte[] inputBytes = inputStreamMapper.toByteArray(uploadRequest.getInputStream());
return RequestBody.fromBytes(inputBytes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.micronaut.objectstorage.aws

import io.micronaut.objectstorage.InputStreamMapper
import io.micronaut.objectstorage.request.InputStreamUploadRequest
import software.amazon.awssdk.services.s3.S3Client
import spock.lang.Specification
class AwsS3OperationsSpec extends Specification {

def 'should create RequestBody from input stream upload request with content size'() {
given:
def inputStream = new ByteArrayInputStream('Stream content'.bytes)
def uploadRequest = new InputStreamUploadRequest(inputStream, 'key', 'application/json', 12L)
def operations = new AwsS3Operations(Mock(AwsS3Configuration), Mock(S3Client), Mock(InputStreamMapper))

when:
def requestBody = operations.getRequestBody(uploadRequest)

then:
requestBody.optionalContentLength().get() == 12L
requestBody.contentStreamProvider().name() == 'Stream'
}

def 'should create RequestBody from input stream upload request without content size'() {
given:
def inputStream = new ByteArrayInputStream('Stream content'.bytes)
def uploadRequest = new InputStreamUploadRequest(inputStream, 'key', 'application/json', null)
def inputStreamMapper = Mock(InputStreamMapper)
inputStreamMapper.toByteArray(inputStream) >> 'Stream content'.bytes
def operations = new AwsS3Operations(Mock(AwsS3Configuration), Mock(S3Client), inputStreamMapper)

when:
def requestBody = operations.getRequestBody(uploadRequest)

then:
requestBody.optionalContentLength().get() == 0
requestBody.contentStreamProvider().name() == 'Stream'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.micronaut.objectstorage.request;

import java.io.InputStream;
import java.util.Optional;

/**
*
* An {@link UploadRequest} backed by a {@link InputStream}.
* @author Munish Chouhan
* @since 2.9.1
*/
public class InputStreamUploadRequest implements UploadRequest {

private final InputStream inputStream;
private final String key;
private final String contentType;
private final Long contentLength;

public InputStreamUploadRequest(InputStream inputStream, String key, String contentType, Long contentLength) {
this.inputStream = inputStream;
this.key = key;
this.contentType = contentType;
this.contentLength = contentLength;
}

@Override
public Optional<String> getContentType() {
return Optional.ofNullable(contentType);
}

@Override
public String getKey() {
return key;
}

@Override
public Optional<Long> getContentSize() {
return Optional.ofNullable(contentLength);
}

@Override
public InputStream getInputStream() {
return inputStream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.micronaut.objectstorage.request

import spock.lang.Specification
import spock.lang.Subject

@Subject(InputStreamUploadRequest)
class InputStreamUploadRequestSpec extends Specification {
def 'should return content type when it is set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', 'application/json', 123L)

when:
def contentType = request.getContentType()

then:
contentType.isPresent()
contentType.get() == 'application/json'
}

def 'should return empty optional when content type is not set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', null, 123L)

when:
def contentType = request.getContentType()

then:
!contentType.isPresent()
}

def 'should return key when it is set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', 'application/json', 123L)

when:
def key = request.getKey()

then:
key == 'key'
}

def 'should return content size when it is set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', 'application/json', 123L)

when:
def contentSize = request.getContentSize()

then:
contentSize.isPresent()
contentSize.get() == 123L
}

def 'should return empty optional when content size is not set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', 'application/json', null)

when:
def contentSize = request.getContentSize()

then:
!contentSize.isPresent()
}

def 'should return input stream when it is set'() {
given:
def inputStream = Mock(InputStream)
def request = new InputStreamUploadRequest(inputStream, 'key', 'application/json', 123L)

when:
def resultStream = request.getInputStream()

then:
resultStream == inputStream
}

def 'should return empty metadata when none is set'() {
given:
def request = new InputStreamUploadRequest(Mock(InputStream), 'key', 'application/json', 123L)

when:
def metadata = request.getMetadata()

then:
metadata.isEmpty()
}
}