Skip to content

Commit

Permalink
Better binary file detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Jul 11, 2024
1 parent 11864ff commit 2316651
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/us/deathmarine/luyten/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -445,7 +446,16 @@ public void extractSimpleFileEntryToTextPane(InputStream inputStream, String tab
return;
}

// build tab content and check if file is binary
// simple isBinary check
boolean isBinary = false;
try {
String type = Files.probeContentType(Paths.get(path));
if (type == null || !type.startsWith("text")) isBinary = true;
} catch (Throwable ignored) {
// If it fails, it fails - does not matter!
}

// build tab content and again check if file is binary
double ascii = 0;
double other = 0;
StringBuilder sb = new StringBuilder();
Expand All @@ -462,7 +472,7 @@ public void extractSimpleFileEntryToTextPane(InputStream inputStream, String tab
}
}

if (other != 0 && other / (ascii + other) > 0.5) {
if (isBinary && other != 0 && other / (ascii + other) > 0.5) {
throw new FileIsBinaryException();
}

Expand Down

0 comments on commit 2316651

Please sign in to comment.