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-06-02 19:12:28 +00:00
|
|
|
import java.io.FileOutputStream;
|
2008-04-20 16:03:19 +00:00
|
|
|
import java.io.FilenameFilter;
|
2008-06-02 19:12:28 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.FileChannel;
|
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
|
|
|
|
|
|
|
public class FileBotUtil {
|
|
|
|
|
|
|
|
private FileBotUtil() {
|
2008-04-20 16:03:19 +00:00
|
|
|
// hide constructor
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 01:05:06 +00:00
|
|
|
/**
|
2008-06-02 19:12:28 +00:00
|
|
|
* invalid characters: \, /, :, *, ?, ", <, >, |, \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-02-03 18:06:12 +00:00
|
|
|
/**
|
|
|
|
* Strip string of invalid characters
|
|
|
|
*
|
|
|
|
* @param filename original filename
|
|
|
|
* @return filename stripped of invalid characters
|
|
|
|
*/
|
|
|
|
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", "" };
|
|
|
|
private static final String[] SUBTITLE_FILE_EXTENSIONS = { "srt", "sub", "ssa" };
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private 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-06-02 19:12:28 +00:00
|
|
|
|
|
|
|
//TODO needed??
|
|
|
|
public static void writeToFile(File file, ByteBuffer data) throws IOException {
|
|
|
|
FileChannel channel = new FileOutputStream(file).getChannel();
|
|
|
|
channel.write(data);
|
|
|
|
channel.close();
|
|
|
|
}
|
|
|
|
|
2007-12-23 19:28:04 +00:00
|
|
|
}
|