001package net.filebot.cli;
002
003import static java.util.Arrays.*;
004import static java.util.stream.Collectors.*;
005import static net.filebot.util.FileUtilities.*;
006
007import java.io.File;
008import java.util.List;
009import java.util.stream.IntStream;
010
011import net.filebot.media.VideoQuality;
012
013public enum StandardConflictAction implements ConflictAction {
014
015        SKIP {
016
017                @Override
018                public File conflict(File from, File to) throws Exception {
019                        return null;
020                }
021        },
022
023        REPLACE {
024
025                @Override
026                public File conflict(File from, File to) throws Exception {
027                        return to;
028                }
029        },
030
031        AUTO {
032
033                @Override
034                public File conflict(File from, File to) throws Exception {
035                        return VideoQuality.isBetter(from, to) ? to : null;
036                }
037        },
038
039        INDEX {
040
041                @Override
042                public File conflict(File from, File to) throws Exception {
043                        // generate indexed destination path (e.g. name.mp4 -> name.1.mp4)
044                        File folder = to.getParentFile();
045                        String name = getName(to);
046                        String extension = getExtension(to);
047
048                        return IntStream.range(1, 100).mapToObj(i -> {
049                                return new File(folder, name + '.' + i + '.' + extension);
050                        }).filter(f -> !f.exists()).findFirst().orElse(null);
051                }
052        },
053
054        FAIL {
055
056                @Override
057                public File conflict(File from, File to) throws Exception {
058                        throw new Exception("Cannot process [" + from + "] because [" + to + "] already exists");
059                }
060        };
061
062        public static List<String> names() {
063                return stream(values()).map(Enum::name).collect(toList());
064        }
065
066        public static StandardConflictAction forName(String name) {
067                for (StandardConflictAction action : values()) {
068                        if (action.name().equalsIgnoreCase(name)) {
069                                return action;
070                        }
071                }
072
073                if ("override".equalsIgnoreCase(name) || "overwrite".equalsIgnoreCase(name)) {
074                        return REPLACE;
075                }
076
077                throw new IllegalArgumentException(name + " not in " + names());
078        }
079
080}