From 5e7f06070ef9cc022118263c602bab6cc03f05b4 Mon Sep 17 00:00:00 2001 From: bgamard Date: Mon, 16 May 2022 19:22:54 +0200 Subject: [PATCH] keep filename in temporary file --- .../java/com/sismics/docs/core/service/FileService.java | 9 +++++++-- .../com/sismics/docs/rest/resource/FileResource.java | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/service/FileService.java b/docs-core/src/main/java/com/sismics/docs/core/service/FileService.java index a69290b7..ad295ef7 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/service/FileService.java +++ b/docs-core/src/main/java/com/sismics/docs/core/service/FileService.java @@ -69,13 +69,18 @@ public class FileService extends AbstractScheduledService { return Scheduler.newFixedDelaySchedule(0, 5, TimeUnit.SECONDS); } + public Path createTemporaryFile() throws IOException { + return createTemporaryFile(null); + } + /** * Create a temporary file. * + * @param name Wanted file name * @return New temporary file */ - public Path createTemporaryFile() throws IOException { - Path path = Files.createTempFile("sismics_docs", null); + public Path createTemporaryFile(String name) throws IOException { + Path path = Files.createTempFile("sismics_docs", name); referenceSet.add(new TemporaryPathReference(path, referenceQueue)); return path; } diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java index dea0ef39..28636ac7 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java @@ -113,10 +113,12 @@ public class FileResource extends BaseResource { } // Keep unencrypted data temporary on disk + String name = fileBodyPart.getContentDisposition() != null ? + URLDecoder.decode(fileBodyPart.getContentDisposition().getFileName(), StandardCharsets.UTF_8) : null; java.nio.file.Path unencryptedFile; long fileSize; try { - unencryptedFile = AppContext.getInstance().getFileService().createTemporaryFile(); + unencryptedFile = AppContext.getInstance().getFileService().createTemporaryFile(name); Files.copy(fileBodyPart.getValueAs(InputStream.class), unencryptedFile, StandardCopyOption.REPLACE_EXISTING); fileSize = Files.size(unencryptedFile); } catch (IOException e) { @@ -124,8 +126,6 @@ public class FileResource extends BaseResource { } try { - String name = fileBodyPart.getContentDisposition() != null ? - URLDecoder.decode(fileBodyPart.getContentDisposition().getFileName(), StandardCharsets.UTF_8) : null; String fileId = FileUtil.createFile(name, previousFileId, unencryptedFile, fileSize, documentDto == null ? null : documentDto.getLanguage(), principal.getId(), documentId);