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