generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StreamingFileUploadRequest (#424)
* avoid calling request.getInputStream() when the InputStream is already in BlobParallelUploadOptions
- Loading branch information
Showing
6 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...age-core/src/main/java/io/micronaut/objectstorage/request/StreamingFileUploadRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2017-2024 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.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.multipart.StreamingFileUpload; | ||
|
||
/** | ||
* | ||
* An {@link UploadRequest} backed by a {@link StreamingFileUpload}. | ||
* @since 2.7.0 | ||
*/ | ||
public class StreamingFileUploadRequest implements UploadRequest { | ||
|
||
@NonNull | ||
private final StreamingFileUpload streamingFileUpload; | ||
@NonNull | ||
private final String key; | ||
|
||
@NonNull | ||
private Map<String, String> metadata; | ||
|
||
public StreamingFileUploadRequest(@NonNull StreamingFileUpload streamingFileUpload) { | ||
this(streamingFileUpload, streamingFileUpload.getName(), Collections.emptyMap()); | ||
} | ||
|
||
public StreamingFileUploadRequest(@NonNull StreamingFileUpload streamingFileUpload, @NonNull String key) { | ||
this(streamingFileUpload, key, Collections.emptyMap()); | ||
} | ||
|
||
public StreamingFileUploadRequest(@NonNull StreamingFileUpload streamingFileUpload, @NonNull String key, @NonNull Map<String, String> metadata) { | ||
this.streamingFileUpload = streamingFileUpload; | ||
this.key = key; | ||
this.metadata = metadata; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Optional<String> getContentType() { | ||
return streamingFileUpload.getContentType() | ||
.map(MediaType::getName); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String getKey() { | ||
return key; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Optional<Long> getContentSize() { | ||
return Optional.of(streamingFileUpload.getSize()); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public InputStream getInputStream() { | ||
return streamingFileUpload.asInputStream(); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public Map<String, String> getMetadata() { | ||
return this.metadata; | ||
} | ||
|
||
@Override | ||
public void setMetadata(@NonNull Map<String, String> metadata) { | ||
this.metadata = metadata; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ge-core/src/test/groovy/io/micronaut/objectstorage/request/StreamingFileUploadSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.micronaut.objectstorage.request | ||
|
||
import io.micronaut.http.server.HttpServerConfiguration | ||
import io.micronaut.http.server.netty.MicronautHttpData | ||
import io.micronaut.http.server.netty.multipart.NettyStreamingFileUpload | ||
import io.micronaut.objectstorage.ObjectStorageOperationsSpecification | ||
import io.netty.buffer.Unpooled | ||
import io.netty.handler.codec.http.multipart.FileUpload | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
import java.nio.charset.Charset | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Path | ||
|
||
import static io.micronaut.objectstorage.ObjectStorageOperationsSpecification.TEXT | ||
|
||
@Subject(StreamingFileUploadRequest) | ||
class StreamingFileUploadSpec extends Specification { | ||
|
||
void "it can be created from a streaming file upload"() { | ||
given: | ||
NettyStreamingFileUpload streamingFileUpload = createStreamingFileUpload() | ||
|
||
when: | ||
StreamingFileUploadRequest request = | ||
UploadRequest.fromStreamingFileUpload(streamingFileUpload) as StreamingFileUploadRequest | ||
|
||
then: | ||
assertThatRequestIsValid(request, streamingFileUpload.name) | ||
} | ||
|
||
void "it can be created from a streaming file upload and key"() { | ||
given: | ||
NettyStreamingFileUpload streamingFileUpload = createStreamingFileUpload() | ||
String key = "path/tp=o/file.txt" | ||
|
||
when: | ||
StreamingFileUploadRequest request = | ||
UploadRequest.fromStreamingFileUpload(streamingFileUpload, key) as StreamingFileUploadRequest | ||
|
||
then: | ||
assertThatRequestIsValid(request, key) | ||
|
||
} | ||
|
||
private NettyStreamingFileUpload createStreamingFileUpload() { | ||
HttpServerConfiguration.MultipartConfiguration cfg = new HttpServerConfiguration.MultipartConfiguration() | ||
cfg.mixed = true | ||
FileUpload fileUpload = new MicronautHttpData.Factory(cfg, StandardCharsets.UTF_8).createFileUpload(null, "test-file.txt", "test-file.txt", "text/plain", "chunked", Charset.defaultCharset(), TEXT.length()) | ||
|
||
return new NettyStreamingFileUpload.Factory(cfg, null).create(fileUpload, null) | ||
|
||
} | ||
|
||
private void assertThatRequestIsValid(StreamingFileUploadRequest request, String expectedKey) { | ||
request.with { | ||
assert key == expectedKey | ||
assert contentType.present | ||
assert contentType.get() == "text/plain" | ||
|
||
} | ||
} | ||
} |