Browse Source

优化文件上传

zhouhao 3 years ago
parent
commit
b51c7cae95

+ 13 - 10
hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/web/ReactiveFileController.java

@@ -43,16 +43,19 @@ public class ReactiveFileController {
     @ResourceAction(id = "upload-static", name = "静态文件")
     @Operation(summary = "上传静态文件")
     public Mono<String> uploadStatic(@RequestPart("file")
-                                     @Parameter(name = "file", description = "文件", style = ParameterStyle.FORM) Part part) {
-        if (part instanceof FilePart) {
-            FilePart filePart = ((FilePart) part);
-            if (properties.denied(filePart.filename(), filePart.headers().getContentType())) {
-                throw new AccessDenyException();
-            }
-            return fileStorageService.saveFile(filePart);
-        } else {
-            return Mono.error(() -> new IllegalArgumentException("[file] part is not a file"));
-        }
+                                     @Parameter(name = "file", description = "文件", style = ParameterStyle.FORM) Mono<Part> partMono) {
+        return partMono
+                .flatMap(part -> {
+                    if (part instanceof FilePart) {
+                        FilePart filePart = ((FilePart) part);
+                        if (properties.denied(filePart.filename(), filePart.headers().getContentType())) {
+                           return Mono.error( new AccessDenyException());
+                        }
+                        return fileStorageService.saveFile(filePart);
+                    } else {
+                        return Mono.error(() -> new IllegalArgumentException("[file] part is not a file"));
+                    }
+                });
 
     }