Search in sources :

Example 1 with RemoteFile

use of net.schmizz.sshj.sftp.RemoteFile 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 2 with RemoteFile

use of net.schmizz.sshj.sftp.RemoteFile in project AmazeFileManager by TeamAmaze.

the class HybridFile method getInputStream.

public InputStream getInputStream(Context context) {
    InputStream inputStream;
    switch(mode) {
        case SFTP:
            inputStream = SshClientUtils.execute(new SFtpClientTemplate(path, false) {

                @Override
                public InputStream execute(final 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();
                                client.close();
                            }
                        }
                    };
                }
            });
            break;
        case SMB:
            try {
                inputStream = new SmbFile(path).getInputStream();
            } catch (IOException e) {
                inputStream = null;
                e.printStackTrace();
            }
            break;
        case OTG:
            ContentResolver contentResolver = context.getContentResolver();
            DocumentFile documentSourceFile = OTGUtil.getDocumentFile(path, context, false);
            try {
                inputStream = contentResolver.openInputStream(documentSourceFile.getUri());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                inputStream = null;
            }
            break;
        case DROPBOX:
            CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
            Log.d(getClass().getSimpleName(), CloudUtil.stripPath(OpenMode.DROPBOX, path));
            inputStream = cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, path));
            break;
        case BOX:
            CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
            inputStream = cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, path));
            break;
        case GDRIVE:
            CloudStorage cloudStorageGDrive = dataUtils.getAccount(OpenMode.GDRIVE);
            inputStream = cloudStorageGDrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, path));
            break;
        case ONEDRIVE:
            CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
            inputStream = cloudStorageOneDrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
            break;
        default:
            try {
                inputStream = new FileInputStream(path);
            } catch (FileNotFoundException e) {
                inputStream = null;
                e.printStackTrace();
            }
            break;
    }
    return inputStream;
}
Also used : CloudStorage(com.cloudrail.si.interfaces.CloudStorage) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) DocumentFile(android.support.v4.provider.DocumentFile) 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) ContentResolver(android.content.ContentResolver)

Example 3 with RemoteFile

use of net.schmizz.sshj.sftp.RemoteFile in project AmazeFileManager by TeamAmaze.

the class HybridFile method getOutputStream.

public OutputStream getOutputStream(Context context) {
    OutputStream outputStream;
    switch(mode) {
        case SFTP:
            return SshClientUtils.execute(new SshClientTemplate(path, false) {

                @Override
                public OutputStream execute(final SSHClient ssh) throws IOException {
                    final SFTPClient client = ssh.newSFTPClient();
                    final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path), EnumSet.of(net.schmizz.sshj.sftp.OpenMode.WRITE, net.schmizz.sshj.sftp.OpenMode.CREAT));
                    return rf.new RemoteFileOutputStream() {

                        @Override
                        public void close() throws IOException {
                            try {
                                super.close();
                            } finally {
                                rf.close();
                                client.close();
                            }
                        }
                    };
                }
            });
        case SMB:
            try {
                outputStream = new SmbFile(path).getOutputStream();
            } catch (IOException e) {
                outputStream = null;
                e.printStackTrace();
            }
            break;
        case OTG:
            ContentResolver contentResolver = context.getContentResolver();
            DocumentFile documentSourceFile = OTGUtil.getDocumentFile(path, context, true);
            try {
                outputStream = contentResolver.openOutputStream(documentSourceFile.getUri());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                outputStream = null;
            }
            break;
        default:
            try {
                outputStream = FileUtil.getOutputStream(new File(path), context);
            } catch (Exception e) {
                outputStream = null;
                e.printStackTrace();
            }
    }
    return outputStream;
}
Also used : SshClientTemplate(com.amaze.filemanager.filesystem.ssh.SshClientTemplate) DocumentFile(android.support.v4.provider.DocumentFile) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) 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) ContentResolver(android.content.ContentResolver) SSHClient(net.schmizz.sshj.SSHClient) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile) RemoteFile(net.schmizz.sshj.sftp.RemoteFile)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 SmbFile (jcifs.smb.SmbFile)3 RemoteFile (net.schmizz.sshj.sftp.RemoteFile)3 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)3 ContentResolver (android.content.ContentResolver)2 DocumentFile (android.support.v4.provider.DocumentFile)2 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)1 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)1 SshClientTemplate (com.amaze.filemanager.filesystem.ssh.SshClientTemplate)1 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 SmbException (jcifs.smb.SmbException)1 SSHClient (net.schmizz.sshj.SSHClient)1 SFTPException (net.schmizz.sshj.sftp.SFTPException)1