DATAMONGO-625 - Polishing.
Introduce default interface methods where possible. Rename save(…) to store(…) to align with method naming. Reduce constructor visibility to avoid invalid API usage. Replace mutable object in builder with fields to avoid mutation of already built objects when reusing the builder. Remove Options.chunked(…) factory method to avoid confusion with chunkSize method. Reformat code, strip trailing whitespaces. Original pull request: #842.
This commit is contained in:
@@ -21,7 +21,7 @@ import org.springframework.lang.Nullable;
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
|
||||
/**
|
||||
* A common interface when dealing with GridFs items using Spring Data.o
|
||||
* A common interface when dealing with GridFs items using Spring Data.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 3.0
|
||||
@@ -31,7 +31,7 @@ public interface GridFsObject<ID, CONTENT> {
|
||||
/**
|
||||
* The {@link GridFSFile#getId()} value converted into its simple java type. <br />
|
||||
* A {@link org.bson.BsonString} will be converted to plain {@link String}.
|
||||
*
|
||||
*
|
||||
* @return can be {@literal null} depending on the implementation.
|
||||
*/
|
||||
@Nullable
|
||||
@@ -39,34 +39,35 @@ public interface GridFsObject<ID, CONTENT> {
|
||||
|
||||
/**
|
||||
* The filename.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getFilename();
|
||||
|
||||
/**
|
||||
* The actual file content.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* @throws IllegalStateException if the content cannot be obtained.
|
||||
*/
|
||||
CONTENT getContent();
|
||||
|
||||
/**
|
||||
* Additional information like file metadata (eg. contentType).
|
||||
*
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
Options getOptions();
|
||||
|
||||
/**
|
||||
* Additional, context relevant information.
|
||||
*
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
class Options {
|
||||
|
||||
private Document metadata = new Document();
|
||||
private int chunkSize = -1;
|
||||
private final Document metadata;
|
||||
private final int chunkSize;
|
||||
|
||||
private Options(Document metadata, int chunkSize) {
|
||||
|
||||
@@ -83,16 +84,6 @@ public interface GridFsObject<ID, CONTENT> {
|
||||
return new Options(new Document(), -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static factory method to create {@link Options} with given chunk size.
|
||||
*
|
||||
* @param chunkSize
|
||||
* @return new instance of {@link Options}.
|
||||
*/
|
||||
public static Options chunked(int chunkSize) {
|
||||
return new Options(new Document(), chunkSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static factory method to create {@link Options} with given content type.
|
||||
*
|
||||
@@ -115,7 +106,7 @@ public interface GridFsObject<ID, CONTENT> {
|
||||
|
||||
/**
|
||||
* Set the associated content type.
|
||||
*
|
||||
*
|
||||
* @param contentType must not be {@literal null}.
|
||||
* @return new instance of {@link Options}.
|
||||
*/
|
||||
|
||||
@@ -59,7 +59,9 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
* @param metadata can be {@literal null}.
|
||||
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
|
||||
*/
|
||||
ObjectId store(InputStream content, @Nullable Object metadata);
|
||||
default ObjectId store(InputStream content, @Nullable Object metadata) {
|
||||
return store(content, null, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name.
|
||||
@@ -80,7 +82,9 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
* @param contentType can be {@literal null}.
|
||||
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
|
||||
*/
|
||||
ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType);
|
||||
default ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
|
||||
return store(content, filename, contentType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name using the given metadata. The metadata object will be
|
||||
@@ -91,7 +95,9 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
* @param metadata can be {@literal null}.
|
||||
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
|
||||
*/
|
||||
ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata);
|
||||
default ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
|
||||
return store(content, filename, null, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the given content into a file with the given name and content type using the given metadata. The metadata
|
||||
@@ -141,21 +147,21 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
uploadBuilder.metadata(metadata);
|
||||
}
|
||||
|
||||
return save(uploadBuilder.build());
|
||||
return store(uploadBuilder.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the given {@link GridFsObject}, likely a {@link GridFsUpload}, into into a file with given
|
||||
* {@link GridFsObject#getFilename() name}. If the {@link GridFsObject#getFileId()} is set, the file will be stored
|
||||
* with that id, otherwise the server auto creates a new id. <br />
|
||||
*
|
||||
*
|
||||
* @param upload the {@link GridFsObject} (most likely a {@link GridFsUpload}) to be stored.
|
||||
* @param <T> id type of the underlying {@link com.mongodb.client.gridfs.model.GridFSFile}
|
||||
* @return the id of the stored file. Either an auto created value or {@link GridFsObject#getFileId()}, but never
|
||||
* {@literal null}.
|
||||
* @since 3.0
|
||||
*/
|
||||
<T> T save(GridFsObject<T, InputStream> upload);
|
||||
<T> T store(GridFsObject<T, InputStream> upload);
|
||||
|
||||
/**
|
||||
* Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the
|
||||
|
||||
@@ -87,31 +87,6 @@ public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOpe
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public ObjectId store(InputStream content, @Nullable Object metadata) {
|
||||
return store(content, null, metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String)
|
||||
*/
|
||||
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
|
||||
return store(content, filename, contentType, (Object) null);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object)
|
||||
*/
|
||||
public ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
|
||||
return store(content, filename, null, metadata);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String, java.lang.Object)
|
||||
@@ -125,7 +100,7 @@ public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOpe
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#save(org.springframework.data.mongodb.gridfs.GridFsObject)
|
||||
*/
|
||||
public <T> T save(GridFsObject<T, InputStream> upload) {
|
||||
public <T> T store(GridFsObject<T, InputStream> upload) {
|
||||
|
||||
GridFSUploadOptions uploadOptions = computeUploadOptionsFor(upload.getOptions().getContentType(),
|
||||
upload.getOptions().getMetadata());
|
||||
|
||||
@@ -15,42 +15,54 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.gridfs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
|
||||
/**
|
||||
* Upload descriptor for a GridFS file upload.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 3.0
|
||||
*/
|
||||
public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
|
||||
private static final InputStream EMPTY_STREAM = new InputStream() {
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
private final @Nullable ID id;
|
||||
private final Lazy<InputStream> dataStream;
|
||||
private final String filename;
|
||||
private final Options options;
|
||||
|
||||
private ID id;
|
||||
private Lazy<InputStream> dataStream;
|
||||
private String filename;
|
||||
private Options options;
|
||||
private GridFsUpload(@Nullable ID id, Lazy<InputStream> dataStream, String filename, Options options) {
|
||||
|
||||
Assert.notNull(dataStream, "Data Stream must not be null");
|
||||
Assert.notNull(filename, "Filename must not be null");
|
||||
Assert.notNull(options, "Options must not be null");
|
||||
|
||||
this.id = id;
|
||||
this.dataStream = dataStream;
|
||||
this.filename = filename;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link GridFSFile#getId()} value converted into its simple java type. <br />
|
||||
* A {@link org.bson.BsonString} will be converted to plain {@link String}.
|
||||
*
|
||||
*
|
||||
* @return can be {@literal null}.
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsObject#getFileId()
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public ID getFileId() {
|
||||
return id;
|
||||
}
|
||||
@@ -70,7 +82,7 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
@Override
|
||||
public InputStream getContent() {
|
||||
return dataStream.orElse(EMPTY_STREAM);
|
||||
return dataStream.orElse(StreamUtils.emptyInput());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -89,22 +101,22 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
* @return new instance of {@link GridFsUpload}.
|
||||
*/
|
||||
public static GridFsUploadBuilder<ObjectId> fromStream(InputStream stream) {
|
||||
return new GridFsUploadBuilder().content(stream);
|
||||
return new GridFsUploadBuilder<ObjectId>().content(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder to create {@link GridFsUpload} in a fluent way.
|
||||
*
|
||||
*
|
||||
* @param <T> the target id type.
|
||||
*/
|
||||
public static class GridFsUploadBuilder<T> {
|
||||
|
||||
private GridFsUpload upload;
|
||||
private Object id;
|
||||
private Lazy<InputStream> dataStream;
|
||||
private String filename;
|
||||
private Options options = Options.none();
|
||||
|
||||
public GridFsUploadBuilder() {
|
||||
this.upload = new GridFsUpload();
|
||||
this.upload.options = Options.none();
|
||||
}
|
||||
private GridFsUploadBuilder() {}
|
||||
|
||||
/**
|
||||
* Define the content of the file to upload.
|
||||
@@ -114,7 +126,22 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public GridFsUploadBuilder<T> content(InputStream stream) {
|
||||
|
||||
upload.dataStream = Lazy.of(() -> stream);
|
||||
Assert.notNull(stream, "InputStream must not be null");
|
||||
|
||||
return content(() -> stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the content of the file to upload.
|
||||
*
|
||||
* @param stream the upload content.
|
||||
* @return this.
|
||||
*/
|
||||
public GridFsUploadBuilder<T> content(Supplier<InputStream> stream) {
|
||||
|
||||
Assert.notNull(stream, "InputStream Supplier must not be null");
|
||||
|
||||
this.dataStream = Lazy.of(stream);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -127,7 +154,7 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public <T1> GridFsUploadBuilder<T1> id(T1 id) {
|
||||
|
||||
upload.id = id;
|
||||
this.id = id;
|
||||
return (GridFsUploadBuilder<T1>) this;
|
||||
}
|
||||
|
||||
@@ -139,7 +166,7 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public GridFsUploadBuilder<T> filename(String filename) {
|
||||
|
||||
upload.filename = filename;
|
||||
this.filename = filename;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -151,7 +178,9 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public GridFsUploadBuilder<T> options(Options options) {
|
||||
|
||||
upload.options = options;
|
||||
Assert.notNull(options, "Options must not be null");
|
||||
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -163,7 +192,7 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public GridFsUploadBuilder<T> metadata(Document metadata) {
|
||||
|
||||
upload.options = upload.options.metadata(metadata);
|
||||
this.options = this.options.metadata(metadata);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -175,40 +204,42 @@ public class GridFsUpload<ID> implements GridFsObject<ID, InputStream> {
|
||||
*/
|
||||
public GridFsUploadBuilder<T> chunkSize(int chunkSize) {
|
||||
|
||||
upload.options = upload.options.chunkSize(chunkSize);
|
||||
this.options = this.options.chunkSize(chunkSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set id, filename, metadata and chunk size from given file.
|
||||
*
|
||||
*
|
||||
* @param gridFSFile must not be {@literal null}.
|
||||
* @return this.
|
||||
*/
|
||||
public GridFsUploadBuilder<T> gridFsFile(GridFSFile gridFSFile) {
|
||||
|
||||
upload.id = gridFSFile.getId();
|
||||
upload.filename = gridFSFile.getFilename();
|
||||
upload.options = upload.options.metadata(gridFSFile.getMetadata());
|
||||
upload.options = upload.options.chunkSize(gridFSFile.getChunkSize());
|
||||
Assert.notNull(gridFSFile, "GridFSFile must not be null");
|
||||
|
||||
this.id = gridFSFile.getId();
|
||||
this.filename = gridFSFile.getFilename();
|
||||
this.options = this.options.metadata(gridFSFile.getMetadata());
|
||||
this.options = this.options.chunkSize(gridFSFile.getChunkSize());
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content type.
|
||||
*
|
||||
*
|
||||
* @param contentType must not be {@literal null}.
|
||||
* @return this.
|
||||
*/
|
||||
public GridFsUploadBuilder<T> contentType(String contentType) {
|
||||
|
||||
upload.options = upload.options.contentType(contentType);
|
||||
this.options = this.options.contentType(contentType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GridFsUpload<T> build() {
|
||||
return (GridFsUpload<T>) upload;
|
||||
return new GridFsUpload(id, dataStream, filename, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ public interface ReactiveGridFsOperations {
|
||||
uploadBuilder.metadata(metadata);
|
||||
}
|
||||
|
||||
return save(uploadBuilder.build());
|
||||
return store(uploadBuilder.build());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +169,7 @@ public interface ReactiveGridFsOperations {
|
||||
* {@link GridFsObject#getFileId()}.
|
||||
* @since 3.0
|
||||
*/
|
||||
<T> Mono<T> save(GridFsObject<T, Publisher<DataBuffer>> upload);
|
||||
<T> Mono<T> store(GridFsObject<T, Publisher<DataBuffer>> upload);
|
||||
|
||||
/**
|
||||
* Returns a {@link Flux} emitting all files matching the given query. <br />
|
||||
|
||||
@@ -121,7 +121,7 @@ public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements R
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.gridfs.ReactiveGridFsOperations#save(org.springframework.data.mongodb.gridfs.GridFsObject)
|
||||
*/
|
||||
public <T> Mono<T> save(GridFsObject<T, Publisher<DataBuffer>> upload) {
|
||||
public <T> Mono<T> store(GridFsObject<T, Publisher<DataBuffer>> upload) {
|
||||
|
||||
GridFSUploadOptions uploadOptions = computeUploadOptionsFor(upload.getOptions().getContentType(),
|
||||
upload.getOptions().getMetadata());
|
||||
|
||||
@@ -20,19 +20,35 @@ import org.bson.types.ObjectId;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
|
||||
/**
|
||||
* Upload descriptor for a GridFS file upload.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<DataBuffer>> {
|
||||
|
||||
private ID id;
|
||||
private Publisher<DataBuffer> dataStream;
|
||||
private String filename;
|
||||
private Options options;
|
||||
private final @Nullable ID id;
|
||||
private final Publisher<DataBuffer> dataStream;
|
||||
private final String filename;
|
||||
private final Options options;
|
||||
|
||||
private ReactiveGridFsUpload(@Nullable ID id, Publisher<DataBuffer> dataStream, String filename, Options options) {
|
||||
|
||||
Assert.notNull(dataStream, "Data Stream must not be null");
|
||||
Assert.notNull(filename, "Filename must not be null");
|
||||
Assert.notNull(options, "Options must not be null");
|
||||
|
||||
this.id = id;
|
||||
this.dataStream = dataStream;
|
||||
this.filename = filename;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link GridFSFile#getId()} value converted into its simple java type. <br />
|
||||
@@ -42,6 +58,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsObject#getFileId()
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public ID getFileId() {
|
||||
return id;
|
||||
}
|
||||
@@ -80,7 +97,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
* @return new instance of {@link GridFsUpload}.
|
||||
*/
|
||||
public static ReactiveGridFsUploadBuilder<ObjectId> fromPublisher(Publisher<DataBuffer> source) {
|
||||
return new ReactiveGridFsUploadBuilder().content(source);
|
||||
return new ReactiveGridFsUploadBuilder<ObjectId>().content(source);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,13 +107,12 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public static class ReactiveGridFsUploadBuilder<T> {
|
||||
|
||||
ReactiveGridFsUpload upload;
|
||||
private @Nullable Object id;
|
||||
private Publisher<DataBuffer> dataStream;
|
||||
private String filename;
|
||||
private Options options = Options.none();
|
||||
|
||||
public ReactiveGridFsUploadBuilder() {
|
||||
|
||||
this.upload = new ReactiveGridFsUpload();
|
||||
this.upload.options = Options.none();
|
||||
}
|
||||
private ReactiveGridFsUploadBuilder() {}
|
||||
|
||||
/**
|
||||
* Define the content of the file to upload.
|
||||
@@ -105,7 +121,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
* @return this.
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> content(Publisher<DataBuffer> source) {
|
||||
upload.dataStream = source;
|
||||
this.dataStream = source;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -118,7 +134,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public <T1> ReactiveGridFsUploadBuilder<T1> id(T1 id) {
|
||||
|
||||
upload.id = id;
|
||||
this.id = id;
|
||||
return (ReactiveGridFsUploadBuilder<T1>) this;
|
||||
}
|
||||
|
||||
@@ -130,7 +146,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> filename(String filename) {
|
||||
|
||||
upload.filename = filename;
|
||||
this.filename = filename;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -142,7 +158,9 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> options(Options options) {
|
||||
|
||||
upload.options = options;
|
||||
Assert.notNull(options, "Options must not be null");
|
||||
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -154,7 +172,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> metadata(Document metadata) {
|
||||
|
||||
upload.options = upload.options.metadata(metadata);
|
||||
this.options = this.options.metadata(metadata);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -166,7 +184,7 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> chunkSize(int chunkSize) {
|
||||
|
||||
upload.options = upload.options.chunkSize(chunkSize);
|
||||
this.options = this.options.chunkSize(chunkSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -178,10 +196,12 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> gridFsFile(GridFSFile gridFSFile) {
|
||||
|
||||
upload.id = gridFSFile.getId();
|
||||
upload.filename = gridFSFile.getFilename();
|
||||
upload.options = upload.options.metadata(gridFSFile.getMetadata());
|
||||
upload.options = upload.options.chunkSize(gridFSFile.getChunkSize());
|
||||
Assert.notNull(gridFSFile, "GridFSFile must not be null");
|
||||
|
||||
this.id = gridFSFile.getId();
|
||||
this.filename = gridFSFile.getFilename();
|
||||
this.options = this.options.metadata(gridFSFile.getMetadata());
|
||||
this.options = this.options.chunkSize(gridFSFile.getChunkSize());
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -194,12 +214,12 @@ public class ReactiveGridFsUpload<ID> implements GridFsObject<ID, Publisher<Data
|
||||
*/
|
||||
public ReactiveGridFsUploadBuilder<T> contentType(String contentType) {
|
||||
|
||||
upload.options = upload.options.contentType(contentType);
|
||||
this.options = this.options.contentType(contentType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReactiveGridFsUpload<T> build() {
|
||||
return (ReactiveGridFsUpload<T>) upload;
|
||||
return new ReactiveGridFsUpload(id, dataStream, filename, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,11 +36,11 @@ import org.bson.codecs.DocumentCodec;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.json.JsonParseException;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.CodecRegistryProvider;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.NumberUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -171,16 +171,12 @@ public class BsonUtils {
|
||||
return new BsonBoolean((Boolean) source);
|
||||
}
|
||||
|
||||
if(source instanceof Float) {
|
||||
if (source instanceof Float) {
|
||||
return new BsonDouble((Float) source);
|
||||
}
|
||||
|
||||
if (source instanceof Double) {
|
||||
return new BsonDouble((Double) source);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Unable to convert % (%s) to BsonValue.", source, source != null ? source.getClass() : "null"));
|
||||
throw new IllegalArgumentException(String.format("Unable to convert %s (%s) to BsonValue.", source,
|
||||
source != null ? source.getClass().getName() : "null"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -301,7 +301,7 @@ public class GridFsTemplateIntegrationTests {
|
||||
.contentType("xml") //
|
||||
.build();
|
||||
|
||||
assertThat(operations.save(upload)).isEqualTo(id);
|
||||
assertThat(operations.store(upload)).isEqualTo(id);
|
||||
|
||||
GridFsResource fsFile = operations.getResource(operations.findOne(query(where("_id").is(id))));
|
||||
byte[] content = StreamUtils.copyToByteArray(fsFile.getInputStream());
|
||||
|
||||
@@ -278,7 +278,7 @@ public class ReactiveGridFsTemplateTests {
|
||||
.contentType("xml") //
|
||||
.build();
|
||||
|
||||
operations.save(upload).as(StepVerifier::create).expectNext(id).verifyComplete();
|
||||
operations.store(upload).as(StepVerifier::create).expectNext(id).verifyComplete();
|
||||
|
||||
operations.findOne(query(where("_id").is(id))).flatMap(operations::getResource)
|
||||
.flatMapMany(ReactiveGridFsResource::getDownloadStream) //
|
||||
|
||||
Reference in New Issue
Block a user