public async Task SyncDirectory(SiteSetting site, string siteKey) { _logger.LogInformation($"Starting sync {siteKey}"); try { _client.Host = site.FtpServer; _client.Credentials = new System.Net.NetworkCredential(site.UserName, site.Password); await _client.ConnectAsync(); await _client.DownloadFolderRecursive(site.RootFolder[0], site.DestinationFolder); _logger.LogInformation($"Finish sync {siteKey}"); } catch (FtpException exc) { _logger.LogError(exc, $"Error processing site {siteKey}"); } finally { if (_client.IsConnected) { await _client.DisconnectAsync(); } } }
public static async Task DownloadFolderRecursive(this IFtpClient client, string source, string destination) { var entries = await client.GetListingAsync(source); foreach (var item in entries.Where(x => x.Type == FtpFileSystemObjectType.File)) { try { var downloadFile = true; var file = $"{destination}\\{item.Name}"; if (File.Exists(file)) { var fileInfo = new FileInfo(file); downloadFile = fileInfo.LastWriteTimeUtc < item.Modified; } if (!downloadFile) { continue; } var result = await client.DownloadFileAsync(file, item.FullName); if (!result) { throw new FtpException($"Error downloading file {item.FullName}"); } } catch (FtpException exc) { if (exc.InnerException is FtpCommandException ftpCommandException) { throw new FtpException($"Error downloading file {item.FullName}. Response type {ftpCommandException.ResponseType}", ftpCommandException); } throw new FtpException($"Error downloading file {item.FullName}", exc); } } foreach (var item in entries.Where(x => x.Type == FtpFileSystemObjectType.Directory)) { var newDestination = $@"{destination}\{item.Name}"; if (!Directory.Exists(newDestination)) { Directory.CreateDirectory(newDestination); } await client.DownloadFolderRecursive($"{source}/{item.Name}", newDestination); } }