Search in sources :

Example 16 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class HadoopFile method getParent.

// ///////////////////////////////
// AbstractFile implementation //
// ///////////////////////////////
@Override
public AbstractFile getParent() {
    if (!parentValSet) {
        FileURL parentFileURL = this.fileURL.getParent();
        if (parentFileURL != null)
            parent = FileFactory.getFile(fileURL.getParent());
        parentValSet = true;
    }
    return parent;
}
Also used : FileURL(com.mucommander.commons.file.FileURL)

Example 17 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class S3File method getHadoopFileSystem.

// /////////////////////////////
// HadoopFile implementation //
// /////////////////////////////
@Override
protected FileSystem getHadoopFileSystem(FileURL url) throws IOException {
    if (!url.containsCredentials())
        throw new AuthException(url);
    // Note: getRealm returns a fresh instance every time
    FileURL realm = url.getRealm();
    // Import credentials
    Credentials creds = url.getCredentials();
    if (creds != null) {
        // URL-encode secret as it may contain non URL-safe characters ('+' and '/')
        realm.setCredentials(new Credentials(creds.getLogin(), URLEncoder.encode(creds.getPassword(), "UTF-8")));
    }
    // Change the scheme to the actual Hadoop fileystem (s3 -> s3n)
    realm.setScheme("s3n");
    return FileSystem.get(URI.create(realm.toString(true, false)), DEFAULT_CONFIGURATION);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AuthException(com.mucommander.commons.file.AuthException) Credentials(com.mucommander.commons.file.Credentials)

Example 18 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class PathUtils method resolveDestination.

/**
 * Resolves a destination path entered by the user and returns a {@link ResolvedDestination} object that
 * that contains a {@link AbstractFile} instance corresponding to the path and a type that describes the kind of
 * destination that was resolved. <code>null</code> is returned if the path is not a valid destination (see below)
 * or could not be resolved, for example becuase of I/O or authentication error.
 * <p>
 * The given path may be either absolute or relative to the specified base folder. If the base folder argument is
 * <code>null</code> and the path is relative, <code>null</code> will always be returned.
 * The path may contain '.', '..' and '~' tokens which will be left for the corresponding
 * {@link com.mucommander.commons.file.SchemeParser} to canonize.
 * </p>
 * <p>
 * The path may refer to the following listed destination types. In all cases, the destination's parent folder must
 * exist, if it doesn't <code>null</code> will always be returned. For example, <code>/non_existing_folder/file</code>
 * is not a valid destination (provided that '/non_existing_folder' does not exist).
 * <dl>
 *  <dt>{@link ResolvedDestination#EXISTING_FOLDER}</dt><dd>if the path denotes a folder, either a directory or a
 * browsable archive.</dd>
 *  <dt>{@link ResolvedDestination#EXISTING_FILE}</dt><dd>if the path denotes a regular file. The file may be a browsable archive,
 * see below.</dd>
 *  <dt>{@link ResolvedDestination#NEW_FILE}</dt><dd>if the path denotes a non-existing file whose parent exists.</dd>
 * </dl>
 * Paths to browsable archives are considered as denoting a folder only if they end with a trailing separator
 * character. If they don't, they're considered as denoting a regular file. For example,
 * <code>/existing_folder/existing_archive.zip/</code> refers to the archive as a folder where as
 * <code>/existing_folder/existing_archive.zip</code> refers to the archive as a regular file.
 * </p>
 *
 * @param destPath the destination path to resolve
 * @param baseFolder the base folder used for relative paths, <code>null</code> to accept only absolute paths
 * @param parent the parent of destPath
 * @return the object that that contains a {@link AbstractFile} instance corresponding to the path and a type that
 * describes the kind of destination that was resolved
 */
public static ResolvedDestination resolveDestination(String destPath, AbstractFile baseFolder, AbstractFile parent) {
    FileURL destURL;
    // Try to resolve the path as a URL
    try {
        destURL = FileURL.getFileURL(destPath);
    // destPath is absolute
    } catch (MalformedURLException e) {
        // Abort now if there is no base folder
        if (baseFolder == null)
            return null;
        String separator = baseFolder.getSeparator();
        // Start by cloning the base folder's URL, including credentials and properties
        FileURL baseFolderURL = baseFolder.getURL();
        destURL = (FileURL) baseFolderURL.clone();
        String basePath = destURL.getPath();
        if (!destPath.equals(""))
            destURL.setPath(basePath + (basePath.endsWith(separator) ? "" : separator) + destPath);
        // => parse the URL from scratch to have the SchemeParser canonize them.
        try {
            destURL = FileURL.getFileURL(destURL.toString(false));
            // Import credentials separately, so that login and passwords that contain URI-unsafe characters
            // such as '/' are properly parsed.
            destURL.setCredentials(baseFolderURL.getCredentials());
            destURL.importProperties(baseFolderURL);
        } catch (MalformedURLException e2) {
            return null;
        }
    }
    // No point in going any further if the URL cannot be resolved into a file
    AbstractFile destFile = FileFactory.getFile(destURL);
    if (destFile == null) {
        LOGGER.info("could not resolve a file for {}", destURL);
        return null;
    }
    // Test if the destination file exists
    boolean destFileExists = destFile.exists();
    if (destFileExists) {
        // if they don't, they'll refer to the archive as a file.
        if (destFile.isDirectory() || (destPath.endsWith(destFile.getSeparator()) && destFile.isBrowsable()))
            return new ResolvedDestination(destFile, DestinationType.EXISTING_FOLDER, destFile);
    }
    if (Objects.equals(parent, destFile.getParent())) {
        destFile.setParent(parent);
    }
    // Test if the destination's parent exists, if not the path is not a valid destination
    AbstractFile destParent = destFile.getParent();
    if (destParent == null || !destParent.exists())
        return null;
    return new ResolvedDestination(destFile, destFileExists ? DestinationType.EXISTING_FILE : DestinationType.NEW_FILE, destParent);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) MalformedURLException(java.net.MalformedURLException) AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 19 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class LocalFile method ls.

@Override
public AbstractFile[] ls(FilenameFilter filenameFilter) throws IOException {
    File[] files = file.listFiles(filenameFilter == null ? null : new LocalFilenameFilter(filenameFilter));
    if (files == null)
        throw new IOException();
    int nbFiles = files.length;
    AbstractFile[] children = new AbstractFile[nbFiles];
    for (int i = 0; i < nbFiles; i++) {
        // Clone the FileURL of this file and set the child's path, this is more efficient than creating a new
        // FileURL instance from scratch.
        FileURL childURL = (FileURL) fileURL.clone();
        childURL.setPath(absPath + SEPARATOR + files[i].getName());
        // Retrieves an AbstractFile (LocalFile or AbstractArchiveFile) instance that's potentially already in
        // the cache, reuse this file as the file's parent, and the already-created java.io.File instance.
        children[i] = FileFactory.getFile(childURL, this, Collections.singletonMap("createdFile", files[i]));
    }
    return children;
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) MonitoredFile(com.mucommander.commons.file.MonitoredFile) ProtocolFile(com.mucommander.commons.file.protocol.ProtocolFile) File(java.io.File) AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 20 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class UNCFile method ls.

@Override
public AbstractFile[] ls(FilenameFilter filenameFilter) throws IOException {
    File[] files = file.listFiles(filenameFilter == null ? null : new UNCFilenameFilter(filenameFilter));
    if (files == null)
        throw new IOException();
    int nbFiles = files.length;
    AbstractFile[] children = new AbstractFile[nbFiles];
    for (int i = 0; i < nbFiles; i++) {
        File file = files[i];
        // Clone the FileURL of this file and set the child's path, this is more efficient than creating a new
        // FileURL instance from scratch.
        FileURL childURL = (FileURL) fileURL.clone();
        childURL.setPath(addTrailingSeparator(fileURL.getPath()) + file.getName());
        // Retrieves an AbstractFile (LocalFile or AbstractArchiveFile) instance that's potentially already in
        // the cache, reuse this file as the file's parent, and the already-created java.io.File instance.
        children[i] = FileFactory.getFile(childURL, this, Collections.singletonMap("createdFile", file));
    }
    return children;
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ProtocolFile(com.mucommander.commons.file.protocol.ProtocolFile) File(java.io.File) AbstractFile(com.mucommander.commons.file.AbstractFile)

Aggregations

FileURL (com.mucommander.commons.file.FileURL)60 AbstractFile (com.mucommander.commons.file.AbstractFile)18 Credentials (com.mucommander.commons.file.Credentials)16 IOException (java.io.IOException)11 AuthException (com.mucommander.commons.file.AuthException)7 MalformedURLException (java.net.MalformedURLException)5 CredentialsMapping (com.mucommander.auth.CredentialsMapping)3 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)3 ProtocolFile (com.mucommander.commons.file.protocol.ProtocolFile)3 File (java.io.File)3 SftpException (com.jcraft.jsch.SftpException)2 AuthDialog (com.mucommander.ui.dialog.auth.AuthDialog)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DbxException (com.dropbox.core.DbxException)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 JsonReader (com.dropbox.core.json.JsonReader)1 DbxCredential (com.dropbox.core.oauth.DbxCredential)1