Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove retry rule in FtpClient.cs #212

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 49 additions & 80 deletions Src/CrispyWaffle.Utils/Communications/FtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public FtpClient(IConnection ftp, string remoteDirectory)
ftp?.Credentials.UserName,
ftp?.Credentials.Password,
remoteDirectory
) { }
)
{ }

/// <summary>
/// Initializes a new instance of the <see cref="FtpClient" /> class.
Expand Down Expand Up @@ -133,58 +134,37 @@ string remoteDirectory
/// <exception cref="System.InvalidOperationException">Response stream is null</exception>
private bool ExistsInternal(string path)
{
var result = false;
Stream responseStream = null;

try
{
LogConsumer.Info(
"Checking in FtpClient the path/file: {0}",
path.GetPathOrFileName()
);
LogConsumer.Info("Checking in FtpClient the path/file: {0}", path.GetPathOrFileName());
var uri = new Uri(path);
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(_userName, _password);
request.Method = !string.IsNullOrWhiteSpace(uri.GetFileExtension())
? WebRequestMethods.Ftp.GetFileSize
: WebRequestMethods.Ftp.ListDirectory;
request.Method = string.IsNullOrWhiteSpace(uri.GetFileExtension())
? WebRequestMethods.Ftp.ListDirectory
: WebRequestMethods.Ftp.GetFileSize;
request.Timeout = 30000;
request.ReadWriteTimeout = 90000;
request.UsePassive = true;
var response = (FtpWebResponse)request.GetResponse();
var status = response.StatusCode;
responseStream = response.GetResponseStream();
if (responseStream == null)
{
throw new InvalidOperationException("Response stream is null");
}

using var reader = new StreamReader(responseStream);
responseStream = null;
while (!reader.EndOfStream)
using var response = (FtpWebResponse)request.GetResponse();
using (var responseStream = response.GetResponseStream())
{
_files.Enqueue(reader.ReadLine());
using var reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{
_files.Enqueue(reader.ReadLine());
}
}

if (
!string.IsNullOrWhiteSpace(uri.GetFileExtension())
&& status == FtpStatusCode.FileStatus
|| status == FtpStatusCode.OpeningData
)
{
result = true;
}
return string.IsNullOrWhiteSpace(uri.GetFileExtension())
? response.StatusCode == FtpStatusCode.OpeningData
: response.StatusCode == FtpStatusCode.FileStatus || response.StatusCode == FtpStatusCode.OpeningData;
}
catch (WebException)
{
result = false;
return false;
}
finally
{
responseStream?.Dispose();
}

return result;
}

/// <summary>
Expand All @@ -197,55 +177,44 @@ private bool ExistsInternal(string path)
private bool CreateInternal(string path, byte[] bytes)
{
var result = false;
var attempts = 0;
while (true)
try
{
attempts++;
try
LogConsumer.Info(
"Uploading to FtpClient the file: {0}",
path.GetPathOrFileName()
);
var uri = new Uri(path);
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(_userName, _password);
request.UsePassive = true;
if (!string.IsNullOrWhiteSpace(uri.GetFileExtension()))
{
LogConsumer.Info(
"Uploading to FtpClient the file: {0}",
path.GetPathOrFileName()
);
var uri = new Uri(path);
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(_userName, _password);
request.UsePassive = true;
if (!string.IsNullOrWhiteSpace(uri.GetFileExtension()))
{
request.Method = WebRequestMethods.Ftp.UploadFile;
request.ContentLength = bytes.Length;
var stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
else
{
request.Method = WebRequestMethods.Ftp.MakeDirectory;
}

var response = (FtpWebResponse)request.GetResponse();
if (
!string.IsNullOrWhiteSpace(uri.GetFileExtension())
&& response.StatusCode == FtpStatusCode.ClosingData
|| response.StatusCode == FtpStatusCode.PathnameCreated
)
{
result = true;
}

response.Close();
break;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.ContentLength = bytes.Length;
var stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
catch (WebException e)
else
{
if (attempts >= 3)
{
throw new FtpClientException(path.GetPathOrFileName(), "create", e);
}
request.Method = WebRequestMethods.Ftp.MakeDirectory;
}

Thread.Sleep(1000);
var response = (FtpWebResponse)request.GetResponse();
if (
!string.IsNullOrWhiteSpace(uri.GetFileExtension())
&& response.StatusCode == FtpStatusCode.ClosingData
|| response.StatusCode == FtpStatusCode.PathnameCreated
)
{
result = true;
}

response.Close();
}
catch (WebException e)
{
throw new FtpClientException(path.GetPathOrFileName(), "create", e);
}

return result;
Expand Down
Loading