|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.gridfs; |
| 17 | + |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import org.bson.Document; |
| 22 | +import org.bson.types.ObjectId; |
| 23 | +import org.reactivestreams.Publisher; |
| 24 | +import org.springframework.core.io.buffer.DataBuffer; |
| 25 | +import org.springframework.core.io.support.ResourcePatternResolver; |
| 26 | +import org.springframework.data.domain.Sort; |
| 27 | +import org.springframework.data.mongodb.core.query.Query; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | + |
| 30 | +import com.mongodb.client.gridfs.GridFSFindIterable; |
| 31 | +import com.mongodb.client.gridfs.model.GridFSFile; |
| 32 | +import com.mongodb.reactivestreams.client.gridfs.AsyncInputStream; |
| 33 | + |
| 34 | +/** |
| 35 | + * Collection of operations to store and read files from MongoDB GridFS using reactive infrastructure. |
| 36 | + * |
| 37 | + * @author Mark Paluch |
| 38 | + * @since 2.2 |
| 39 | + */ |
| 40 | +public interface ReactiveGridFsOperations { |
| 41 | + |
| 42 | + /** |
| 43 | + * Stores the given content into a file with the given name. |
| 44 | + * |
| 45 | + * @param content must not be {@literal null}. |
| 46 | + * @param filename must not be {@literal null} or empty. |
| 47 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 48 | + */ |
| 49 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, String filename) { |
| 50 | + return store(content, filename, (Object) null); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Stores the given content into a file with the given name. |
| 55 | + * |
| 56 | + * @param content must not be {@literal null}. |
| 57 | + * @param metadata can be {@literal null}. |
| 58 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 59 | + */ |
| 60 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable Object metadata) { |
| 61 | + return store(content, null, metadata); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Stores the given content into a file with the given name. |
| 66 | + * |
| 67 | + * @param content must not be {@literal null}. |
| 68 | + * @param metadata can be {@literal null}. |
| 69 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 70 | + */ |
| 71 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable Document metadata) { |
| 72 | + return store(content, null, metadata); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Stores the given content into a file with the given name and content type. |
| 77 | + * |
| 78 | + * @param content must not be {@literal null}. |
| 79 | + * @param filename must not be {@literal null} or empty. |
| 80 | + * @param contentType can be {@literal null}. |
| 81 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 82 | + */ |
| 83 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable String filename, @Nullable String contentType) { |
| 84 | + return store(content, filename, contentType, (Object) null); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Stores the given content into a file with the given name using the given metadata. The metadata object will be |
| 89 | + * marshalled before writing. |
| 90 | + * |
| 91 | + * @param content must not be {@literal null}. |
| 92 | + * @param filename can be {@literal null} or empty. |
| 93 | + * @param metadata can be {@literal null}. |
| 94 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 95 | + */ |
| 96 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable String filename, @Nullable Object metadata) { |
| 97 | + return store(content, filename, null, metadata); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Stores the given content into a file with the given name and content type using the given metadata. The metadata |
| 102 | + * object will be marshalled before writing. |
| 103 | + * |
| 104 | + * @param content must not be {@literal null}. |
| 105 | + * @param filename must not be {@literal null} or empty. |
| 106 | + * @param contentType can be {@literal null}. |
| 107 | + * @param metadata can be {@literal null} |
| 108 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 109 | + */ |
| 110 | + Mono<ObjectId> store(AsyncInputStream content, @Nullable String filename, @Nullable String contentType, |
| 111 | + @Nullable Object metadata); |
| 112 | + |
| 113 | + /** |
| 114 | + * Stores the given content into a file with the given name and content type using the given metadata. The metadata |
| 115 | + * object will be marshalled before writing. |
| 116 | + * |
| 117 | + * @param content must not be {@literal null}. |
| 118 | + * @param filename must not be {@literal null} or empty. |
| 119 | + * @param contentType can be {@literal null}. |
| 120 | + * @param metadata can be {@literal null} |
| 121 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 122 | + */ |
| 123 | + Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable String filename, @Nullable String contentType, |
| 124 | + @Nullable Object metadata); |
| 125 | + |
| 126 | + /** |
| 127 | + * Stores the given content into a file with the given name using the given metadata. |
| 128 | + * |
| 129 | + * @param content must not be {@literal null}. |
| 130 | + * @param filename must not be {@literal null} or empty. |
| 131 | + * @param metadata can be {@literal null}. |
| 132 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 133 | + */ |
| 134 | + default Mono<ObjectId> store(Publisher<DataBuffer> content, @Nullable String filename, @Nullable Document metadata) { |
| 135 | + return store(content, filename, null, metadata); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Stores the given content into a file with the given name and content type using the given metadata. |
| 140 | + * |
| 141 | + * @param content must not be {@literal null}. |
| 142 | + * @param filename must not be {@literal null} or empty. |
| 143 | + * @param contentType can be {@literal null}. |
| 144 | + * @param metadata can be {@literal null}. |
| 145 | + * @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created. |
| 146 | + */ |
| 147 | + Mono<ObjectId> store(AsyncInputStream content, @Nullable String filename, @Nullable String contentType, |
| 148 | + @Nullable Document metadata); |
| 149 | + |
| 150 | + Mono<ObjectId> store(Publisher<DataBuffer> content, String filename, String contentType, Document metadata); |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the |
| 154 | + * {@link Query} will not be regarded as MongoDB does not support ordering for GridFS file access. |
| 155 | + * |
| 156 | + * @see <a href="https://jira.mongodb.org/browse/JAVA-431">MongoDB Jira: JAVA-431</a> |
| 157 | + * @param query must not be {@literal null}. |
| 158 | + * @return {@link GridFSFindIterable} to obtain results from. Eg. by calling |
| 159 | + * {@link GridFSFindIterable#into(java.util.Collection)}. |
| 160 | + */ |
| 161 | + Flux<GridFSFile> find(Query query); |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns a single {@link com.mongodb.client.gridfs.model.GridFSFile} matching the given query or {@literal null} in |
| 165 | + * case no file matches. |
| 166 | + * |
| 167 | + * @param query must not be {@literal null}. |
| 168 | + * @return |
| 169 | + */ |
| 170 | + Mono<GridFSFile> findOne(Query query); |
| 171 | + |
| 172 | + /** |
| 173 | + * Deletes all files matching the given {@link Query}. |
| 174 | + * |
| 175 | + * @param query must not be {@literal null}. |
| 176 | + */ |
| 177 | + Mono<Void> delete(Query query); |
| 178 | + |
| 179 | + /** |
| 180 | + * Returns the {@link GridFsResource} with the given file name. |
| 181 | + * |
| 182 | + * @param filename must not be {@literal null}. |
| 183 | + * @return the resource. Use {@link org.springframework.core.io.Resource#exists()} to check if the returned |
| 184 | + * {@link GridFsResource} is actually present. |
| 185 | + * @see ResourcePatternResolver#getResource(String) |
| 186 | + */ |
| 187 | + Mono<ReactiveGridFsResource> getResource(String filename); |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns the {@link GridFsResource} for a {@link GridFSFile}. |
| 191 | + * |
| 192 | + * @param file must not be {@literal null}. |
| 193 | + * @return the resource for the file. |
| 194 | + */ |
| 195 | + Mono<ReactiveGridFsResource> getResource(GridFSFile file); |
| 196 | + |
| 197 | + /** |
| 198 | + * Returns all {@link GridFsResource}s matching the given file name pattern. |
| 199 | + * |
| 200 | + * @param filenamePattern must not be {@literal null}. |
| 201 | + * @return |
| 202 | + */ |
| 203 | + Flux<ReactiveGridFsResource> getResources(String filenamePattern); |
| 204 | +} |
0 commit comments