This commit is contained in:
Reinhard Pointner 2016-04-13 11:22:03 +00:00
parent a8d91485ae
commit e0cc5eb771
6 changed files with 31 additions and 46 deletions

View File

@ -20,7 +20,7 @@ public enum NativeRenameAction implements RenameAction {
@Override @Override
public File rename(File src, File dst) { public File rename(File src, File dst) {
dst = resolveDestination(src, dst); dst = resolve(src, dst);
rename(singletonMap(src, dst)); rename(singletonMap(src, dst));
return dst; return dst;
} }
@ -33,7 +33,7 @@ public enum NativeRenameAction implements RenameAction {
int i = 0; int i = 0;
for (Entry<File, File> it : map.entrySet()) { for (Entry<File, File> it : map.entrySet()) {
src[i] = it.getKey().getAbsolutePath(); src[i] = it.getKey().getAbsolutePath();
dst[i] = resolveDestination(it.getKey(), it.getValue()).getAbsolutePath(); dst[i] = resolve(it.getKey(), it.getValue()).getAbsolutePath();
i++; i++;
} }

View File

@ -30,7 +30,7 @@ public enum StandardRenameAction implements RenameAction {
@Override @Override
public File rename(File from, File to) throws Exception { public File rename(File from, File to) throws Exception {
File destionation = FileUtilities.resolveDestination(from, to, true); File destionation = FileUtilities.resolveDestination(from, to);
// move file and the create a symlink to the new location via NIO.2 // move file and the create a symlink to the new location via NIO.2
try { try {
@ -48,7 +48,7 @@ public enum StandardRenameAction implements RenameAction {
@Override @Override
public File rename(File from, File to) throws Exception { public File rename(File from, File to) throws Exception {
File destionation = FileUtilities.resolveDestination(from, to, true); File destionation = FileUtilities.resolveDestination(from, to);
// create symlink via NIO.2 // create symlink via NIO.2
try { try {
@ -63,7 +63,7 @@ public enum StandardRenameAction implements RenameAction {
@Override @Override
public File rename(File from, File to) throws Exception { public File rename(File from, File to) throws Exception {
File destionation = FileUtilities.resolveDestination(from, to, true); File destionation = FileUtilities.resolveDestination(from, to);
// create hardlink via NIO.2 // create hardlink via NIO.2
try { try {
@ -104,7 +104,7 @@ public enum StandardRenameAction implements RenameAction {
@Override @Override
public File rename(File from, File to) throws IOException { public File rename(File from, File to) throws IOException {
return FileUtilities.resolveDestination(from, to, false); return FileUtilities.resolve(from, to);
} }
}; };

View File

@ -601,7 +601,7 @@ public class CmdlineOperations implements CmdlineInterface {
// resolve destination // resolve destination
if (!destination.isAbsolute()) { if (!destination.isAbsolute()) {
// same folder, different name // same folder, different name
destination = resolveDestination(source, destination, false); destination = resolve(source, destination);
} }
if (!destination.equals(source) && destination.exists() && renameAction != StandardRenameAction.TEST) { if (!destination.equals(source) && destination.exists() && renameAction != StandardRenameAction.TEST) {

View File

@ -122,7 +122,7 @@ class RenameAction extends AbstractAction {
File file = match.getCandidate(); File file = match.getCandidate();
Object meta = match.getValue(); Object meta = match.getValue();
if (renameMap.containsKey(file) && meta != null) { if (renameMap.containsKey(file) && meta != null) {
File destination = resolveDestination(file, renameMap.get(file), false); File destination = resolve(file, renameMap.get(file));
if (destination.isFile()) { if (destination.isFile()) {
xattr.setMetaInfo(destination, meta, file.getName()); xattr.setMetaInfo(destination, meta, file.getName());
} }
@ -149,7 +149,7 @@ class RenameAction extends AbstractAction {
private Map<File, File> checkRenamePlan(List<Entry<File, File>> renamePlan, Window parent) throws IOException { private Map<File, File> checkRenamePlan(List<Entry<File, File>> renamePlan, Window parent) throws IOException {
// ask for user permissions to output paths // ask for user permissions to output paths
if (isMacSandbox()) { if (isMacSandbox()) {
if (!MacAppUtilities.askUnlockFolders(parent, renamePlan.stream().flatMap(e -> Stream.of(e.getKey(), resolveDestination(e.getKey(), e.getValue()))).map(f -> new File(f.getAbsolutePath())).collect(Collectors.toList()))) { if (!MacAppUtilities.askUnlockFolders(parent, renamePlan.stream().flatMap(e -> Stream.of(e.getKey(), resolve(e.getKey(), e.getValue()))).map(f -> new File(f.getAbsolutePath())).collect(Collectors.toList()))) {
return emptyMap(); return emptyMap();
} }
} }
@ -161,13 +161,7 @@ class RenameAction extends AbstractAction {
for (Entry<File, File> mapping : renamePlan) { for (Entry<File, File> mapping : renamePlan) {
File source = mapping.getKey(); File source = mapping.getKey();
File destination = mapping.getValue(); File destination = resolve(source, mapping.getValue());
// resolve destination
if (!destination.isAbsolute()) {
// same folder, different name
destination = new File(source.getParentFile(), destination.getPath());
}
try { try {
if (renameMap.containsKey(source)) if (renameMap.containsKey(source))
@ -176,7 +170,7 @@ class RenameAction extends AbstractAction {
if (destinationSet.contains(destination)) if (destinationSet.contains(destination))
throw new IllegalArgumentException("Conflict detected: " + mapping.getValue().getPath()); throw new IllegalArgumentException("Conflict detected: " + mapping.getValue().getPath());
if (destination.exists() && !resolveDestination(mapping.getKey(), mapping.getValue(), false).equals(mapping.getKey())) if (destination.exists() && !resolve(mapping.getKey(), mapping.getValue()).equals(mapping.getKey()))
throw new IllegalArgumentException("File already exists: " + mapping.getValue().getPath()); throw new IllegalArgumentException("File already exists: " + mapping.getValue().getPath());
if (getExtension(destination) == null && destination.isFile()) if (getExtension(destination) == null && destination.isFile())
@ -313,7 +307,7 @@ class RenameAction extends AbstractAction {
// rename file, throw exception on failure // rename file, throw exception on failure
File source = mapping.getKey(); File source = mapping.getKey();
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false); File destination = resolve(mapping.getKey(), mapping.getValue());
boolean isSameFile = source.equals(destination); boolean isSameFile = source.equals(destination);
if (!isSameFile || (isSameFile && !source.getName().equals(destination.getName()))) { if (!isSameFile || (isSameFile && !source.getName().equals(destination.getName()))) {
action.rename(source, destination); action.rename(source, destination);
@ -381,7 +375,7 @@ class RenameAction extends AbstractAction {
Map<File, File> todo = new LinkedHashMap<File, File>(); Map<File, File> todo = new LinkedHashMap<File, File>();
for (Entry<File, File> mapping : renameMap.entrySet()) { for (Entry<File, File> mapping : renameMap.entrySet()) {
File source = mapping.getKey(); File source = mapping.getKey();
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false); File destination = resolve(mapping.getKey(), mapping.getValue());
if (!source.equals(destination)) { if (!source.equals(destination)) {
todo.put(source, destination); todo.put(source, destination);
} }
@ -397,7 +391,7 @@ class RenameAction extends AbstractAction {
} finally { } finally {
// check status of renamed files // check status of renamed files
for (Entry<File, File> it : renameMap.entrySet()) { for (Entry<File, File> it : renameMap.entrySet()) {
if (resolveDestination(it.getKey(), it.getValue(), false).exists()) { if (resolve(it.getKey(), it.getValue()).exists()) {
renameLog.put(it.getKey(), it.getValue()); renameLog.put(it.getKey(), it.getValue());
} }
} }

View File

@ -28,7 +28,6 @@ import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.TransformedList; import ca.odell.glazedlists.TransformedList;
import ca.odell.glazedlists.event.ListEvent; import ca.odell.glazedlists.event.ListEvent;
import net.filebot.similarity.Match; import net.filebot.similarity.Match;
import net.filebot.util.FileUtilities;
import net.filebot.util.ui.SwingUI; import net.filebot.util.ui.SwingUI;
public class RenameModel extends MatchModel<Object, File> { public class RenameModel extends MatchModel<Object, File> {
@ -80,34 +79,33 @@ public class RenameModel extends MatchModel<Object, File> {
for (int i = 0; i < names.size(); i++) { for (int i = 0; i < names.size(); i++) {
if (hasComplement(i)) { if (hasComplement(i)) {
// make sure we're dealing with regular File objects form here on out // make sure we're dealing with regular File objects form here on out
File originalFile = new File(files().get(i).getPath()); File source = new File(files().get(i).getPath());
FormattedFuture formattedFuture = names.get(i); FormattedFuture task = names.get(i);
StringBuilder nameBuilder = new StringBuilder(); StringBuilder destination = new StringBuilder();
// append formatted name, throw exception if not ready // append formatted name, throw exception if not ready
try { try {
nameBuilder.append(formattedFuture.get(0, TimeUnit.SECONDS)); destination.append(task.get(0, TimeUnit.SECONDS));
} catch (ExecutionException e) { } catch (ExecutionException e) {
throw new IllegalStateException(String.format("\"%s\" could not be formatted: %s.", formattedFuture.preview(), e.getCause().getMessage())); throw new IllegalStateException(String.format("\"%s\" could not be formatted: %s.", task.preview(), e.getCause().getMessage()));
} catch (TimeoutException e) { } catch (TimeoutException e) {
throw new IllegalStateException(String.format("\"%s\" has not been formatted yet.", formattedFuture.preview())); throw new IllegalStateException(String.format("\"%s\" has not been formatted yet.", task.preview()));
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
// append extension, if desired // append extension, if desired
if (preserveExtension) { if (preserveExtension) {
String extension = FileUtilities.getExtension(originalFile); String extension = getExtension(source);
if (extension != null) { if (extension != null) {
nameBuilder.append('.').append(extension.toLowerCase()); destination.append('.').append(extension.toLowerCase());
} }
} }
// insert mapping // insert mapping
if (map.put(originalFile, nameBuilder.toString()) != null) { if (map.put(source, destination.toString()) != null) {
throw new IllegalStateException(String.format("Duplicate file entry: \"%s\"", originalFile.getName())); throw new IllegalStateException(String.format("Duplicate file: \"%s\"", source.getName()));
} }
} }
} }

View File

@ -57,7 +57,7 @@ public final class FileUtilities {
public static File moveRename(File source, File destination) throws IOException { public static File moveRename(File source, File destination) throws IOException {
// resolve destination // resolve destination
destination = resolveDestination(source, destination, true); destination = resolveDestination(source, destination);
if (source.isDirectory()) { if (source.isDirectory()) {
// move folder // move folder
@ -81,7 +81,7 @@ public final class FileUtilities {
public static File copyAs(File source, File destination) throws IOException { public static File copyAs(File source, File destination) throws IOException {
// resolve destination // resolve destination
destination = resolveDestination(source, destination, true); destination = resolveDestination(source, destination);
if (source.isDirectory()) { if (source.isDirectory()) {
// copy folder // copy folder
@ -93,28 +93,21 @@ public final class FileUtilities {
return Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING).toFile(); return Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING).toFile();
} }
public static File resolveDestination(File source, File destination) { public static File resolve(File source, File destination) {
// resolve destination // resolve destination
if (!destination.isAbsolute()) { if (!destination.isAbsolute()) {
// same folder, different name // same folder, different name
destination = new File(source.getParentFile(), destination.getPath()); destination = new File(source.getParentFile(), destination.getPath());
} }
return destination; return destination;
} }
public static File resolveDestination(File source, File destination, boolean mkdirs) throws IOException { public static File resolveDestination(File source, File destination) throws IOException {
// resolve destination // resolve destination
if (!destination.isAbsolute()) { destination = resolve(source, destination);
// same folder, different name
destination = new File(source.getParentFile(), destination.getPath());
}
// create parent folder if necessary // create parent folder if necessary and make sure that the folder structure is created, and throw exception if the folder structure can't be created
if (mkdirs) { Files.createDirectories(destination.getParentFile().toPath());
// make sure that the folder structure is created, and throw exception if the folder structure can't be created
Files.createDirectories(destination.getParentFile().toPath());
}
return destination; return destination;
} }