Example #1
0
        private void DownloadAndStoreFile(ConversionStartResult conversionResult, DirectoryInfo targetFolder)
        {
            Licensing.Key = DocumentConverterSettings.Default.SftpLicenseKey;

            try
            {
                using (var client = new Sftp())
                {
                    client.Connect(conversionResult.UploadUrl, conversionResult.Port);
                    client.Login(conversionResult.User, conversionResult.Password);

                    var remotePath = $"{client.GetCurrentDirectory()}{conversionResult.ConvertedFileName}";
                    var targetPath = Path.Combine(targetFolder.FullName, $"{conversionResult.ConvertedFileName}");

                    if (!client.FileExists(remotePath))
                    {
                        Console.WriteLine($"ERROR: File {conversionResult.ConvertedFileName} not found on SFTP server.");
                    }

                    var length = client.GetFile(remotePath, targetPath);
                    Console.WriteLine($"Converted file {targetPath} and downloaded {length} bytes");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        private async Task <DownloadAndStoreFileResult> DownloadAndStoreFile(ConversionStartResult documentConversionResult,
                                                                             FileSystemInfo targetFolder)
        {
            Licensing.Key = sftpLicenseKey;

            try
            {
                using (var client = new Sftp())
                {
                    await client.ConnectAsync(documentConversionResult.UploadUrl, documentConversionResult.Port);

                    await client.LoginAsync(documentConversionResult.User, documentConversionResult.Password);

                    var remotePath = $"{client.GetCurrentDirectory()}{documentConversionResult.ConvertedFileName}";
                    var targetPath = Path.Combine(targetFolder.FullName, $"{documentConversionResult.ConvertedFileName}");

                    if (!await client.FileExistsAsync(remotePath))
                    {
                        throw new FileNotFoundException($"File '{documentConversionResult.ConvertedFileName}' not found on SFTP server");
                    }

                    var lengthOfContent = await client.GetFileAsync(remotePath, targetPath);

                    if (!File.Exists(targetPath))
                    {
                        throw new InvalidOperationException($"Was unable to download file  {targetPath} from sftp server");
                    }

                    return(new DownloadAndStoreFileResult {
                        LengthOfContent = lengthOfContent, TargetPath = targetPath
                    });
                }
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                throw;
            }
        }