Skip to content

Commit

Permalink
Merge pull request #3 from TechieGuy12/memoryusagefix
Browse files Browse the repository at this point in the history
Memory usage fix and additional functionality
  • Loading branch information
TechieGuy12 authored Aug 2, 2022
2 parents 2c7f6a7 + 0485ab5 commit f838094
Show file tree
Hide file tree
Showing 31 changed files with 1,947 additions and 495 deletions.
12 changes: 7 additions & 5 deletions FileVerification/CheckSumFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum ChecksumFileLayout
/// <summary>
/// The string representation of the hash algorithm.
/// </summary>
HASH_ALGORITHM,
HASHALGORITHM,
/// <summary>
/// The hash of the file.
/// </summary>
Expand All @@ -32,7 +32,7 @@ public class ChecksumFile
/// <summary>
/// The default checksum file name.
/// </summary>
public const string DEFAULT_CHECKSUM_FILENAME = "__fv.txt";
public const string DEFAULTCHECKSUMFILENAME = "__fv.txt";

/// <summary>
/// Gets the directory where the checksum file is located.
Expand Down Expand Up @@ -138,7 +138,7 @@ public void Read()
HashInfo info =
new HashInfo(
fileName,
values[(int)ChecksumFileLayout.HASH_ALGORITHM],
values[(int)ChecksumFileLayout.HASHALGORITHM],
values[(int)ChecksumFileLayout.HASH]);

// Get the full path to the file to use as the key to make
Expand Down Expand Up @@ -285,8 +285,10 @@ public void Write()
try
{
// Write the file hash information to the checksum file
using StreamWriter sw = new StreamWriter(FullPath);
sw.Write(string.Join("", info));
using (StreamWriter sw = new StreamWriter(FullPath))
{
sw.Write(string.Join("", info));
}
}
catch (DirectoryNotFoundException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TE.FileVerification.Net;

namespace TE.FileVerification.Configuration.Notifications
namespace TE.FileVerification.Configuration
{
/// <summary>
/// Contains the data used to send the request.
Expand All @@ -19,13 +20,13 @@ public class Data
/// Gets or sets the headers for the request.
/// </summary>
[XmlElement("headers")]
public Headers Headers { get; set; }
public Headers? Headers { get; set; }

/// <summary>
/// Gets or sets the body for the request.
/// </summary>
[XmlElement("body")]
public string Body { get; set; }
public string? Body { get; set; }

/// <summary>
/// Gets or sets the MIME type string value.
Expand All @@ -39,7 +40,7 @@ public string MimeTypeString
}
set
{
_mimeType = (value == Request.JSON_NAME || value == Request.XML_NAME) ? value : Request.JSON_NAME;
_mimeType = value == Request.JSON_NAME || value == Request.XML_NAME ? value : Request.JSON_NAME;
}
}

Expand Down
26 changes: 26 additions & 0 deletions FileVerification/Configuration/Exclusions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using TE.FileVerification.IO;

