129 lines
4.8 KiB
C#
129 lines
4.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using PartSource.Automation.Models.Configuration;
|
|
using PartSource.Automation.Models.Ftp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PartSource.Automation.Services
|
|
{
|
|
public class FtpService
|
|
{
|
|
private readonly FtpConfiguration _ftpConfiguration;
|
|
|
|
public FtpService(FtpConfiguration ftpConfiguration)
|
|
{
|
|
_ftpConfiguration = ftpConfiguration;
|
|
}
|
|
|
|
public IList<FtpFileInfo> ListFilesExtended(string directory = "")
|
|
{
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
|
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
|
|
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
|
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
|
|
|
IList<FtpFileInfo> files = new List<FtpFileInfo>();
|
|
string[] fileStrings = reader.ReadToEnd().Split("\r\n");
|
|
|
|
foreach (string fileString in fileStrings)
|
|
{
|
|
if (string.IsNullOrEmpty(fileString))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string dateString = fileString[..17];
|
|
string[] sizeAndName = fileString[18..].TrimStart().Split(" ", 2);
|
|
|
|
if (sizeAndName[0].ToUpperInvariant().IndexOf("DIR") > -1)
|
|
{
|
|
files.Add(new FtpFileInfo
|
|
{
|
|
Modified = DateTime.Parse(fileString[..17]),
|
|
Size = 0,
|
|
Filename = sizeAndName[1].Trim(),
|
|
FileType = FtpFileType.Directory
|
|
});
|
|
}
|
|
|
|
else
|
|
{
|
|
files.Add(new FtpFileInfo
|
|
{
|
|
Modified = DateTime.Parse(fileString[..17]),
|
|
Size = long.Parse(sizeAndName[0]),
|
|
Filename = sizeAndName[1].Trim(),
|
|
FileType = FtpFileType.File
|
|
});
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
public string[] ListFiles(string directory = "")
|
|
{
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
|
request.Method = WebRequestMethods.Ftp.ListDirectory;
|
|
|
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
|
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
|
|
|
string files = reader.ReadToEnd();
|
|
|
|
return files.Length > 0
|
|
? files.Split("\r\n")
|
|
: Array.Empty<string>();
|
|
}
|
|
|
|
public string Download(string filename, string destination = null)
|
|
{
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
|
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
|
|
|
if (string.IsNullOrEmpty(destination))
|
|
{
|
|
destination = _ftpConfiguration.Destination;
|
|
}
|
|
|
|
if (Path.DirectorySeparatorChar == '\\')
|
|
{
|
|
filename = filename.Replace("/", "\\");
|
|
}
|
|
|
|
destination = Path.Combine(destination, filename);
|
|
string destinationDirectory = Path.GetDirectoryName(destination);
|
|
if (!Directory.Exists(destinationDirectory))
|
|
{
|
|
Directory.CreateDirectory(destinationDirectory);
|
|
}
|
|
|
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
|
using Stream responseStream = response.GetResponseStream();
|
|
using FileStream fileStream = new FileStream(destination, FileMode.Create);
|
|
|
|
responseStream.CopyTo(fileStream);
|
|
|
|
return destination;
|
|
}
|
|
|
|
public void Delete(string filename)
|
|
{
|
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
|
request.Method = WebRequestMethods.Ftp.DeleteFile;
|
|
|
|
|
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
|
}
|
|
}
|
|
}
|