2007-12-23 19:28:04 +00:00
|
|
|
|
2008-02-28 00:39:49 +00:00
|
|
|
package net.sourceforge.filebot;
|
2007-12-23 19:28:04 +00:00
|
|
|
|
|
|
|
|
2008-03-21 01:45:21 +00:00
|
|
|
import java.io.File;
|
2008-03-25 21:03:32 +00:00
|
|
|
import java.io.FileFilter;
|
2008-04-20 16:03:19 +00:00
|
|
|
import java.io.FilenameFilter;
|
2008-03-21 01:45:21 +00:00
|
|
|
import java.util.List;
|
2008-03-16 22:58:21 +00:00
|
|
|
import java.util.regex.Pattern;
|
2008-03-16 14:00:10 +00:00
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
import net.sourceforge.tuned.FileUtil;
|
|
|
|
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-10-10 19:20:37 +00:00
|
|
|
public final class FileBotUtil {
|
2007-12-23 19:28:04 +00:00
|
|
|
|
2008-10-11 20:42:03 +00:00
|
|
|
public static final String getApplicationName() {
|
|
|
|
return "FileBot";
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public static final String getApplicationVersion() {
|
|
|
|
return "1.9";
|
|
|
|
};
|
|
|
|
|
2008-03-16 01:05:06 +00:00
|
|
|
/**
|
2008-07-13 17:59:05 +00:00
|
|
|
* Invalid characters in filenames: \, /, :, *, ?, ", <, >, |, \r and \n
|
2008-03-16 01:05:06 +00:00
|
|
|
*/
|
2008-06-02 19:12:28 +00:00
|
|
|
public static final String INVALID_CHARACTERS = "\\/:*?\"<>|\r\n";
|
2008-03-16 22:58:21 +00:00
|
|
|
public static final Pattern INVALID_CHARACTERS_PATTERN = Pattern.compile(String.format("[%s]+", Pattern.quote(INVALID_CHARACTERS)));
|
2008-03-16 01:05:06 +00:00
|
|
|
|
2008-10-29 22:54:16 +00:00
|
|
|
public static final Pattern EMBEDDED_CHECKSUM_PATTERN = Pattern.compile("[(\\[](\\p{XDigit}{8})[\\])]");
|
|
|
|
|
2008-03-16 01:05:06 +00:00
|
|
|
|
2008-02-03 18:06:12 +00:00
|
|
|
/**
|
2008-07-13 17:59:05 +00:00
|
|
|
* Strip filename of invalid characters
|
2008-02-03 18:06:12 +00:00
|
|
|
*
|
|
|
|
* @param filename original filename
|
2008-07-13 17:59:05 +00:00
|
|
|
* @return valid filename stripped of invalid characters
|
2008-02-03 18:06:12 +00:00
|
|
|
*/
|
|
|
|
public static String validateFileName(String filename) {
|
2008-04-20 16:03:19 +00:00
|
|
|
// strip invalid characters from filename
|
2008-03-16 22:58:21 +00:00
|
|
|
return INVALID_CHARACTERS_PATTERN.matcher(filename).replaceAll("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isInvalidFileName(String filename) {
|
|
|
|
return INVALID_CHARACTERS_PATTERN.matcher(filename).find();
|
2008-02-03 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
2008-04-20 16:03:19 +00:00
|
|
|
private static final String[] TORRENT_FILE_EXTENSIONS = { "torrent" };
|
|
|
|
private static final String[] SFV_FILE_EXTENSIONS = { "sfv" };
|
|
|
|
private static final String[] LIST_FILE_EXTENSIONS = { "txt", "list", "" };
|
2008-07-13 17:59:05 +00:00
|
|
|
private static final String[] SUBTITLE_FILE_EXTENSIONS = { "srt", "sub", "ssa", "smi" };
|
2008-04-20 16:03:19 +00:00
|
|
|
|
|
|
|
|
2008-03-21 01:45:21 +00:00
|
|
|
public static boolean containsOnlyFolders(List<File> files) {
|
|
|
|
for (File file : files) {
|
|
|
|
if (!file.isDirectory())
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-16 14:00:10 +00:00
|
|
|
|
2008-03-21 01:45:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean containsOnlyTorrentFiles(List<File> files) {
|
2008-04-20 16:03:19 +00:00
|
|
|
return containsOnly(files, TORRENT_FILE_EXTENSIONS);
|
2008-03-21 01:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean containsOnlySfvFiles(List<File> files) {
|
2008-04-20 16:03:19 +00:00
|
|
|
return containsOnly(files, SFV_FILE_EXTENSIONS);
|
2008-03-21 01:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean containsOnlyListFiles(List<File> files) {
|
2008-04-20 16:03:19 +00:00
|
|
|
return containsOnly(files, LIST_FILE_EXTENSIONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-13 17:59:05 +00:00
|
|
|
public static boolean containsOnly(List<File> files, String... extensions) {
|
2008-03-21 01:45:21 +00:00
|
|
|
for (File file : files) {
|
2008-06-21 19:24:18 +00:00
|
|
|
if (!FileUtil.hasExtension(file, extensions))
|
2008-03-21 01:45:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-25 21:03:32 +00:00
|
|
|
public static final FileFilter FOLDERS_ONLY = new FileFilter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public static final FileFilter FILES_ONLY = new FileFilter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return file.isFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2008-04-20 16:03:19 +00:00
|
|
|
|
|
|
|
public static final FilenameFilter SUBTITLES_ONLY = new FilenameFilter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String name) {
|
2008-06-21 19:24:18 +00:00
|
|
|
return FileUtil.hasExtension(name, SUBTITLE_FILE_EXTENSIONS);
|
2008-04-20 16:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-07-30 22:37:01 +00:00
|
|
|
|
2008-10-10 19:20:37 +00:00
|
|
|
/**
|
|
|
|
* Dummy constructor to prevent instantiation.
|
|
|
|
*/
|
2008-07-30 22:37:01 +00:00
|
|
|
private FileBotUtil() {
|
2008-10-10 19:20:37 +00:00
|
|
|
throw new UnsupportedOperationException();
|
2008-07-30 22:37:01 +00:00
|
|
|
}
|
|
|
|
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|