From ea537fc362b6d687138fcebc077f7c530267c46c Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 12 Sep 2024 14:40:56 +0200 Subject: [PATCH 1/9] create preview content for tar files --- .../MetadataBitstreamRestRepository.java | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index aebb260cd438..d0ea344f9cfc 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -32,8 +32,8 @@ import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.compress.archivers.ArchiveException; -import org.apache.commons.compress.archivers.ArchiveInputStream; -import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Logger; import org.dspace.app.rest.Parameter; @@ -320,9 +320,7 @@ private List processInputStreamToFilePreview(Context context, Bitstrea data = extractFile(inputStream, "zip"); fileInfos = FileTreeViewGenerator.parse(data); } else if (bitstream.getFormat(context).getMIMEType().equals("application/x-tar")) { - ArchiveInputStream is = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, - inputStream); - data = extractFile(is, "tar"); + data = extractFile(inputStream, "tar"); fileInfos = FileTreeViewGenerator.parse(data); } } @@ -390,21 +388,35 @@ public String extractFile(InputStream inputStream, String fileType) { Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); - zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); - Path root = zipFileSystem.getPath("/"); - Files.walk(root) - .forEach(path -> { - try { - long fileSize = Files.size(path); - if (Files.isDirectory(path)) { - filePaths.add(path.toString().substring(1) + "/|" + fileSize ); - } else { - filePaths.add(path.toString().substring(1) + "|" + fileSize ); - } - } catch (IOException e) { - e.printStackTrace(); + if ("tar".equals(fileType)) { + try (InputStream fi = Files.newInputStream(tempFile); + TarArchiveInputStream tis = new TarArchiveInputStream(fi)) { + TarArchiveEntry entry; + while ((entry = tis.getNextTarEntry()) != null) { + if (entry.isDirectory()) { + filePaths.add(entry.getName() + "/|" + entry.getSize()); + } else { + filePaths.add(entry.getName() + "|" + entry.getSize()); } - }); + } + } + } else { + zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); + Path root = zipFileSystem.getPath("/"); + Files.walk(root) + .forEach(path -> { + try { + long fileSize = Files.size(path); + if (Files.isDirectory(path)) { + filePaths.add(path.toString().substring(1) + "/|" + fileSize); + } else { + filePaths.add(path.toString().substring(1) + "|" + fileSize); + } + } catch (IOException e) { + e.printStackTrace(); + } + }); + } } catch (IOException e) { e.printStackTrace(); } finally { From 40ca7d6707f2b59e7d382b7501adf7275874953e Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 19 Sep 2024 10:20:18 +0200 Subject: [PATCH 2/9] Added right logs --- .../rest/repository/MetadataBitstreamRestRepository.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index d0ea344f9cfc..2b78ccd78779 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -413,18 +413,19 @@ public String extractFile(InputStream inputStream, String fileType) { filePaths.add(path.toString().substring(1) + "|" + fileSize); } } catch (IOException e) { - e.printStackTrace(); + log.error("Cannot get file size for path: {} because of: {}", path.toString(), + e.getMessage()); } }); } } catch (IOException e) { - e.printStackTrace(); + log.error("Cannot extract file because of: {}", e.getMessage()); } finally { if (zipFileSystem != null) { try { zipFileSystem.close(); } catch (IOException e) { - e.printStackTrace(); + log.error("Cannot close ZIP file system because of: {}", e.getMessage()); } } @@ -432,7 +433,7 @@ public String extractFile(InputStream inputStream, String fileType) { try { Files.delete(tempFile); } catch (IOException e) { - e.printStackTrace(); + log.error("Cannot delete temporary file because of: {}", e.getMessage()); } } } From 73df861fe6ff60ce2e24b8940b54be8aff8cb051 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 10:40:31 +0200 Subject: [PATCH 3/9] devided extractFile funs into several separated smaller funs --- .../MetadataBitstreamRestRepository.java | 188 +++++++++++------- 1 file changed, 118 insertions(+), 70 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index d0ea344f9cfc..437f133d9b88 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -16,6 +16,7 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.sql.SQLException; import java.util.ArrayList; @@ -364,95 +365,105 @@ private String composePreviewURL(Context context, Item item, Bitstream bitstream return url; } - /** - * Convert ZIP file into structured String. - * @param inputStream Input stream with ZIP content - * @param fileType ZIP/TAR - * @return structured String + * Creates a temporary file with the appropriate extension based on the specified file type. + * @param fileType the type of file for which to create a temporary file + * @return a Path object representing the temporary file + * @throws IOException if an I/O error occurs while creating the file */ - public String extractFile(InputStream inputStream, String fileType) { - List filePaths = new ArrayList<>(); - Path tempFile = null; - FileSystem zipFileSystem = null; + private Path createTempFile(String fileType) throws IOException { + String extension = "tar".equals(fileType) ? ".tar" : ".zip"; + return Files.createTempFile("temp", extension); + } - try { - switch (fileType) { - case "tar": - tempFile = Files.createTempFile("temp", ".tar"); - break; - default: - tempFile = Files.createTempFile("temp", ".zip"); + private void addFilePath(List filePaths, String path, long size) { + String fileInfo = (Files.isDirectory(Paths.get(path))) ? path + "/|" + size : path + "|" + size; + filePaths.add(fileInfo); + } + /** + * Processes a TAR file, extracting its entries and adding their paths to the provided list. + * @param filePaths the list to populate with the extracted file paths + * @param tempFile the temporary TAR file to process + * @throws IOException if an I/O error occurs while reading the TAR file + */ + private void processTarFile(List filePaths, Path tempFile) throws IOException { + try (InputStream fi = Files.newInputStream(tempFile); + TarArchiveInputStream tis = new TarArchiveInputStream(fi)) { + TarArchiveEntry entry; + while ((entry = tis.getNextTarEntry()) != null) { + addFilePath(filePaths, entry.getName(), entry.getSize()); } + } + } - Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); - - if ("tar".equals(fileType)) { - try (InputStream fi = Files.newInputStream(tempFile); - TarArchiveInputStream tis = new TarArchiveInputStream(fi)) { - TarArchiveEntry entry; - while ((entry = tis.getNextTarEntry()) != null) { - if (entry.isDirectory()) { - filePaths.add(entry.getName() + "/|" + entry.getSize()); - } else { - filePaths.add(entry.getName() + "|" + entry.getSize()); - } - } - } - } else { - zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); - Path root = zipFileSystem.getPath("/"); - Files.walk(root) - .forEach(path -> { - try { - long fileSize = Files.size(path); - if (Files.isDirectory(path)) { - filePaths.add(path.toString().substring(1) + "/|" + fileSize); - } else { - filePaths.add(path.toString().substring(1) + "|" + fileSize); - } - } catch (IOException e) { - e.printStackTrace(); - } - }); + /** + * Processes a ZIP file, extracting its entries and adding their paths to the provided list. + * @param filePaths the list to populate with the extracted file paths + * @param zipFileSystem the FileSystem object representing the ZIP file + * @throws IOException if an I/O error occurs while reading the ZIP file + */ + private void processZipFile(List filePaths, FileSystem zipFileSystem) throws IOException { + Path root = zipFileSystem.getPath("/"); + Files.walk(root).forEach(path -> { + try { + long fileSize = Files.size(path); + addFilePath(filePaths, path.toString().substring(1), fileSize); + } catch (IOException e) { + e.printStackTrace(); } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (zipFileSystem != null) { - try { - zipFileSystem.close(); - } catch (IOException e) { - e.printStackTrace(); - } + }); + } + + /** + * Closes the specified FileSystem resource if it is not null. + * @param zipFileSystem the FileSystem to close + */ + private void closeFileSystem(FileSystem zipFileSystem) { + if (!Objects.isNull(zipFileSystem)) { + + try { + zipFileSystem.close(); + } catch (IOException e) { + e.printStackTrace(); } + } + } - if (tempFile != null) { - try { - Files.delete(tempFile); - } catch (IOException e) { - e.printStackTrace(); - } + /** + * Deletes the specified temporary file if it is not null. + * @param tempFile the Path object representing the temporary file to delete + */ + private void deleteTempFile(Path tempFile) { + if (!Objects.isNull(tempFile)) { + try { + Files.delete(tempFile); + } catch (IOException e) { + e.printStackTrace(); } } + } + + /** + * Builds an XML response string based on the provided list of file paths. + * @param filePaths the list of file paths to include in the XML response + * @return an XML string representation of the file paths + */ + private String buildXmlResponse(List filePaths) { + StringBuilder sb = new StringBuilder(); + sb.append(""); - // Is a folder regex String folderRegex = "/|\\d+"; Pattern pattern = Pattern.compile(folderRegex); - - StringBuilder sb = new StringBuilder(); - sb.append(("")); Iterator iterator = filePaths.iterator(); int fileCounter = 0; - while ((iterator.hasNext() && fileCounter < maxPreviewCount)) { - String filePath = iterator.next(); + while (iterator.hasNext() && fileCounter < maxPreviewCount) { + String filePath = iterator.next(); // Check if the file is a folder Matcher matcher = pattern.matcher(filePath); if (!matcher.matches()) { - // It is a file - fileCounter++; + fileCounter++; // Count as a file } sb.append("").append(filePath).append(""); } @@ -460,10 +471,47 @@ public String extractFile(InputStream inputStream, String fileType) { if (fileCounter > maxPreviewCount) { sb.append("...too many files...|0"); } - sb.append(("")); + sb.append(""); return sb.toString(); } + /** + * Extracts files from an InputStream, processes them based on the specified file type (tar or zip), + * and returns an XML representation of the file paths. + * + * @param inputStream the InputStream containing the file data + * @param fileType the type of file to extract ("tar" or "zip") + * @return an XML string representing the extracted file paths + */ + public String extractFile(InputStream inputStream, String fileType) { + List filePaths = new ArrayList<>(); + Path tempFile = null; + FileSystem zipFileSystem = null; + + try { + // Create a temporary file based on the file type + tempFile = createTempFile(fileType); + + // Copy the input stream to the temporary file + Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); + + // Process the file based on its type + if ("tar".equals(fileType)) { + processTarFile(filePaths, tempFile); + } else { + zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); + processZipFile(filePaths, zipFileSystem); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + closeFileSystem(zipFileSystem); + deleteTempFile(tempFile); + } + + return buildXmlResponse(filePaths); + } + /** * Read input stream and return content as String * @param inputStream to read From 782f553224d4326101925ac95ec989dcb11ecc6b Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 11:16:48 +0200 Subject: [PATCH 4/9] added comment and removed empty line --- .../rest/repository/MetadataBitstreamRestRepository.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 437f133d9b88..d8ee4a20d75d 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -376,6 +376,14 @@ private Path createTempFile(String fileType) throws IOException { return Files.createTempFile("temp", extension); } + /** + * Adds a file path and its size to the list of file paths. + * If the path represents a directory, appends a "/" to the path. + * + * @param filePaths the list of file paths to add to + * @param path the file or directory path + * @param size the size of the file or directory + */ private void addFilePath(List filePaths, String path, long size) { String fileInfo = (Files.isDirectory(Paths.get(path))) ? path + "/|" + size : path + "|" + size; filePaths.add(fileInfo); @@ -421,7 +429,6 @@ private void processZipFile(List filePaths, FileSystem zipFileSystem) th */ private void closeFileSystem(FileSystem zipFileSystem) { if (!Objects.isNull(zipFileSystem)) { - try { zipFileSystem.close(); } catch (IOException e) { From 653f85e49c19236554c0e01edcf35cd9a6398d96 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 11:22:03 +0200 Subject: [PATCH 5/9] added empty lines and removed unwanted comments --- .../MetadataBitstreamRestRepository.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index d8ee4a20d75d..0421990c53a8 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -379,7 +379,6 @@ private Path createTempFile(String fileType) throws IOException { /** * Adds a file path and its size to the list of file paths. * If the path represents a directory, appends a "/" to the path. - * * @param filePaths the list of file paths to add to * @param path the file or directory path * @param size the size of the file or directory @@ -457,20 +456,22 @@ private void deleteTempFile(Path tempFile) { * @return an XML string representation of the file paths */ private String buildXmlResponse(List filePaths) { - StringBuilder sb = new StringBuilder(); - sb.append(""); - + // Is a folder regex String folderRegex = "/|\\d+"; Pattern pattern = Pattern.compile(folderRegex); + + StringBuilder sb = new StringBuilder(); + sb.append(""); Iterator iterator = filePaths.iterator(); int fileCounter = 0; - while (iterator.hasNext() && fileCounter < maxPreviewCount) { String filePath = iterator.next(); + // Check if the file is a folder Matcher matcher = pattern.matcher(filePath); if (!matcher.matches()) { - fileCounter++; // Count as a file + // It is a file + fileCounter++; } sb.append("").append(filePath).append(""); } @@ -485,7 +486,6 @@ private String buildXmlResponse(List filePaths) { /** * Extracts files from an InputStream, processes them based on the specified file type (tar or zip), * and returns an XML representation of the file paths. - * * @param inputStream the InputStream containing the file data * @param fileType the type of file to extract ("tar" or "zip") * @return an XML string representing the extracted file paths From 6297825c0e81b827cfa0ce4fb751f799f212eec7 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 11:23:30 +0200 Subject: [PATCH 6/9] removed empty line --- .../app/rest/repository/MetadataBitstreamRestRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 0421990c53a8..a3cfe9b8b41d 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -466,7 +466,6 @@ private String buildXmlResponse(List filePaths) { int fileCounter = 0; while (iterator.hasNext() && fileCounter < maxPreviewCount) { String filePath = iterator.next(); - // Check if the file is a folder Matcher matcher = pattern.matcher(filePath); if (!matcher.matches()) { From 2a6810c0469c4eeed6296d2ec90542bfdaf7ff23 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 16:28:43 +0200 Subject: [PATCH 7/9] used consts --- .../repository/MetadataBitstreamRestRepository.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index a3cfe9b8b41d..81634b441841 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -82,7 +82,10 @@ @Component(MetadataBitstreamWrapperRest.CATEGORY + "." + MetadataBitstreamWrapperRest.NAME) public class MetadataBitstreamRestRepository extends DSpaceRestRepository { private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataBitstreamRestRepository.class); - + private final String FILE_EXTENSION_ZIP = ".zip"; + private final String FILE_EXTENSION_TAR = ".tar"; + private final String ARCHIVE_TYPE_ZIP = "zip"; + private final String ARCHIVE_TYPE_TAR = "tar"; @Autowired HandleService handleService; @@ -318,10 +321,10 @@ private List processInputStreamToFilePreview(Context context, Bitstrea } else { String data = ""; if (bitstream.getFormat(context).getMIMEType().equals("application/zip")) { - data = extractFile(inputStream, "zip"); + data = extractFile(inputStream, ARCHIVE_TYPE_ZIP); fileInfos = FileTreeViewGenerator.parse(data); } else if (bitstream.getFormat(context).getMIMEType().equals("application/x-tar")) { - data = extractFile(inputStream, "tar"); + data = extractFile(inputStream, ARCHIVE_TYPE_TAR); fileInfos = FileTreeViewGenerator.parse(data); } } @@ -372,7 +375,7 @@ private String composePreviewURL(Context context, Item item, Bitstream bitstream * @throws IOException if an I/O error occurs while creating the file */ private Path createTempFile(String fileType) throws IOException { - String extension = "tar".equals(fileType) ? ".tar" : ".zip"; + String extension = FILE_EXTENSION_TAR.equals(fileType) ? FILE_EXTENSION_TAR : FILE_EXTENSION_ZIP; return Files.createTempFile("temp", extension); } From 1d16699ff19c1ce69a1d947828566f16c344818f Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 3 Oct 2024 19:32:04 +0200 Subject: [PATCH 8/9] try incorrect identification level --- .../app/rest/repository/MetadataBitstreamRestRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index a342b03d87e4..266981a77c3a 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -335,14 +335,14 @@ private List processInputStreamToFilePreview(Context context, Bitstrea if (bitstream.getFormat(context).getMIMEType().equals("application/zip")) { data = extractFile(inputStream, ARCHIVE_TYPE_ZIP); try { - fileInfos = FileTreeViewGenerator.parse(data); + fileInfos = FileTreeViewGenerator.parse(data); } catch (Exception e) { log.error("Cannot extract file content because: {}", e.getMessage()); } } else if (bitstream.getFormat(context).getMIMEType().equals("application/x-tar")) { data = extractFile(inputStream, ARCHIVE_TYPE_TAR); try { - fileInfos = FileTreeViewGenerator.parse(data); + fileInfos = FileTreeViewGenerator.parse(data); } catch (Exception e) { log.error("Cannot extract file content because: {}", e.getMessage()); } From 96653ea9e5b3625ff71e2455ff96b229200b9843 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 10 Oct 2024 09:34:25 +0200 Subject: [PATCH 9/9] log errors and removed unneeded consts --- .../MetadataBitstreamRestRepository.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 266981a77c3a..224dc5a656c7 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -83,8 +83,6 @@ @Component(MetadataBitstreamWrapperRest.CATEGORY + "." + MetadataBitstreamWrapperRest.NAME) public class MetadataBitstreamRestRepository extends DSpaceRestRepository { private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataBitstreamRestRepository.class); - private final String FILE_EXTENSION_ZIP = ".zip"; - private final String FILE_EXTENSION_TAR = ".tar"; private final String ARCHIVE_TYPE_ZIP = "zip"; private final String ARCHIVE_TYPE_TAR = "tar"; // This constant is used to limit the length of the preview content stored in the database to prevent @@ -395,7 +393,8 @@ private String composePreviewURL(Context context, Item item, Bitstream bitstream * @throws IOException if an I/O error occurs while creating the file */ private Path createTempFile(String fileType) throws IOException { - String extension = FILE_EXTENSION_TAR.equals(fileType) ? FILE_EXTENSION_TAR : FILE_EXTENSION_ZIP; + String extension = ARCHIVE_TYPE_TAR.equals(fileType) ? + String.format(".%s", ARCHIVE_TYPE_TAR) : String.format(".%s", ARCHIVE_TYPE_ZIP); return Files.createTempFile("temp", extension); } @@ -440,7 +439,7 @@ private void processZipFile(List filePaths, FileSystem zipFileSystem) th long fileSize = Files.size(path); addFilePath(filePaths, path.toString().substring(1), fileSize); } catch (IOException e) { - e.printStackTrace(); + log.error("An error occurred while getting the size of the zip file.", e); } }); } @@ -450,11 +449,11 @@ private void processZipFile(List filePaths, FileSystem zipFileSystem) th * @param zipFileSystem the FileSystem to close */ private void closeFileSystem(FileSystem zipFileSystem) { - if (!Objects.isNull(zipFileSystem)) { + if (Objects.nonNull(zipFileSystem)) { try { zipFileSystem.close(); } catch (IOException e) { - e.printStackTrace(); + log.error("An error occurred while closing the zip file.", e); } } } @@ -464,11 +463,11 @@ private void closeFileSystem(FileSystem zipFileSystem) { * @param tempFile the Path object representing the temporary file to delete */ private void deleteTempFile(Path tempFile) { - if (!Objects.isNull(tempFile)) { + if (Objects.nonNull(tempFile)) { try { Files.delete(tempFile); } catch (IOException e) { - e.printStackTrace(); + log.error("An error occurred while deleting temp file.", e); } } } @@ -525,14 +524,14 @@ public String extractFile(InputStream inputStream, String fileType) { Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING); // Process the file based on its type - if ("tar".equals(fileType)) { + if (ARCHIVE_TYPE_TAR.equals(fileType)) { processTarFile(filePaths, tempFile); } else { zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null); processZipFile(filePaths, zipFileSystem); } } catch (IOException e) { - e.printStackTrace(); + log.error(String.format("An error occurred while extracting file of type %s.", fileType), e); } finally { closeFileSystem(zipFileSystem); deleteTempFile(tempFile);