namespace TE.FileVerification.Configuration
{
/// <summary>
/// An exclusions node in the XML file.
/// </summary>
public class Exclusions : MatchBase
{
/// <summary>
/// Returns the flag indicating if the file/folder is to be ignored.
/// </summary>
/// <<param name="fullPath">
/// The full path to the file or folder.
/// </param>
/// <returns>
/// True if the file/folder is to be ignored, otherwise false.
/// </returns>
public bool Exclude(string fullPath)
{
FilterTypeName = "Exclude";
return IsMatchFound(fullPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace TE.FileVerification.Configuration.Notifications
namespace TE.FileVerification.Configuration
{
public class Header
{
[XmlElement("name")]
public string Name { get; set; }
public string? Name { get; set; }

[XmlElement("value")]
public string Value { get; set; }
public string? Value { get; set; }
}
}
44 changes: 44 additions & 0 deletions FileVerification/Configuration/Headers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace TE.FileVerification.Configuration
{
public class Headers
{
[XmlElement("header")]
public Collection<Header>? HeaderList { get; set; }

/// <summary>
/// Sets the headers for a request.
/// </summary>
/// <param name="request">
/// The request that will include the headers.
/// </param>
public void Set(HttpRequestMessage request)
{
if (request == null)
{
return;
}

if (HeaderList == null || HeaderList.Count <= 0)
{
return;
}

foreach (Header header in HeaderList)
{
if (!string.IsNullOrWhiteSpace(header.Name))
{
request.Headers.Add(header.Name, header.Value);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Text.Json;
using System.Globalization;
using TE.FileVerification.Net;

namespace TE.FileVerification.Configuration.Notifications
namespace TE.FileVerification.Configuration
{
public class Notification
{
// The message to send with the request.
private StringBuilder _message;
private readonly StringBuilder _message;

/// <summary>
/// Gets or sets the URL of the request.
/// </summary>
[XmlElement("url")]
public string Url { get; set; }
public string? Url { get; set; }

/// <summary>
/// Gets the URI value of the string URL.
/// </summary>
[XmlIgnore]
public Uri Uri
public Uri? Uri
{
get
{
Expand All @@ -46,12 +48,12 @@ public Uri Uri
}
}
}

/// <summary>
/// Gets or sets the string representation of the request method.
/// </summary>
[XmlElement("method")]
public string MethodString { get; set; }
public string MethodString { get; set; } = "Post";

/// <summary>
/// Gets the request method.
Expand All @@ -69,7 +71,7 @@ public HttpMethod Method

try
{
method = (HttpMethod)Enum.Parse(typeof(HttpMethod), MethodString.ToUpper(), true);
method = (HttpMethod)Enum.Parse(typeof(HttpMethod), MethodString.ToUpper(CultureInfo.CurrentCulture), true);
}
catch (Exception ex)
when (ex is ArgumentNullException || ex is ArgumentException || ex is OverflowException)
Expand All @@ -85,7 +87,7 @@ public HttpMethod Method
/// Gets or sets the data to send for the request.
/// </summary>
[XmlElement("data")]
public Data Data { get; set; }
public Data? Data { get; set; }

/// <summary>
/// Returns a value indicating if there is a message waiting to be sent
Expand Down Expand Up @@ -121,36 +123,44 @@ public Notification()
/// </param>
internal void QueueRequest(string message)
{
//_message.Append(CleanMessage(message) + @"\n");
_message.Append(message);
_message.Append(CleanMessage(message) + @"\n");
}

/// <summary>
/// Send the notification request.
/// </summary>
/// <exception cref="NullReferenceException">
/// <exception cref="InvalidOperationException">
/// Thrown when the URL is null or empty.
/// </exception>
internal HttpResponseMessage Send()
internal Response? Send()
{
// If there isn't a message to be sent, then just return
if (_message?.Length <= 0)
if (_message == null || _message.Length <= 0)
{
return null;
}

if (Uri == null)
if (GetUri() == null)
{
throw new InvalidOperationException("The URL is null or empty.");
}

if (Data == null)
{
throw new NullReferenceException("The URL is null or empty.");
throw new InvalidOperationException("Data for the request was not provided.");
}

string content = Data.Body.Replace("[message]", cleanForJSON(_message.ToString()));
string content = string.Empty;
if (Data.Body != null)
{
content = Data.Body.Replace("[message]", _message.ToString(), StringComparison.OrdinalIgnoreCase);
}

HttpResponseMessage response =
Response response =
Request.Send(
Method,
Uri,
Data.Headers.HeaderList,
GetUri(),
Data.Headers,
content,
Data.MimeType);

Expand All @@ -161,37 +171,46 @@ internal HttpResponseMessage Send()
/// <summary>
/// Send the notification request.
/// </summary>
/// <exception cref="NullReferenceException">
/// <exception cref="InvalidOperationException">
/// Thrown when the URL is null or empty.
/// </exception>
internal async Task<HttpResponseMessage> SendAsync()
internal async Task<Response?> SendAsync()
{
// If there isn't a message to be sent, then just return
if (_message?.Length <= 0)
if (_message == null || _message.Length <= 0)
{
return null;
}

if (Uri == null)
if (GetUri() == null)
{
throw new InvalidOperationException("The URL is null or empty.");
}

if (Data == null)
{
throw new InvalidOperationException("Data for the request was not provided.");
}

string content = string.Empty;
if (Data.Body != null)
{
throw new NullReferenceException("The URL is null or empty.");
content = Data.Body.Replace("[message]", _message.ToString(), StringComparison.OrdinalIgnoreCase);
}

string content = Data.Body.Replace("[message]", _message.ToString());

HttpResponseMessage response =
Response response =
await Request.SendAsync(
Method,
Uri,
Data.Headers.HeaderList,
GetUri(),
Data.Headers,
content,
Data.MimeType);
Data.MimeType).ConfigureAwait(false);

_message.Clear();
return response;
return response;
}

public static string cleanForJSON(string s)
public static string CleanMessage(string s)
{
if (s == null || s.Length == 0)
{
Expand All @@ -201,8 +220,8 @@ public static string cleanForJSON(string s)
char c = '\0';
int i;
int len = s.Length;
StringBuilder sb = new StringBuilder(len + 4);
String t;
StringBuilder sb = new(len + 4);
string t;

for (i = 0; i < len; i += 1)
{
Expand Down Expand Up @@ -236,8 +255,8 @@ public static string cleanForJSON(string s)
default:
if (c < ' ')
{
t = "000" + String.Format("X", c);
sb.Append("\\u" + t.Substring(t.Length - 4));
t = "000" + string.Format(CultureInfo.CurrentCulture, "{0:X}", c);
sb.Append(string.Concat("\\u", t.AsSpan(t.Length - 4)));
}
else
{
Expand All @@ -248,5 +267,22 @@ public static string cleanForJSON(string s)
}
return sb.ToString();
}

/// <summary>
/// Gets the URI value of the string URL.
/// </summary>
/// <exception cref="UriFormatException">
/// Thrown if the URL is not in a valid format.
/// </exception>
private Uri GetUri()
{
if (string.IsNullOrWhiteSpace(Url))
{
throw new UriFormatException();
}

Uri uri = new(Url);
return uri;
}
}
}
Loading

0 comments on commit f838094

Please sign in to comment.