-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved helper classes to helper namespace
- Loading branch information
Showing
4 changed files
with
185 additions
and
187 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Security; | ||
using System.Net.Sockets; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Downloader.Extensions.Helpers; | ||
|
||
internal static class ExceptionHelper | ||
{ | ||
internal static bool IsMomentumError(this Exception error) | ||
{ | ||
if (error.HasSource("System.Net.Http", | ||
"System.Net.Sockets", | ||
"System.Net.Security")) | ||
return true; | ||
|
||
if (error.HasTypeOf(typeof(WebException), typeof(SocketException))) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
internal static bool HasTypeOf(this Exception exp, params Type[] types) | ||
{ | ||
Exception innerException = exp; | ||
while (innerException != null) | ||
{ | ||
foreach (Type type in types) | ||
{ | ||
if (innerException.GetType() == type) | ||
return true; | ||
} | ||
|
||
innerException = innerException.InnerException; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
internal static bool HasSource(this Exception exp, params string[] sources) | ||
{ | ||
Exception innerException = exp; | ||
while (innerException != null) | ||
{ | ||
foreach (string source in sources) | ||
{ | ||
if (string.Equals(innerException.Source, source, StringComparison.OrdinalIgnoreCase)) | ||
return true; | ||
} | ||
|
||
innerException = innerException.InnerException; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Sometime a server get certificate validation error | ||
/// https://stackoverflow.com/questions/777607/the-remote-certificate-is-invalid-according-to-the-validation-procedure-using | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="certificate"></param> | ||
/// <param name="chain"></param> | ||
/// <param name="sslPolicyErrors"></param> | ||
internal static bool CertificateValidationCallBack(object sender, | ||
X509Certificate certificate, | ||
X509Chain chain, SslPolicyErrors sslPolicyErrors) | ||
{ | ||
// If the certificate is a valid, signed certificate, return true. | ||
if (sslPolicyErrors == SslPolicyErrors.None) | ||
return true; | ||
|
||
// If there are errors in the certificate chain, look at each error to determine the cause. | ||
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0) | ||
{ | ||
if (chain?.ChainStatus != null) | ||
{ | ||
foreach (X509ChainStatus status in chain.ChainStatus) | ||
{ | ||
if (status.Status == X509ChainStatusFlags.NotTimeValid) | ||
{ | ||
// If the error is for certificate expiration then it can be continued | ||
return true; | ||
} | ||
else if (certificate.Subject == certificate.Issuer && | ||
status.Status == X509ChainStatusFlags.UntrustedRoot) | ||
{ | ||
// Self-signed certificates with an untrusted root are valid. | ||
continue; | ||
} | ||
else if (status.Status != X509ChainStatusFlags.NoError) | ||
{ | ||
// If there are any other errors in the certificate chain, the certificate is invalid, | ||
// so the method returns false. | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// When processing reaches this line, the only errors in the certificate chain are | ||
// untrusted root errors for self-signed certificates. These certificates are valid | ||
// for default Exchange server installations, so return true. | ||
return true; | ||
} | ||
else | ||
{ | ||
// In all other cases, return false. | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Downloader.Extensions.Helpers; | ||
|
||
internal static class FileHelper | ||
{ | ||
public static Stream CreateFile(string filename) | ||
{ | ||
string directory = Path.GetDirectoryName(filename); | ||
if (string.IsNullOrWhiteSpace(directory)) | ||
{ | ||
return Stream.Null; | ||
} | ||
|
||
if (Directory.Exists(directory) == false) | ||
{ | ||
Directory.CreateDirectory(directory); | ||
} | ||
|
||
return new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); | ||
} | ||
public static string GetTempFile() | ||
{ | ||
return GetTempFile(Path.GetTempPath(), string.Empty); | ||
} | ||
public static string GetTempFile(string baseDirectory, string fileExtension) | ||
{ | ||
if (string.IsNullOrWhiteSpace(baseDirectory)) | ||
{ | ||
baseDirectory = Path.GetTempPath(); | ||
} | ||
|
||
string filename = Path.Combine(baseDirectory, Guid.NewGuid().ToString("N") + fileExtension); | ||
CreateFile(filename).Dispose(); | ||
|
||
return filename; | ||
} | ||
|
||
public static long GetAvailableFreeSpaceOnDisk(string directory) | ||
{ | ||
try | ||
{ | ||
var drive = new DriveInfo(directory); | ||
if (drive.IsReady) | ||
{ | ||
return drive.AvailableFreeSpace; | ||
} | ||
|
||
return 0L; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
// null or use UNC (\\server\share) paths not supported. | ||
return 0L; | ||
} | ||
} | ||
|
||
public static void ThrowIfNotEnoughSpace(long actualNeededSize, params string[] directories) | ||
{ | ||
if (directories != null) | ||
{ | ||
foreach (string directory in directories) | ||
{ | ||
var availableFreeSpace = GetAvailableFreeSpaceOnDisk(directory); | ||
if (availableFreeSpace > 0 && availableFreeSpace < actualNeededSize) | ||
{ | ||
throw new IOException($"There is not enough space on the disk `{directory}` with {availableFreeSpace} bytes"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.