From a51463cf7da735f3fb3971bcd3d3b155d4d39c62 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 18 Jul 2023 14:18:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/file/FileUploadProperties.java | 4 +- .../file/service/LocalFileStorageService.java | 55 ++++++++++--------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/FileUploadProperties.java b/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/FileUploadProperties.java index 88eb89afe..795705fc2 100644 --- a/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/FileUploadProperties.java +++ b/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/FileUploadProperties.java @@ -113,15 +113,17 @@ public class FileUploadProperties { info.location = staticLocation + "/" + filePath + "/" + fileName; info.savePath = absPath + "/" + fileName; - + info.relativeLocation = filePath + "/" + fileName; return info; } @Getter @Setter public static class StaticFileInfo { + private String savePath; + private String relativeLocation; private String location; } } diff --git a/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/service/LocalFileStorageService.java b/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/service/LocalFileStorageService.java index e30b34db5..233ce0ecb 100644 --- a/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/service/LocalFileStorageService.java +++ b/hsweb-system/hsweb-system-file/src/main/java/org/hswebframework/web/file/service/LocalFileStorageService.java @@ -24,12 +24,14 @@ public class LocalFileStorageService implements FileStorageService { @Override public Mono saveFile(FilePart filePart) { FileUploadProperties.StaticFileInfo info = properties.createStaticSavePath(filePart.filename()); - File file = new File(info.getSavePath()); - - return (filePart) - .transferTo(file) - .then(Mono.fromRunnable(()->properties.applyFilePermission(file))) - .thenReturn(info.getLocation()); + return createStaticFileInfo(filePart.filename()) + .flatMap(into -> { + File file = new File(info.getSavePath()); + return (filePart) + .transferTo(file) + .then(Mono.fromRunnable(() -> properties.applyFilePermission(file))) + .thenReturn(info.getLocation()); + }); } private static final OpenOption[] FILE_CHANNEL_OPTIONS = { @@ -37,30 +39,33 @@ public class LocalFileStorageService implements FileStorageService { StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE}; + protected Mono createStaticFileInfo(String fileName) { + return Mono.just(properties.createStaticSavePath(fileName)); + } + @Override @SneakyThrows public Mono saveFile(InputStream inputStream, String fileType) { String fileName = "_temp" + (fileType.startsWith(".") ? fileType : "." + fileType); - FileUploadProperties.StaticFileInfo info = properties.createStaticSavePath(fileName); - - return Mono - .fromCallable(() -> { - try (ReadableByteChannel input = Channels.newChannel(inputStream); - FileChannel output = FileChannel.open(Paths.get(info.getSavePath()), FILE_CHANNEL_OPTIONS)) { - long size = (input instanceof FileChannel ? ((FileChannel) input).size() : Long.MAX_VALUE); - long totalWritten = 0; - while (totalWritten < size) { - long written = output.transferFrom(input, totalWritten, size - totalWritten); - if (written <= 0) { - break; + return createStaticFileInfo(fileName) + .flatMap(info -> Mono + .fromCallable(() -> { + try (ReadableByteChannel input = Channels.newChannel(inputStream); + FileChannel output = FileChannel.open(Paths.get(info.getSavePath()), FILE_CHANNEL_OPTIONS)) { + long size = (input instanceof FileChannel ? ((FileChannel) input).size() : Long.MAX_VALUE); + long totalWritten = 0; + while (totalWritten < size) { + long written = output.transferFrom(input, totalWritten, size - totalWritten); + if (written <= 0) { + break; + } + totalWritten += written; + } + return info.getLocation(); } - totalWritten += written; - } - return info.getLocation(); - } - }) - .doOnSuccess((ignore)-> properties.applyFilePermission(new File(info.getSavePath()))) - .subscribeOn(Schedulers.boundedElastic()); + }) + .doOnSuccess((ignore) -> properties.applyFilePermission(new File(info.getSavePath()))) + .subscribeOn(Schedulers.boundedElastic())); } }