From 28fa511fbe6874c8c3c715db97ffe36b0675ceab Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 21 Nov 2016 02:25:46 +0800 Subject: [PATCH] IOException if text file is insanely huge (>1GB) @see https://www.filebot.net/forums/viewtopic.php?f=10&t=4364 --- source/net/filebot/cli/BindingsHandler.java | 2 +- source/net/filebot/media/MediaDetection.java | 25 ++++++++++---------- source/net/filebot/util/FileUtilities.java | 10 ++++++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/source/net/filebot/cli/BindingsHandler.java b/source/net/filebot/cli/BindingsHandler.java index eca5103b..8cf57cb3 100644 --- a/source/net/filebot/cli/BindingsHandler.java +++ b/source/net/filebot/cli/BindingsHandler.java @@ -70,7 +70,7 @@ public class BindingsHandler extends MapOptionHandler { try { return readTextFile(f).trim(); } catch (IOException e) { - throw new CmdLineException(owner, e.getMessage(), e); + throw new CmdLineException(owner, "Failed to read @file", e); } } return v; diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 3b8fc351..0b0af836 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -583,11 +583,15 @@ public class MediaDetection { } // try to grep imdb id from nfo files - for (int imdbid : grepImdbIdFor(movieFile)) { - Movie movie = service.getMovieDescriptor(new Movie(imdbid), locale); - if (movie != null) { - options.add(movie); + try { + for (int imdbid : grepImdbIdFor(movieFile)) { + Movie movie = service.getMovieDescriptor(new Movie(imdbid), locale); + if (movie != null) { + options.add(movie); + } } + } catch (Exception e) { + debug.warning("Failed to lookup info by id: " + e); } } @@ -1210,20 +1214,16 @@ public class MediaDetection { public static Set grepImdbIdFor(File file) throws Exception { Set collection = new LinkedHashSet(); List nfoFiles = new ArrayList(); + if (file.isDirectory()) { nfoFiles.addAll(listFiles(file, NFO_FILES)); } else if (file.getParentFile() != null && file.getParentFile().isDirectory()) { nfoFiles.addAll(getChildren(file.getParentFile(), NFO_FILES)); } - // parse ids from nfo files + // parse IMDb IDs from NFO files for (File nfo : nfoFiles) { - try { - String text = readTextFile(nfo); - collection.addAll(grepImdbId(text)); - } catch (Exception e) { - debug.warning("Failed to read nfo: " + e.getMessage()); - } + collection.addAll(grepImdbId(readTextFile(nfo))); } return collection; @@ -1242,8 +1242,9 @@ public class MediaDetection { // search for id in sibling nfo files for (File folder : folders) { - if (!folder.exists()) + if (!folder.exists()) { continue; + } for (File nfo : getChildren(folder, NFO_FILES)) { String text = readTextFile(nfo); diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index d112e788..cf6d7b2b 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -195,10 +195,16 @@ public final class FileUtilities { } public static String readTextFile(File file) throws IOException { + long size = file.length(); + + // ignore absurdly large text files that might cause OutOfMemoryError issues + if (size > ONE_GIGABYTE) { + throw new IOException(String.format("Text file is too large: %s (%s)", file, formatSize(size))); + } + byte[] bytes = readFile(file); - // check BOM - BOM bom = BOM.detect(bytes); + BOM bom = BOM.detect(bytes); // check BOM if (bom != null) { return new String(bytes, bom.size(), bytes.length - bom.size(), bom.getCharset());