Search in sources :

Example 1 with SshClientTemplate

use of com.amaze.filemanager.filesystem.ssh.SshClientTemplate 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

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