1 /*
2 * Copyright 2020 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
17 package org.bremersee.web.reactive.multipart;
18
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import javax.validation.constraints.NotNull;
23 import org.bremersee.web.multipart.FileAwareMultipartFile;
24 import org.springframework.http.codec.multipart.Part;
25 import org.springframework.util.MultiValueMap;
26 import org.springframework.validation.annotation.Validated;
27 import org.springframework.web.multipart.MultipartFile;
28 import reactor.core.publisher.Flux;
29 import reactor.core.publisher.Mono;
30
31 /**
32 * The multipart file builder interface.
33 *
34 * @author Christian Bremer
35 */
36 @Validated
37 public interface MultipartFileBuilder {
38
39 /**
40 * Build multipart file from the given part.
41 *
42 * <p>If hhe given part is {@code null}, an empty multipart file will be returned.
43 *
44 * @param part the part (can be {@code null})
45 * @return the multipart file
46 */
47 Mono<MultipartFile> build(Part part);
48
49 /**
50 * Build multipart file from the last part of the given parts.
51 *
52 * @param parts the parts
53 * @return the multipart file
54 */
55 Mono<MultipartFile> build(@NotNull Flux<? extends Part> parts);
56
57 /**
58 * Build multipart files from the given parts.
59 *
60 * @param parts the parts
61 * @return the multipart files
62 */
63 Mono<List<MultipartFile>> buildList(@NotNull Flux<? extends Part> parts);
64
65 /**
66 * Build list of multipart files from the given multi part data.
67 *
68 * @param multiPartData the multi part data
69 * @param requestParameters the request parameters
70 * @return the list of multipart files
71 */
72 Mono<List<MultipartFile>> buildList(
73 @NotNull MultiValueMap<String, Part> multiPartData,
74 String... requestParameters);
75
76 /**
77 * Build list (flux) of lists of multipart files from the given multi part data.
78 *
79 * @param multiPartData the multi part data
80 * @param requestParameters the request parameters
81 * @return the list (flux) of lists of multipart files
82 */
83 Flux<List<MultipartFile>> buildLists(
84 @NotNull MultiValueMap<String, Part> multiPartData,
85 String... requestParameters);
86
87 /**
88 * Build map mono.
89 *
90 * @param parts the parts
91 * @return the mono
92 */
93 @SuppressWarnings("unchecked")
94 Mono<Map<String, MultipartFile>> buildMap(@NotNull Flux<? extends Part>... parts);
95
96 /**
97 * Build map of multipart files from the given multi part data.
98 *
99 * @param multiPartData the multi part data
100 * @param requestParameters the request parameters
101 * @return the map of multipart files
102 */
103 Mono<Map<String, MultipartFile>> buildMap(
104 @NotNull MultiValueMap<String, Part> multiPartData,
105 String... requestParameters);
106
107 /**
108 * Build multi value map mono.
109 *
110 * @param parts the parts
111 * @return the mono
112 */
113 @SuppressWarnings("unchecked")
114 Mono<MultiValueMap<String, MultipartFile>> buildMultiValueMap(
115 @NotNull Flux<? extends Part>... parts);
116
117 /**
118 * Build multi value map of multipart files from the given multi part data.
119 *
120 * @param multiPartData the multi part data
121 * @param requestParameters the request parameters
122 * @return the multi value map of multipart files
123 */
124 Mono<MultiValueMap<String, MultipartFile>> buildMultiValueMap(
125 @NotNull MultiValueMap<String, Part> multiPartData,
126 String... requestParameters);
127
128 /**
129 * Get multipart file with the specified index. If the list is smaller than the index, an empty multipart file will be
130 * returned.
131 *
132 * @param list the list
133 * @param index the index
134 * @return the multipart file
135 */
136 @NotNull
137 static MultipartFile getMultipartFile(List<MultipartFile> list, int index) {
138 MultipartFile multipartFile = list != null && index >= 0 && list.size() > index
139 ? list.get(index)
140 : FileAwareMultipartFile.empty();
141 return multipartFile != null ? multipartFile : FileAwareMultipartFile.empty();
142 }
143
144 /**
145 * Get multipart file with the specified request parameter name. If no such multipart file exists, an empty one will
146 * be returned.
147 *
148 * @param map the map
149 * @param name the request parameter name
150 * @return the multipart file
151 */
152 @NotNull
153 static MultipartFile getMultipartFile(Map<String, MultipartFile> map, String name) {
154 MultipartFile multipartFile = map != null && name != null
155 ? map.getOrDefault(name, FileAwareMultipartFile.empty())
156 : FileAwareMultipartFile.empty();
157 return multipartFile != null ? multipartFile : FileAwareMultipartFile.empty();
158 }
159
160 /**
161 * Get multipart files with the specified request parameter. If no such list of multipart files exists, an empty list
162 * will be returned.
163 *
164 * @param map the map
165 * @param name the name
166 * @return the multipart files
167 */
168 @NotNull
169 static List<MultipartFile> getMultipartFiles(
170 MultiValueMap<String, MultipartFile> map,
171 String name) {
172
173 List<MultipartFile> list = map != null && name != null
174 ? map.getOrDefault(name, Collections.emptyList())
175 : Collections.emptyList();
176 return list != null ? list : Collections.emptyList();
177 }
178
179 /**
180 * Get first multipart file. If no such multipart file exists, an empty one will be returned.
181 *
182 * @param map the map
183 * @param name the name
184 * @return the first multipart file
185 */
186 @NotNull
187 static MultipartFile getFirstMultipartFile(
188 MultiValueMap<String, MultipartFile> map,
189 String name) {
190
191 MultipartFile multipartFile = map != null && name != null
192 ? map.getFirst(name)
193 : FileAwareMultipartFile.empty();
194 return multipartFile != null ? multipartFile : FileAwareMultipartFile.empty();
195 }
196
197 }