public async Task <IReadOnlyCollection <FileRef> > List() { await _EnsureConnection(); var items = await _ftpClient.GetListingAsync($"/{PathHelper.Combine(_options.Path)}"); return(items .Where(i => i.Type == FtpFileSystemObjectType.File) .Select(i => new FileRef(i.Name)) .ToArray()); }
protected virtual async Task <ImportVehiclesForSiteResult> InternalImportForSiteAsync(Site site, IFtpClient ftpClient) { ImportVehiclesForSiteResult importResult; try { if (site.ImportRelativeFtpPath != null) { if (ftpClient.DirectoryExists(site.ImportRelativeFtpPath)) { FtpListItem lastModifiedFileInfo = (await ftpClient.GetListingAsync(site.ImportRelativeFtpPath, FtpListOption.Auto)) .Where(r => r.Type == FtpFileSystemObjectType.File) .OrderByDescending(r => r.Modified) .FirstOrDefault(); if (lastModifiedFileInfo != null) { IEnumerable <Vehicle> vehicles; using (var csvFileStream = new MemoryStream(await ftpClient.DownloadAsync(lastModifiedFileInfo.FullName))) { vehicles = VehicleBulkFactory.Create(new VehicleFromCsvFileBulkFactorySettings(site.Id, csvFileStream)); } await VehicleRepository.RefreshEntitiesForSiteAsync(site.Id, vehicles); importResult = new ImportVehiclesForSiteResult( site.Id, site.Name, vehicles.Count(), vehicles.Count(r => r.Condition == VehicleConditions.New), vehicles.Count(r => r.Condition == VehicleConditions.Used)); } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"No files in specified folder ({site.ImportRelativeFtpPath}).", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder ({site.ImportRelativeFtpPath}) was not found.", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder is not defined.", ImportStatusEnum.NotStarted); } } catch (Exception ex) { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, ex.Message, ImportStatusEnum.Failed); } return(importResult); }
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); } }
/// <summary> /// Leitura e retorno dos arquivos contidos no caminho remoto específicado do servidor FTP /// </summary> /// <param name="caminho">Caminho no servidor FTP para a leitura</param> /// <returns> Lista com os nomes dos arquivos </returns> public async Task <List <string> > RetornaArquivos(string caminho) { try { List <string> arquivos = new List <string>(); FtpListItem[] listagem = await _cliente.GetListingAsync(); foreach (var item in listagem) { if (item.Type == FtpFileSystemObjectType.File) { arquivos.Add(item.FullName); } } return(arquivos); } catch (Exception ex) { throw ex; } }