Search in sources :

Example 1 with StringPattern

use of com.biglybt.ui.common.util.StringPattern in project BiglyBT by BiglySoftware.

the class AddFind method addLocal.

/**
 * attempt a local add (arg may be a directory, a file or a pattern eg: d:/*.torrent)
 * @param ci
 * @param arg argument - could be directory, file or pattern eg: d:\*.torrent
 * @param outputDir directory to save files from torrent to
 * @param scansubdir if true, will recurse subdirectories looking for files to add
 * @param finding if true, don't start downloading the files; simply add them to the 'found' list
 */
protected void addLocal(ConsoleInput ci, String arg, String outputDir, boolean scansubdir, boolean finding) {
    // substitute ~ for home directory, if specified
    arg = transformLocalArgument(arg);
    // see if the argument is an existing file or directory
    File test = new File(arg);
    if (test.exists()) {
        if (test.isDirectory()) {
            final List<File> toAdd = new ArrayList<>();
            try {
                if (scansubdir) {
                    Files.walk(test.toPath()).filter(new Predicate<Path>() {

                        @Override
                        public boolean test(Path p) {
                            return p.toString().endsWith(".torrent") || p.toString().endsWith(".tor");
                        }
                    }).filter(new Predicate<Path>() {

                        @Override
                        public boolean test(Path p) {
                            return toAdd.add(p.toFile());
                        }
                    });
                } else {
                    Files.list(test.toPath()).filter(new Predicate<Path>() {

                        @Override
                        public boolean test(Path p) {
                            return p.toString().endsWith(".torrent") || p.toString().endsWith(".tor");
                        }
                    }).filter(new Predicate<Path>() {

                        @Override
                        public boolean test(Path p) {
                            return toAdd.add(p.toFile());
                        }
                    });
                }
            } catch (IOException e) {
            }
            if (toAdd.size() > 0) {
                addFiles(ci, toAdd.toArray(new File[toAdd.size()]), finding, outputDir);
            } else {
                ci.adds = null;
                ci.out.println("> Directory '" + arg + "' seems to contain no torrent files.");
            }
        } else {
            ci.downloadTorrent(arg, outputDir);
            ci.out.println("> '" + arg + "' added.");
            ci.torrents.clear();
        }
        return;
    }
    // check to see if they are numeric and if so, try and add them from the 'adds' in ci
    try {
        int id = Integer.parseInt(arg);
        if (ci.adds != null && ci.adds.length > id) {
            String torrentPath = ci.adds[id].getAbsolutePath();
            ci.downloadTorrent(torrentPath, outputDir);
            ci.out.println("> '" + torrentPath + "' added.");
            ci.torrents.clear();
        } else {
            ci.out.println("> No such file id '" + id + "'. Try \"add -l\" to list available files");
        }
        return;
    } catch (NumberFormatException e) {
    }
    // last resort - try to process it as a directory/pattern eg: c:/torrents/*.torrent
    String dirName = test.getParent();
    if (dirName == null)
        dirName = ".";
    final String filePattern = test.getName();
    final List<File> files = new ArrayList<>();
    try {
        Files.list(Paths.get(dirName)).filter(new Predicate<Path>() {

            @Override
            public boolean test(Path p) {
                return new StringPattern(p.toString()).matches(filePattern);
            }
        }).filter(new Predicate<Path>() {

            @Override
            public boolean test(Path p) {
                return files.add(p.toFile());
            }
        });
    } catch (IOException e) {
    }
    if (files.size() > 0) {
        addFiles(ci, files.toArray(new File[files.size()]), finding, outputDir);
    } else {
        ci.adds = null;
        ci.out.println("> No files found. Searched for '" + filePattern + "' in '" + dirName + "'");
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StringPattern(com.biglybt.ui.common.util.StringPattern) File(java.io.File) Predicate(java.util.function.Predicate)

Example 2 with StringPattern

use of com.biglybt.ui.common.util.StringPattern in project BiglyBT by BiglySoftware.

the class Set method execute.

@Override
public void execute(String commandName, ConsoleInput ci, List args) {
    boolean non_defaults = false;
    Iterator it = args.iterator();
    while (it.hasNext()) {
        String arg = (String) it.next();
        if (arg.equals("-export")) {
            non_defaults = true;
            it.remove();
        }
    }
    if (args.isEmpty()) {
        displayOptions(ci.out, new StringPattern("*"), non_defaults);
        return;
    }
    String external_name = (String) args.get(0);
    String internal_name = (String) ExternalUIConst.parameterlegacy.get(external_name);
    if (internal_name == null || internal_name.length() == 0) {
        internal_name = external_name;
    }
    // else
    // ci.out.println("> converting " + origParamName + " to " + parameter);
    Parameter param;
    switch(args.size()) {
        case 1:
            // allow wildcards : eg: Core* or *DHT* to shorten result list
            StringPattern sp = new StringPattern(internal_name);
            if (sp.hasWildcard()) {
                displayOptions(ci.out, sp, non_defaults);
            } else {
                // try to display the value of the specified parameter
                if (!COConfigurationManager.doesParameterDefaultExist(internal_name)) {
                    ci.out.println("> Command 'set': Parameter '" + external_name + "' unknown.");
                    return;
                }
                param = Parameter.get(internal_name, external_name);
                ci.out.println(param.getString(false));
            }
            break;
        case 2:
        case 3:
            String setto = (String) args.get(1);
            String type;
            if (args.size() == 2) {
                // guess the parameter type by getting the current value and determining its type
                param = Parameter.get(internal_name, external_name);
                type = param.getType();
            } else
                type = (String) args.get(2);
            boolean success = false;
            if (type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer")) {
                COConfigurationManager.setParameter(internal_name, Integer.parseInt(setto));
                success = true;
            } else if (type.equalsIgnoreCase("bool") || type.equalsIgnoreCase("boolean")) {
                boolean value;
                if (setto.equalsIgnoreCase("true") || setto.equalsIgnoreCase("y") || setto.equals("1")) {
                    value = true;
                } else {
                    value = false;
                }
                COConfigurationManager.setParameter(internal_name, value);
                success = true;
            } else if (type.equalsIgnoreCase("float")) {
                COConfigurationManager.setParameter(internal_name, Float.parseFloat(setto));
                success = true;
            } else if (type.equalsIgnoreCase("string")) {
                COConfigurationManager.setParameter(internal_name, setto);
                success = true;
            } else if (type.equalsIgnoreCase("password")) {
                SHA1Hasher hasher = new SHA1Hasher();
                byte[] password = setto.getBytes();
                byte[] encoded;
                if (password.length > 0) {
                    encoded = hasher.calculateHash(password);
                } else {
                    encoded = password;
                }
                COConfigurationManager.setParameter(internal_name, encoded);
                success = true;
            }
            if (success) {
                COConfigurationManager.save();
                ci.out.println("> Parameter '" + external_name + "' set to '" + setto + "'. [" + type + "]");
            } else
                ci.out.println("ERROR: invalid type given");
            break;
        default:
            ci.out.println("Usage: 'set \"parameter\" value type', where type = int, bool, float, string, password");
            break;
    }
}
Also used : SHA1Hasher(com.biglybt.core.util.SHA1Hasher) Iterator(java.util.Iterator) StringPattern(com.biglybt.ui.common.util.StringPattern)

Aggregations

StringPattern (com.biglybt.ui.common.util.StringPattern)2 SHA1Hasher (com.biglybt.core.util.SHA1Hasher)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Predicate (java.util.function.Predicate)1