Avoid issues caused by strange text files

This commit is contained in:
Reinhard Pointner 2016-06-21 01:37:46 +08:00
parent 6b8b0537ad
commit ba439cc2b6
1 changed files with 13 additions and 7 deletions

View File

@ -1,6 +1,6 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
@ -8,10 +8,10 @@ import static net.filebot.util.FileUtilities.*;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
@ -69,14 +69,20 @@ class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy<File>
// load file paths from text files // load file paths from text files
if (recursive && LIST_FILES.accept(f)) { if (recursive && LIST_FILES.accept(f)) {
try { try {
List<File> list = Files.lines(f.toPath(), UTF_8).map(File::new).filter(it -> { String[] lines = readTextFile(f).split("\\R");
return it.isAbsolute() && it.exists(); List<File> paths = stream(lines).filter(s -> s.length() > 0).map(path -> {
}).collect(toList()); try {
File file = new File(path);
return file.isAbsolute() && file.exists() ? file : null;
} catch (Exception e) {
return null; // ignore invalid file paths
}
}).filter(Objects::nonNull).collect(toList());
if (list.isEmpty()) { if (paths.isEmpty()) {
sink.add(f); // treat as simple text file sink.add(f); // treat as simple text file
} else { } else {
load(list, false, sink); // add paths from text file load(paths, false, sink); // add paths from text file
} }
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.WARNING, "Failed to read paths from text file: " + e.getMessage()); debug.log(Level.WARNING, "Failed to read paths from text file: " + e.getMessage());