Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
ImgPriority change for Transkribus/TranskribusSwtGui#263
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlep committed Jan 24, 2019
1 parent 120f46a commit da7eb1a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Transkribus components' version numbers -->
<fimagestoreClient.version>0.4.1</fimagestoreClient.version>
<fimagestoreClient.version>0.4.2-SNAPSHOT</fimagestoreClient.version>
<pdfUtils.version>0.0.3</pdfUtils.version>
<interfaces.version>0.0.5</interfaces.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public boolean accept(File pathname) {
final String mime = MimeTypes.getMimeType(FilenameUtils.getExtension(pathname.getName()));
//is allowed mimetype and not starts with ".", which may occur on mac
//FIXME pathname.isFile() does not have to be checked here?
return pathname.isFile() && !pathname.getName().startsWith(".") && ImgPriority.priorities.containsKey(mime);
return pathname.isFile() && !pathname.getName().startsWith(".") && ImgPriority.containsMimeType(mime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ImgFilenameFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
final String mime = MimeTypes.getMimeType(FilenameUtils.getExtension(name));
//is allowed mimetype and not starts with ".", which may occur on mac
return !name.startsWith(".") && ImgPriority.priorities.containsKey(mime);
return !name.startsWith(".") && ImgPriority.containsMimeType(mime);
}

}
28 changes: 27 additions & 1 deletion src/main/java/eu/transkribus/core/io/util/ImgPriority.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package eu.transkribus.core.io.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.dea.fimagestore.core.util.MimeTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImgPriority {
private static final Logger logger = LoggerFactory.getLogger(ImgPriority.class);

// this map will be used to prioritize different images for one page on the path
public static Map<String, Integer> priorities = new HashMap<>();
private static Map<String, Integer> priorities = new HashMap<>();

static {
//jpg > tif/tiff
Expand All @@ -22,4 +28,24 @@ public static Integer getPriority(File f) {
final String mime = MimeTypes.getMimeType(FilenameUtils.getExtension(f.getName()));
return priorities.get(mime);
}

public static boolean containsMimeType(final String mimeType) {
return priorities.containsKey(mimeType);
}

public static List<String> getAllowedFilenameExtensions() {
List<String> exts = new ArrayList<>(ImgPriority.priorities.size() * 2);
for(String mimeType : ImgPriority.priorities.keySet()) {
List<String> extsForType = MimeTypes.lookupExtensions(mimeType);
if(extsForType.isEmpty()) {
logger.error("Reverse lookup of registered mimetype failed: " + mimeType);
continue;
}
for(String ext : extsForType) {
exts.add(ext.toLowerCase());
exts.add(ext.toUpperCase());
}
}
return exts;
}
}
22 changes: 22 additions & 0 deletions src/test/java/eu/transkribus/core/io/util/ImgPriorityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eu.transkribus.core.io.util;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImgPriorityTest {
private static final Logger logger = LoggerFactory.getLogger(ImgPriorityTest.class);

@Test
public void testGetAllowedFilenameExtensions() {
List<String> exts = ImgPriority.getAllowedFilenameExtensions();
Assert.assertNotNull(exts);
logger.debug("Collected allowed file extensions: " + exts.stream().collect(Collectors.joining(", ")));
Assert.assertFalse(exts.isEmpty());
}

}

0 comments on commit da7eb1a

Please sign in to comment.