Skip to content

Commit

Permalink
The max file preview size is loaded from the cfg (#630)
Browse files Browse the repository at this point in the history
* The max file preview size is loaded from the cfg

* Removed a condition to not preview files longer less 3 chars. And updated condition to check if the file is file or a folder.
  • Loading branch information
milanmajchrak authored Apr 24, 2024
1 parent fcf0397 commit 248dca2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -60,6 +63,7 @@
import org.dspace.util.FileInfo;
import org.dspace.util.FileTreeViewGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -72,7 +76,6 @@
@Component(MetadataBitstreamWrapperRest.CATEGORY + "." + MetadataBitstreamWrapperRest.NAME)
public class MetadataBitstreamRestRepository extends DSpaceRestRepository<MetadataBitstreamWrapperRest, Integer> {
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataBitstreamRestRepository.class);
private final static int MAX_FILE_PREVIEW_COUNT = 1000;

@Autowired
HandleService handleService;
Expand Down Expand Up @@ -102,6 +105,10 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada
@Autowired
ConfigurationService configurationService;

// Configured ZIP file preview limit (default: 1000) - if the ZIP file contains more files, it will be truncated
@Value("${file.preview.zip.limit.length:1000}")
private int maxPreviewCount;

@SearchRestMethod(name = "byHandle")
public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handle", required = true) String handle,
@Parameter(value = "fileGrpType") String fileGrpType,
Expand Down Expand Up @@ -345,29 +352,28 @@ public String extractFile(InputStream inputStream, String fileType) {
}
}

// Is a folder regex
String folderRegex = "/|\\d+";
Pattern pattern = Pattern.compile(folderRegex);

StringBuilder sb = new StringBuilder();
sb.append(("<root>"));
List<String> allFiles = filePaths;
Iterator<String> iterator = filePaths.iterator();
int fileCounter = 0;
for (String filePath : allFiles) {
if (!filePath.isEmpty() && filePath.length() > 3) {
if (filePath.contains(".")) {
fileCounter++;
}
sb.append("<element>");
sb.append(filePath);
sb.append("</element>");

if (fileCounter > MAX_FILE_PREVIEW_COUNT) {
sb.append("<element>");
sb.append("/|0");
sb.append("</element>");
sb.append("<element>");
sb.append("...too many files...|0");
sb.append("</element>");
break;
}
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++;
}
sb.append("<element>").append(filePath).append("</element>");
}

if (fileCounter > maxPreviewCount) {
sb.append("<element>...too many files...|0</element>");
}
sb.append(("</root>"));
return sb.toString();
Expand Down
3 changes: 3 additions & 0 deletions dspace/config/clarin-dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ shibboleth.name.conversion.outputEncoding = UTF-8
### File preview ###
# File preview is enabled by default
file.preview.enabled = false
# It the ZIP file contains more than 1000 files show only the first 1000 files
file.preview.zip.limit.length = 1000


### Storage service ###
# Synchronization is NOT enabled by default
Expand Down

0 comments on commit 248dca2

Please sign in to comment.