diff --git a/source/net/sourceforge/filebot/cli/ArgumentProcessor.java b/source/net/sourceforge/filebot/cli/ArgumentProcessor.java index 28645047..df4e2fb0 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentProcessor.java +++ b/source/net/sourceforge/filebot/cli/ArgumentProcessor.java @@ -172,19 +172,20 @@ public class ArgumentProcessor { } // map old files to new paths by applying formatting and validating filenames - Map renameMap = new LinkedHashMap(); + Map renameMap = new LinkedHashMap(); for (Match match : matches) { File file = match.getValue(); Episode episode = match.getCandidate(); String newName = (format != null) ? format.format(new MediaBindingBean(episode, file)) : EpisodeFormat.SeasonEpisode.format(episode); + File newFile = new File(newName + "." + getExtension(file)); - if (isInvalidFileName(newName)) { + if (isInvalidFilePath(newFile)) { CLILogger.config("Stripping invalid characters from new name: " + newName); - newName = validateFileName(newName); + newFile = validateFilePath(newFile); } - renameMap.put(file, newName + "." + getExtension(file)); + renameMap.put(file, newFile); } // rename episodes @@ -209,20 +210,21 @@ public class ArgumentProcessor { } // map old files to new paths by applying formatting and validating filenames - Map renameMap = new LinkedHashMap(); + Map renameMap = new LinkedHashMap(); for (int i = 0; i < movieFiles.length; i++) { if (movieDescriptors[i] != null) { Movie movie = movieDescriptors[i]; File file = movieFiles[i]; String newName = (format != null) ? format.format(new MediaBindingBean(movie, file)) : movie.toString(); + File newFile = new File(newName + "." + getExtension(file)); - if (isInvalidFileName(newName)) { + if (isInvalidFilePath(newFile)) { CLILogger.config("Stripping invalid characters from new path: " + newName); - newName = validateFileName(newName); + newFile = validateFilePath(newFile); } - renameMap.put(file, newName + "." + getExtension(file)); + renameMap.put(file, newFile); } else { CLILogger.warning("No matching movie: " + movieFiles[i]); } @@ -237,8 +239,9 @@ public class ArgumentProcessor { String movieName = getName(movieFiles[i]); if (subtitleName.equalsIgnoreCase(movieName)) { - String movieDestinationName = renameMap.get(movieFiles[i]); - renameMap.put(subtitleFile, getNameWithoutExtension(movieDestinationName) + "." + getExtension(subtitleFile)); + File movieDestination = renameMap.get(movieFiles[i]); + File subtitleDestination = new File(movieDestination.getParentFile(), getName(movieDestination) + "." + getExtension(subtitleFile)); + renameMap.put(subtitleFile, subtitleDestination); // movie match found, we're done break; @@ -375,12 +378,12 @@ public class ArgumentProcessor { } - private Set renameAll(Map renameMap) throws Exception { + private Set renameAll(Map renameMap) throws Exception { // rename files final List> renameLog = new ArrayList>(); try { - for (Entry it : renameMap.entrySet()) { + for (Entry it : renameMap.entrySet()) { try { // rename file, throw exception on failure File destination = renameFile(it.getKey(), it.getValue()); diff --git a/source/net/sourceforge/filebot/ui/rename/HistorySpooler.java b/source/net/sourceforge/filebot/ui/rename/HistorySpooler.java index 3f0b8921..0ae3a481 100644 --- a/source/net/sourceforge/filebot/ui/rename/HistorySpooler.java +++ b/source/net/sourceforge/filebot/ui/rename/HistorySpooler.java @@ -50,11 +50,11 @@ public final class HistorySpooler { } - public synchronized void append(Iterable> elements) { + public synchronized void append(Iterable> elements) { List sequence = new ArrayList(); - for (Entry element : elements) { - sequence.add(new Element(element.getKey().getName(), element.getValue(), element.getKey().getParentFile())); + for (Entry element : elements) { + sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile())); } // append to session history diff --git a/source/net/sourceforge/filebot/ui/rename/RenameAction.java b/source/net/sourceforge/filebot/ui/rename/RenameAction.java index ed5dd75d..75c2804e 100644 --- a/source/net/sourceforge/filebot/ui/rename/RenameAction.java +++ b/source/net/sourceforge/filebot/ui/rename/RenameAction.java @@ -16,6 +16,7 @@ import java.util.HashSet; import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.AbstractMap.SimpleEntry; import java.util.Map.Entry; import javax.swing.AbstractAction; @@ -39,10 +40,10 @@ class RenameAction extends AbstractAction { public void actionPerformed(ActionEvent evt) { - List> renameLog = new ArrayList>(); + List> renameLog = new ArrayList>(); try { - for (Entry mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) { + for (Entry mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) { // rename file, throw exception on failure renameFile(mapping.getKey(), mapping.getValue()); @@ -59,12 +60,12 @@ class RenameAction extends AbstractAction { UILogger.warning(e.getMessage()); // revert rename operations in reverse order - for (ListIterator> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) { - Entry mapping = iterator.previous(); + for (ListIterator> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) { + Entry mapping = iterator.previous(); // revert rename File original = mapping.getKey(); - File current = new File(original.getParentFile(), mapping.getValue()); + File current = new File(original.getParentFile(), mapping.getValue().getPath()); if (current.renameTo(original)) { // remove reverted rename operation from log @@ -100,19 +101,22 @@ class RenameAction extends AbstractAction { } - private Iterable> validate(Map renameMap, Window parent) { - final List> source = new ArrayList>(renameMap.entrySet()); + private Iterable> validate(Map renameMap, Window parent) { + final List> source = new ArrayList>(renameMap.size()); + for (Entry entry : renameMap.entrySet()) { + source.add(new SimpleEntry(entry.getKey(), new File(entry.getValue()))); + } - List destinationFileNameView = new AbstractList() { + List destinationFileNameView = new AbstractList() { @Override - public String get(int index) { + public File get(int index) { return source.get(index).getValue(); } @Override - public String set(int index, String name) { + public File set(int index, File name) { return source.get(index).setValue(name); } @@ -131,5 +135,4 @@ class RenameAction extends AbstractAction { // return empty list if validation was cancelled return emptyList(); } - } diff --git a/source/net/sourceforge/filebot/ui/rename/ValidateDialog.java b/source/net/sourceforge/filebot/ui/rename/ValidateDialog.java index e3a31ca7..a6eb9cbd 100644 --- a/source/net/sourceforge/filebot/ui/rename/ValidateDialog.java +++ b/source/net/sourceforge/filebot/ui/rename/ValidateDialog.java @@ -39,15 +39,15 @@ class ValidateDialog extends JDialog { private final JList list; - private String[] model; + private File[] model; private boolean cancelled = true; - public ValidateDialog(Window owner, Collection source) { + public ValidateDialog(Window owner, Collection source) { super(owner, "Invalid Names", ModalityType.DOCUMENT_MODAL); - model = source.toArray(new String[0]); + model = source.toArray(new File[0]); list = new JList(model); list.setEnabled(false); @@ -99,7 +99,7 @@ class ValidateDialog extends JDialog { } - public List getModel() { + public List getModel() { return unmodifiableList(Arrays.asList(model)); } @@ -124,7 +124,7 @@ class ValidateDialog extends JDialog { // validate names for (int i = 0; i < model.length; i++) { // remove illegal characters - model[i] = validateFilePath(new File(model[i])).getPath(); + model[i] = validateFilePath(model[i]); } // update view @@ -155,14 +155,12 @@ class ValidateDialog extends JDialog { }; - public static boolean validate(Component parent, List source) { - IndexView invalidFilePaths = new IndexView(source); + public static boolean validate(Component parent, List source) { + IndexView invalidFilePaths = new IndexView(source); for (int i = 0; i < source.size(); i++) { - String path = source.get(i); - // invalid file names are also invalid file paths - if (isInvalidFilePath(new File(path))) { + if (isInvalidFilePath(source.get(i))) { invalidFilePaths.addIndex(i); } } @@ -182,7 +180,7 @@ class ValidateDialog extends JDialog { return false; } - List validatedFilePaths = dialog.getModel(); + List validatedFilePaths = dialog.getModel(); // validate source list via index view for (int i = 0; i < invalidFilePaths.size(); i++) { diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index d1ac5def..4531c995 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -30,13 +30,11 @@ import com.ibm.icu.text.CharsetMatch; public final class FileUtilities { - public static File renameFile(File source, String newPath) throws IOException { - File destination = new File(newPath); - + public static File renameFile(File source, File destination) throws IOException { // resolve destination if (!destination.isAbsolute()) { // same folder, different name - destination = new File(source.getParentFile(), newPath); + destination = new File(source.getParentFile(), destination.getPath()); } // make sure we that we can create the destination folder structure