keep filename in temporary file

This commit is contained in:
bgamard 2022-05-16 19:22:54 +02:00
parent dc0c20cd0c
commit 5e7f06070e
2 changed files with 10 additions and 5 deletions

View File

@ -69,13 +69,18 @@ public class FileService extends AbstractScheduledService {
return Scheduler.newFixedDelaySchedule(0, 5, TimeUnit.SECONDS); return Scheduler.newFixedDelaySchedule(0, 5, TimeUnit.SECONDS);
} }
public Path createTemporaryFile() throws IOException {
return createTemporaryFile(null);
}
/** /**
* Create a temporary file. * Create a temporary file.
* *
* @param name Wanted file name
* @return New temporary file * @return New temporary file
*/ */
public Path createTemporaryFile() throws IOException { public Path createTemporaryFile(String name) throws IOException {
Path path = Files.createTempFile("sismics_docs", null); Path path = Files.createTempFile("sismics_docs", name);
referenceSet.add(new TemporaryPathReference(path, referenceQueue)); referenceSet.add(new TemporaryPathReference(path, referenceQueue));
return path; return path;
} }

View File

@ -113,10 +113,12 @@ public class FileResource extends BaseResource {
} }
// Keep unencrypted data temporary on disk // 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; java.nio.file.Path unencryptedFile;
long fileSize; long fileSize;
try { try {
unencryptedFile = AppContext.getInstance().getFileService().createTemporaryFile(); unencryptedFile = AppContext.getInstance().getFileService().createTemporaryFile(name);
Files.copy(fileBodyPart.getValueAs(InputStream.class), unencryptedFile, StandardCopyOption.REPLACE_EXISTING); Files.copy(fileBodyPart.getValueAs(InputStream.class), unencryptedFile, StandardCopyOption.REPLACE_EXISTING);
fileSize = Files.size(unencryptedFile); fileSize = Files.size(unencryptedFile);
} catch (IOException e) { } catch (IOException e) {
@ -124,8 +126,6 @@ public class FileResource extends BaseResource {
} }
try { 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 ? String fileId = FileUtil.createFile(name, previousFileId, unencryptedFile, fileSize, documentDto == null ?
null : documentDto.getLanguage(), principal.getId(), documentId); null : documentDto.getLanguage(), principal.getId(), documentId);