Example #1
0
        public async Task <bool> WriteImageSourceAsByteArraySFTP(ImageSource imageSource, string imageLocationMemory)
        {
            var taskResult = await Task.Run(() =>
            {
                BitmapEncoder encoder = new TiffBitmapEncoder();
                byte[] biteArray      = ImageSourceToBytes(encoder, imageSource); // Function returns byte[] csv file

                using (var client = new Renci.SshNet.SftpClient(Host, Port, Username, Password))
                {
                    client.Connect();
                    if (client.IsConnected)
                    {
                        client.ChangeDirectory(SFTPWorkingDirectory);
                        using (var ms = new MemoryStream(biteArray))
                        {
                            client.BufferSize = (uint)ms.Length;                                               // bypass Payload error large files
                            client.Create(SFTPWorkingDirectory + "/" + imageLocationMemory);
                            client.WriteAllBytes(SFTPWorkingDirectory + "/" + imageLocationMemory, biteArray); // imageLocationDisk == openFileDialog.FileName
                            return(true);
                        }
                    }
                    else
                    {
                        OutputMessage = "Couldn't connect to SFTP server.";
                        return(false);
                    }
                }
            });

            return(taskResult);
        }
        public bool CopyLocalToRemote(Dictionary <string, byte[]> files, ILog log)
        {
            var isSuccess = true;

            // upload new files as destination user

            var connectionInfo = SshClient.GetConnectionInfo(_config);

            using (var sftp = new Renci.SshNet.SftpClient(connectionInfo))
            {
                try
                {
                    sftp.Connect();

                    foreach (var dest in files)
                    {
                        try
                        {
                            sftp.WriteAllBytes(dest.Key, dest.Value);
                        }
                        catch (SftpPathNotFoundException exp)
                        {
                            // path not found, folder is probably wrong
                            log?.Error($"SftpClient :: Failed to copy file. Check that the full path to {dest} is valid and that 'sudo' is not required to perform file copy. {exp}");

                            // failed to copy the file. TODO: retries
                            isSuccess = false;
                            break;
                        }
                        catch (Exception exp)
                        {
                            log?.Error($"SftpClient :: Failed to perform CopyLocalToRemote [{connectionInfo.Host}:{connectionInfo.Port}]: {exp}");

                            // failed to copy the file. TODO: retries
                            isSuccess = false;
                            break;
                        }
                    }
                    sftp.Disconnect();
                }
                catch (Exception exp)
                {
                    isSuccess = false;
                    log?.Error($"SftpClient :: Failed to perform CopyLocalToRemote [{connectionInfo.Host}:{connectionInfo.Port}]: {exp}");
                }
            }

            return(isSuccess);
        }