mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-23 16:28:51 -05:00
Refactor
This commit is contained in:
parent
a8d91485ae
commit
e0cc5eb771
@ -20,7 +20,7 @@ public enum NativeRenameAction implements RenameAction {
|
||||
|
||||
@Override
|
||||
public File rename(File src, File dst) {
|
||||
dst = resolveDestination(src, dst);
|
||||
dst = resolve(src, dst);
|
||||
rename(singletonMap(src, dst));
|
||||
return dst;
|
||||
}
|
||||
@ -33,7 +33,7 @@ public enum NativeRenameAction implements RenameAction {
|
||||
int i = 0;
|
||||
for (Entry<File, File> it : map.entrySet()) {
|
||||
src[i] = it.getKey().getAbsolutePath();
|
||||
dst[i] = resolveDestination(it.getKey(), it.getValue()).getAbsolutePath();
|
||||
dst[i] = resolve(it.getKey(), it.getValue()).getAbsolutePath();
|
||||
i++;
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ public enum StandardRenameAction implements RenameAction {
|
||||
|
||||
@Override
|
||||
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
|
||||
try {
|
||||
@ -48,7 +48,7 @@ public enum StandardRenameAction implements RenameAction {
|
||||
|
||||
@Override
|
||||
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
|
||||
try {
|
||||
@ -63,7 +63,7 @@ public enum StandardRenameAction implements RenameAction {
|
||||
|
||||
@Override
|
||||
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
|
||||
try {
|
||||
@ -104,7 +104,7 @@ public enum StandardRenameAction implements RenameAction {
|
||||
|
||||
@Override
|
||||
public File rename(File from, File to) throws IOException {
|
||||
return FileUtilities.resolveDestination(from, to, false);
|
||||
return FileUtilities.resolve(from, to);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -601,7 +601,7 @@ public class CmdlineOperations implements CmdlineInterface {
|
||||
// resolve destination
|
||||
if (!destination.isAbsolute()) {
|
||||
// same folder, different name
|
||||
destination = resolveDestination(source, destination, false);
|
||||
destination = resolve(source, destination);
|
||||
}
|
||||
|
||||
if (!destination.equals(source) && destination.exists() && renameAction != StandardRenameAction.TEST) {
|
||||
|
@ -122,7 +122,7 @@ class RenameAction extends AbstractAction {
|
||||
File file = match.getCandidate();
|
||||
Object meta = match.getValue();
|
||||
if (renameMap.containsKey(file) && meta != null) {
|
||||
File destination = resolveDestination(file, renameMap.get(file), false);
|
||||
File destination = resolve(file, renameMap.get(file));
|
||||
if (destination.isFile()) {
|
||||
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 {
|
||||
// ask for user permissions to output paths
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -161,13 +161,7 @@ class RenameAction extends AbstractAction {
|
||||
|
||||
for (Entry<File, File> mapping : renamePlan) {
|
||||
File source = mapping.getKey();
|
||||
File destination = mapping.getValue();
|
||||
|
||||
// resolve destination
|
||||
if (!destination.isAbsolute()) {
|
||||
// same folder, different name
|
||||
destination = new File(source.getParentFile(), destination.getPath());
|
||||
}
|
||||
File destination = resolve(source, mapping.getValue());
|
||||
|
||||
try {
|
||||
if (renameMap.containsKey(source))
|
||||
@ -176,7 +170,7 @@ class RenameAction extends AbstractAction {
|
||||
if (destinationSet.contains(destination))
|
||||
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());
|
||||
|
||||
if (getExtension(destination) == null && destination.isFile())
|
||||
@ -313,7 +307,7 @@ class RenameAction extends AbstractAction {
|
||||
|
||||
// rename file, throw exception on failure
|
||||
File source = mapping.getKey();
|
||||
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false);
|
||||
File destination = resolve(mapping.getKey(), mapping.getValue());
|
||||
boolean isSameFile = source.equals(destination);
|
||||
if (!isSameFile || (isSameFile && !source.getName().equals(destination.getName()))) {
|
||||
action.rename(source, destination);
|
||||
@ -381,7 +375,7 @@ class RenameAction extends AbstractAction {
|
||||
Map<File, File> todo = new LinkedHashMap<File, File>();
|
||||
for (Entry<File, File> mapping : renameMap.entrySet()) {
|
||||
File source = mapping.getKey();
|
||||
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false);
|
||||
File destination = resolve(mapping.getKey(), mapping.getValue());
|
||||
if (!source.equals(destination)) {
|
||||
todo.put(source, destination);
|
||||
}
|
||||
@ -397,7 +391,7 @@ class RenameAction extends AbstractAction {
|
||||
} finally {
|
||||
// check status of renamed files
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,6 @@ import ca.odell.glazedlists.EventList;
|
||||
import ca.odell.glazedlists.TransformedList;
|
||||
import ca.odell.glazedlists.event.ListEvent;
|
||||
import net.filebot.similarity.Match;
|
||||
import net.filebot.util.FileUtilities;
|
||||
import net.filebot.util.ui.SwingUI;
|
||||
|
||||
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++) {
|
||||
if (hasComplement(i)) {
|
||||
// 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);
|
||||
StringBuilder nameBuilder = new StringBuilder();
|
||||
FormattedFuture task = names.get(i);
|
||||
StringBuilder destination = new StringBuilder();
|
||||
|
||||
// append formatted name, throw exception if not ready
|
||||
try {
|
||||
nameBuilder.append(formattedFuture.get(0, TimeUnit.SECONDS));
|
||||
destination.append(task.get(0, TimeUnit.SECONDS));
|
||||
} 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) {
|
||||
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) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// append extension, if desired
|
||||
if (preserveExtension) {
|
||||
String extension = FileUtilities.getExtension(originalFile);
|
||||
|
||||
String extension = getExtension(source);
|
||||
if (extension != null) {
|
||||
nameBuilder.append('.').append(extension.toLowerCase());
|
||||
destination.append('.').append(extension.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
// insert mapping
|
||||
if (map.put(originalFile, nameBuilder.toString()) != null) {
|
||||
throw new IllegalStateException(String.format("Duplicate file entry: \"%s\"", originalFile.getName()));
|
||||
if (map.put(source, destination.toString()) != null) {
|
||||
throw new IllegalStateException(String.format("Duplicate file: \"%s\"", source.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public final class FileUtilities {
|
||||
|
||||
public static File moveRename(File source, File destination) throws IOException {
|
||||
// resolve destination
|
||||
destination = resolveDestination(source, destination, true);
|
||||
destination = resolveDestination(source, destination);
|
||||
|
||||
if (source.isDirectory()) {
|
||||
// move folder
|
||||
@ -81,7 +81,7 @@ public final class FileUtilities {
|
||||
|
||||
public static File copyAs(File source, File destination) throws IOException {
|
||||
// resolve destination
|
||||
destination = resolveDestination(source, destination, true);
|
||||
destination = resolveDestination(source, destination);
|
||||
|
||||
if (source.isDirectory()) {
|
||||
// copy folder
|
||||
@ -93,28 +93,21 @@ public final class FileUtilities {
|
||||
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
|
||||
if (!destination.isAbsolute()) {
|
||||
// same folder, different name
|
||||
destination = new File(source.getParentFile(), destination.getPath());
|
||||
}
|
||||
|
||||
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
|
||||
if (!destination.isAbsolute()) {
|
||||
// same folder, different name
|
||||
destination = new File(source.getParentFile(), destination.getPath());
|
||||
}
|
||||
destination = resolve(source, destination);
|
||||
|
||||
// create parent folder if necessary
|
||||
if (mkdirs) {
|
||||
// make sure that the folder structure is created, and throw exception if the folder structure can't be created
|
||||
Files.createDirectories(destination.getParentFile().toPath());
|
||||
}
|
||||
// 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
|
||||
Files.createDirectories(destination.getParentFile().toPath());
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user