Search in sources :

Example 1 with SFtpClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.

the class HybridFile method forEachChildrenFile.

/**
 * Helper method to list children of this file
 */
public void forEachChildrenFile(Context context, boolean isRoot, OnFileFound onFileFound) {
    switch(mode) {
        case SFTP:
            try {
                SshClientUtils.execute(new SFtpClientTemplate(path) {

                    @Override
                    public Void execute(SFTPClient client) throws IOException {
                        try {
                            for (RemoteResourceInfo info : client.ls(SshClientUtils.extractRemotePathFrom(path))) {
                                HybridFileParcelable f = new HybridFileParcelable(String.format("%s/%s", path, info.getName()));
                                f.setName(info.getName());
                                f.setMode(OpenMode.SFTP);
                                f.setDirectory(info.isDirectory());
                                f.setDate(info.getAttributes().getMtime() * 1000);
                                f.setSize(f.isDirectory() ? 0 : info.getAttributes().getSize());
                                f.setPermission(Integer.toString(FilePermission.toMask(info.getAttributes().getPermissions()), 8));
                                onFileFound.onFileFound(f);
                            }
                        } catch (IOException e) {
                            Log.w("DEBUG.listFiles", "IOException", e);
                        }
                        return null;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case SMB:
            try {
                SmbFile smbFile = new SmbFile(path);
                for (SmbFile smbFile1 : smbFile.listFiles()) {
                    HybridFileParcelable baseFile = new HybridFileParcelable(smbFile1.getPath());
                    baseFile.setName(smbFile1.getName());
                    baseFile.setMode(OpenMode.SMB);
                    baseFile.setDirectory(smbFile1.isDirectory());
                    baseFile.setDate(smbFile1.lastModified());
                    baseFile.setSize(baseFile.isDirectory() ? 0 : smbFile1.length());
                    onFileFound.onFileFound(baseFile);
                }
            } catch (MalformedURLException | SmbException e) {
                e.printStackTrace();
            }
            break;
        case OTG:
            OTGUtil.getDocumentFiles(path, context, onFileFound);
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            try {
                CloudUtil.getCloudFiles(path, dataUtils.getAccount(mode), mode, onFileFound);
            } catch (CloudPluginException e) {
                e.printStackTrace();
            }
            break;
        default:
            RootHelper.getFiles(path, isRoot, true, null, onFileFound);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteResourceInfo(net.schmizz.sshj.sftp.RemoteResourceInfo) IOException(java.io.IOException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) SmbException(jcifs.smb.SmbException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException)

Example 2 with SFtpClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.

the class HybridFile method delete.

public boolean delete(Context context, boolean rootmode) throws ShellNotRunningException {
    if (isSftp()) {
        SshClientUtils.execute(new SFtpClientTemplate(path) {

            @Override
            public Void execute(SFTPClient client) throws IOException {
                if (isDirectory(AppConfig.getInstance()))
                    client.rmdir(SshClientUtils.extractRemotePathFrom(path));
                else
                    client.rm(SshClientUtils.extractRemotePathFrom(path));
                return null;
            }
        });
        return true;
    } else if (isSmb()) {
        try {
            new SmbFile(path).delete();
        } catch (SmbException | MalformedURLException e) {
            e.printStackTrace();
        }
    } else {
        if (isRoot() && rootmode) {
            setMode(OpenMode.ROOT);
            RootUtils.delete(getPath());
        } else {
            FileUtil.deleteFile(new File(path), context);
        }
    }
    return !exists();
}
Also used : SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) IOException(java.io.IOException) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile) SmbFile(jcifs.smb.SmbFile)

Example 3 with SFtpClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.

the class HybridFile method getUsableSpace.

/**
 * Gets usable i.e. free space of a device
 * @return
 */
public long getUsableSpace() {
    long size = 0L;
    switch(mode) {
        case SMB:
            try {
                size = (new SmbFile(path).getDiskFreeSpace());
            } catch (MalformedURLException e) {
                size = 0L;
                e.printStackTrace();
            } catch (SmbException e) {
                size = 0L;
                e.printStackTrace();
            }
            break;
        case FILE:
        case ROOT:
            size = new File(path).getUsableSpace();
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
            size = spaceAllocation.getTotal() - spaceAllocation.getUsed();
            break;
        case SFTP:
            size = SshClientUtils.execute(new SFtpClientTemplate(path) {

                @Override
                public Long execute(@NonNull SFTPClient client) throws IOException {
                    try {
                        Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
                        return response.diskFreeSpace();
                    } catch (SFTPException e) {
                        Log.e(TAG, "Error querying server", e);
                        return 0L;
                    } catch (Buffer.BufferException e) {
                        Log.e(TAG, "Error parsing reply", e);
                        return 0L;
                    }
                }
            });
            break;
        case OTG:
            // TODO: Get free space from OTG when {@link DocumentFile} API adds support
            break;
    }
    return size;
}
Also used : Buffer(net.schmizz.sshj.common.Buffer) MalformedURLException(java.net.MalformedURLException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Statvfs(com.amaze.filemanager.filesystem.ssh.Statvfs) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) NonNull(android.support.annotation.NonNull) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 4 with SFtpClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.

the class HybridFile method getInputStream.

/**
 * Handles getting input stream for various {@link OpenMode}
 * @deprecated use {@link #getInputStream(Context)} which allows handling content resolver
 * @return
 */
public InputStream getInputStream() {
    InputStream inputStream;
    if (isSftp()) {
        return SshClientUtils.execute(new SFtpClientTemplate(path) {

            @Override
            public InputStream execute(SFTPClient client) throws IOException {
                final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path));
                return rf.new RemoteFileInputStream() {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            rf.close();
                        }
                    }
                };
            }
        });
    } else if (isSmb()) {
        try {
            inputStream = new SmbFile(path).getInputStream();
        } catch (IOException e) {
            inputStream = null;
            e.printStackTrace();
        }
    } else {
        try {
            inputStream = new FileInputStream(path);
        } catch (FileNotFoundException e) {
            inputStream = null;
            e.printStackTrace();
        }
    }
    return inputStream;
}
Also used : SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) IOException(java.io.IOException) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) FileInputStream(java.io.FileInputStream) SmbFile(jcifs.smb.SmbFile)

