From 230df95da49720e4d3ad2bde9da9d4b0eca05c62 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Tue, 13 Dec 2022 10:40:18 +0100 Subject: [PATCH 1/2] #3630 - Export of single document fails when username is too short - Pad temp file names to three char prefix if necessary --- .../ukp/clarin/webanno/api/format/FormatSupport.java | 7 +++++-- .../project/export/ProjectExportServiceImpl.java | 6 +++++- .../webapp/remoteapi/LegacyRemoteApiController.java | 8 +++++--- .../webapp/remoteapi/aero/AeroRemoteApiController.java | 2 +- .../inception/support/io/FileUploadDownloadHelper.java | 3 +-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java b/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java index 5c8965748e1..867fe469f2e 100644 --- a/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java +++ b/inception/inception-api-formats/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/format/FormatSupport.java @@ -34,6 +34,7 @@ import java.util.Optional; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.uima.analysis_engine.AnalysisEngine; import org.apache.uima.analysis_engine.AnalysisEngineDescription; import org.apache.uima.analysis_engine.AnalysisEngineProcessException; @@ -196,8 +197,10 @@ default File write(SourceDocument aDocument, CAS aCas, File aTargetFolder, } // If the writer produced only a single file, then that is the result - File exportFile = createTempFile( - FilenameUtils.getBaseName(aTargetFolder.listFiles()[0].getName()), + String filename = FilenameUtils.getBaseName(aTargetFolder.listFiles()[0].getName()); + // temp-file prefix must be at least 3 chars + filename = StringUtils.rightPad(filename, 3, "_"); + File exportFile = createTempFile(filename, "." + FilenameUtils.getExtension(aTargetFolder.listFiles()[0].getName())); // File exportFile = new File(aTargetFolder.getParent(), // aTargetFolder.listFiles()[0].getName()); diff --git a/inception/inception-project-export/src/main/java/de/tudarmstadt/ukp/inception/project/export/ProjectExportServiceImpl.java b/inception/inception-project-export/src/main/java/de/tudarmstadt/ukp/inception/project/export/ProjectExportServiceImpl.java index ed4284ebca8..8080c25f41a 100644 --- a/inception/inception-project-export/src/main/java/de/tudarmstadt/ukp/inception/project/export/ProjectExportServiceImpl.java +++ b/inception/inception-project-export/src/main/java/de/tudarmstadt/ukp/inception/project/export/ProjectExportServiceImpl.java @@ -52,6 +52,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; @@ -171,7 +172,10 @@ public void onContextRefreshedEvent(ContextRefreshedEvent aEvent) public File exportProject(FullProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor) throws ProjectExportException, IOException, InterruptedException { - File projectZipFile = File.createTempFile(aRequest.getProject().getSlug(), ".zip"); + String filename = aRequest.getProject().getSlug(); + // temp-file prefix must be at least 3 chars + filename = StringUtils.rightPad(filename, 3, "_"); + File projectZipFile = File.createTempFile(filename, ".zip"); boolean success = false; try { exportProject(aRequest, aMonitor, projectZipFile); diff --git a/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/LegacyRemoteApiController.java b/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/LegacyRemoteApiController.java index d064e0ff7b3..86da546e5ee 100644 --- a/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/LegacyRemoteApiController.java +++ b/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/LegacyRemoteApiController.java @@ -182,7 +182,10 @@ public ResponseEntity projectCreate( // // If the current filename does not start with "." and is in the root folder of the ZIP, // import it as a source document - File zipFile = File.createTempFile(aFile.getOriginalFilename(), ".zip"); + String filename = aFile.getOriginalFilename(); + // temp-file prefix must be at least 3 chars + filename = StringUtils.rightPad(filename, 3, "_"); + File zipFile = File.createTempFile(filename, ".zip"); aFile.transferTo(zipFile); ZipFile zip = new ZipFile(zipFile); @@ -191,8 +194,7 @@ public ResponseEntity projectCreate( // ZipEntry entry = (ZipEntry) zipEnumerate.nextElement(); // If it is the zip name, ignore it - if ((FilenameUtils.removeExtension(aFile.getOriginalFilename()) + "/") - .equals(entry.toString())) { + if ((FilenameUtils.removeExtension(filename) + "/").equals(entry.toString())) { continue; } // IF the current filename is META-INF/webanno/source-meta-data.properties store it as diff --git a/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/aero/AeroRemoteApiController.java b/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/aero/AeroRemoteApiController.java index 3a68debc348..737d2d02529 100644 --- a/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/aero/AeroRemoteApiController.java +++ b/inception/inception-remote/src/main/java/de/tudarmstadt/ukp/clarin/webanno/webapp/remoteapi/aero/AeroRemoteApiController.java @@ -400,7 +400,7 @@ public ResponseEntity> projectImport( userRepository.isAdministrator(user)); Project importedProject; - File tempFile = File.createTempFile("webanno-training", null); + File tempFile = File.createTempFile("inception-project-import", null); try (InputStream is = new BufferedInputStream(aFile.getInputStream()); OutputStream os = new FileOutputStream(tempFile);) { if (!ZipUtils.isZipStream(is)) { diff --git a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/io/FileUploadDownloadHelper.java b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/io/FileUploadDownloadHelper.java index ce17e48d87a..ab1ea3ac974 100644 --- a/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/io/FileUploadDownloadHelper.java +++ b/inception/inception-support/src/main/java/de/tudarmstadt/ukp/inception/support/io/FileUploadDownloadHelper.java @@ -36,13 +36,12 @@ public class FileUploadDownloadHelper { + private static final String INCEPTION_TMP_FILE_PREFIX = "inception_file"; private final Logger log = LoggerFactory.getLogger(getClass()); private final IFileCleaner fileTracker; - private final String INCEPTION_TMP_FILE_PREFIX = "inception_file"; - public FileUploadDownloadHelper(Application application) { fileTracker = application.getResourceSettings().getFileCleaner(); From d276802705d1d9f4b533f6b134933446214f66eb Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Tue, 13 Dec 2022 11:20:12 +0100 Subject: [PATCH 2/2] #3630 - Export of single document fails when username is too short - Fix dependencies --- inception/inception-api-formats/pom.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inception/inception-api-formats/pom.xml b/inception/inception-api-formats/pom.xml index eeb56ed4cb9..bd55e40a78b 100644 --- a/inception/inception-api-formats/pom.xml +++ b/inception/inception-api-formats/pom.xml @@ -57,5 +57,9 @@ commons-io commons-io + + org.apache.commons + commons-lang3 + - \ No newline at end of file +