Example 5 with SFtpClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate in project AmazeFileManager by TeamAmaze.

the class HybridFile method listFiles.

/**
 * Helper method to list children of this file
 * @deprecated use forEachChildrenFile()
 */
public ArrayList<HybridFileParcelable> listFiles(Context context, boolean isRoot) {
    ArrayList<HybridFileParcelable> arrayList = new ArrayList<>();
    switch(mode) {
        case SFTP:
            try {
                arrayList = SshClientUtils.execute(new SFtpClientTemplate(path) {

                    @Override
                    public ArrayList<HybridFileParcelable> execute(SFTPClient client) throws IOException {
                        ArrayList<HybridFileParcelable> retval = new ArrayList<HybridFileParcelable>();
                        try {
                            for (RemoteResourceInfo info : client.ls(SshClientUtils.extractRemotePathFrom(path))) {
                                HybridFileParcelable f = new HybridFileParcelable(String.format("%s/%s", path, info.getName()));
                                f.setName(info.getName());
                                f.setMode(OpenMode.SFTP);
                                f.setDirectory(info.isDirectory());
                                f.setDate(info.getAttributes().getMtime() * 1000);
                                f.setSize(f.isDirectory() ? 0 : info.getAttributes().getSize());
                                f.setPermission(Integer.toString(FilePermission.toMask(info.getAttributes().getPermissions()), 8));
                                retval.add(f);
                            }
                        } catch (IOException e) {
                            Log.w("DEBUG.listFiles", "IOException", e);
                        }
                        return retval;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                arrayList.clear();
            }
            break;
        case SMB:
            try {
                SmbFile smbFile = new SmbFile(path);
                for (SmbFile smbFile1 : smbFile.listFiles()) {
                    HybridFileParcelable baseFile = new HybridFileParcelable(smbFile1.getPath());
                    baseFile.setName(smbFile1.getName());
                    baseFile.setMode(OpenMode.SMB);
                    baseFile.setDirectory(smbFile1.isDirectory());
                    baseFile.setDate(smbFile1.lastModified());
                    baseFile.setSize(baseFile.isDirectory() ? 0 : smbFile1.length());
                    arrayList.add(baseFile);
                }
            } catch (MalformedURLException e) {
                if (arrayList != null)
                    arrayList.clear();
                e.printStackTrace();
            } catch (SmbException e) {
                if (arrayList != null)
                    arrayList.clear();
                e.printStackTrace();
            }
            break;
        case OTG:
            arrayList = OTGUtil.getDocumentFilesList(path, context);
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            try {
                arrayList = CloudUtil.listFiles(path, dataUtils.getAccount(mode), mode);
            } catch (CloudPluginException e) {
                e.printStackTrace();
                arrayList = new ArrayList<>();
            }
            break;
        default:
            arrayList = RootHelper.getFilesList(path, isRoot, true, null);
    }
    return arrayList;
}
Also used : SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteResourceInfo(net.schmizz.sshj.sftp.RemoteResourceInfo) IOException(java.io.IOException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) SmbException(jcifs.smb.SmbException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile)

Aggregations

SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)9 SmbFile (jcifs.smb.SmbFile)9 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)9 DocumentFile (android.support.v4.provider.DocumentFile)6 IOException (java.io.IOException)6 SmbException (jcifs.smb.SmbException)6 RemoteFile (net.schmizz.sshj.sftp.RemoteFile)6 File (java.io.File)5 MalformedURLException (java.net.MalformedURLException)5 SFTPException (net.schmizz.sshj.sftp.SFTPException)5 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)4 FileNotFoundException (java.io.FileNotFoundException)4 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)3 NonNull (android.support.annotation.NonNull)2 CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)2 Statvfs (com.amaze.filemanager.filesystem.ssh.Statvfs)2 SpaceAllocation (com.cloudrail.si.types.SpaceAllocation)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Buffer (net.schmizz.sshj.common.Buffer)2