Skip to content

Commit

Permalink
release v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
bberka committed Jun 24, 2022
1 parent 7e0a238 commit 601d1e0
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 129 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/EasMe/v17/.suo
Binary file not shown.
24 changes: 9 additions & 15 deletions EasMe/EasDel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// File or folder deletion with logging options, uses EasLog
/// </summary>
public class EasDel
public static class EasDel
{

//public static string DirCurrent = Directory.GetCurrentDirectory();//gets current directory
Expand All @@ -13,19 +13,13 @@ public class EasDel
//public static string DirSystem = Environment.GetFolderPath(Environment.SpecialFolder.System);//gets system32 directory C:\Windows\System32
//public static string DirLog = DirCurrent + "\\Logs\\"; //log file directory

public static bool _isEnableLogging = true;

//When calling the class give bool value to determine to enable or disable logging
public EasDel(bool isEnableLogging = false)
{
_isEnableLogging = isEnableLogging;
}

/// <summary>
/// Deletes file or folder, if it is folder it will delete all files and subfolders.
/// </summary>
/// <param name="FilePath"></param>
public void DeleteAllFiles(string FilePath)
/// <param name="isLog"></param>
public static void DeleteAllFiles(string FilePath, bool isLoggingEnabled = true)
{
if (Directory.Exists(FilePath))
{
Expand All @@ -36,11 +30,11 @@ public void DeleteAllFiles(string FilePath)
try
{
File.Delete(file);
if (_isEnableLogging) EasLog.Info("File deleted: " + file);
if (isLoggingEnabled) EasLog.Info("File deleted: " + file);
}
catch
{
if (_isEnableLogging) EasLog.Error("Error deleting file: " + file, Error.FAILED_TO_DELETE);
if (isLoggingEnabled) EasLog.Error("Error deleting file: " + file, Error.FAILED_TO_DELETE);
}

}
Expand All @@ -51,23 +45,23 @@ public void DeleteAllFiles(string FilePath)
try
{
Directory.Delete(FilePath);
if (_isEnableLogging) EasLog.Info("Folder deleted: " + FilePath);
if (isLoggingEnabled) EasLog.Info("Folder deleted: " + FilePath);
}
catch
{
if (_isEnableLogging) EasLog.Error("Error deleting folder:" + FilePath, Error.FAILED_TO_DELETE);
if (isLoggingEnabled) EasLog.Error("Error deleting folder:" + FilePath, Error.FAILED_TO_DELETE);
}
}
else
{
try
{
File.Delete(FilePath);
if (_isEnableLogging) EasLog.Info("File deleted: " + FilePath);
if (isLoggingEnabled) EasLog.Info("File deleted: " + FilePath);
}
catch
{
if (_isEnableLogging) EasLog.Error("Error deleting file:" + FilePath, Error.FAILED_TO_DELETE);
if (isLoggingEnabled) EasLog.Error("Error deleting file:" + FilePath, Error.FAILED_TO_DELETE);
}

}
Expand Down
27 changes: 23 additions & 4 deletions EasMe/EasGenerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
public static class EasGenerate
{
/// <summary>
/// Generate a random string with a given length and allowed characters
/// Generate a random string with a given length and characters. Max allowed length value is 1024.
/// </summary>
/// <param name="length"></param>
/// <param name="allowedChars"></param>
/// <param name="onlyLetter"></param>
/// <returns></returns>
static string GenerateString(string chars, int length)
{
if (length > 1024) throw new EasException(Error.TOO_BIG_VALUE, "Give length to create random string is too big. Max allowed length value is 1024.");
var random = new Random();
string resultToken = new(
Enumerable.Repeat(chars, length)
.Select(token => token[random.Next(token.Length)]).ToArray());

return resultToken;
}
/// <summary>
/// Generates and returns random string with a given length and allowed characters. By defualt it will allow letters and digits. Max allowed length value is 1024.
/// </summary>
/// <param name="length"></param>
/// <param name="allowedChars"></param>
/// <param name="onlyLetter"></param>
/// <returns></returns>
public static string GenerateRandomString(int length, string allowedChars = "", bool onlyLetter = false)
{
string lowerAll = "abcdefghijklmnoprstuvwxyz";
Expand All @@ -37,18 +45,29 @@ public static string GenerateRandomString(int length, string allowedChars = "",

}
}

/// <summary>
/// Generates and returns random string that only contains letters. Max allowed length value is 1024.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GenerateRandomLetters(int length)
{
string lowerAll = "abcdefghijklmnoprstuvwxyz";
string upperAll = lowerAll.ToUpper();
string allChars = lowerAll + upperAll;
return GenerateString(allChars, length);
}
public static string GenerateRandomNumbers(int digitCount)
/// <summary>
/// Generates and returns random string that only contains digits. Max allowed lengthvalue is 1024.
/// </summary>
/// <param name="digitCount"></param>
/// <returns></returns>
public static int GenerateRandomNumbers(int digitCount)
{
string digits = "0123456789";
return GenerateString(digits, digitCount);
var random = GenerateString(digits, digitCount);
var RandomStringAsInt32 = Convert.ToInt32(random);
return RandomStringAsInt32;

}
}
Expand Down
31 changes: 19 additions & 12 deletions EasMe/EasINI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,55 @@ namespace EasMe
/// <summary>
/// Write or read from INI file
/// </summary>
public class EasINI
public static class EasINI
{

private string Path;
private static string? Path;

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


public EasINI(string INIFilePath)
public static void LoadFile(string INIFilePath)
{
Path = INIFilePath;
}

public EasINI()
public static void LoadDefaultFile()
{
Path = Directory.GetCurrentDirectory() + @"\service.ini";
Path = Directory.GetCurrentDirectory() + @"\service.ini";
}

private static void CheckLoaded()
{
if (Path == null)
{
throw new EasException(Error.NOT_LOADED, "INI file path not loaded, CAll LoadFile() or LoadDefaultFile() in your application startup.");
}
}
/// <summary>
/// Writes a value to the INI file
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void Write(string Section, string Key, string Value)
public static void Write(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.Path);
CheckLoaded();
WritePrivateProfileString(Section, Key, Value, Path);
}
/// <summary>
/// Reads a value from the INI file
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string? Read(string Section, string Key)
public static string? Read(string Section, string Key)
{
StringBuilder buffer = new StringBuilder(255);
GetPrivateProfileString(Section, Key, "", buffer, 255, this.Path);
CheckLoaded();
StringBuilder buffer = new(255);
GetPrivateProfileString(Section, Key, "", buffer, 255, Path);
return Convert.ToString(buffer);
}

Expand Down
25 changes: 21 additions & 4 deletions EasMe/EasJWT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,43 @@ namespace EasMe
/// <summary>
/// JWT Authentication helper, generating and reading tokens.
/// </summary>
public class EasJWT
public static class EasJWT
{
private static string _secretString;
private static byte[] _secretBytes;
private static string _issuer;
private static string _audience;
public EasJWT(string secret, string issuer = "", string audience = "")

/// <summary>
/// Loads your secret key, issuer, audience. Call this method in your application startup.
/// </summary>
/// <param name="secret"></param>
/// <param name="issuer"></param>
/// <param name="audience"></param>
public static void LoadConfiguration(string secret, string issuer = "", string audience = "")
{
_secretString = secret;
_secretBytes = Encoding.ASCII.GetBytes(_secretString);
_issuer = issuer;
_audience = audience;
}

private static void CheckConfig()
{
if (_secretBytes.IsNullOrEmpty()) throw new EasException(Error.NULL_REFERENCE, "EasJWT configuartion error, secret not loaded.");
if (_secretString.IsNullOrEmpty()) throw new EasException(Error.NULL_REFERENCE, "EasJWT configuartion error, secret not loaded.");
if (_issuer.IsNullOrEmpty()) throw new EasException(Error.NULL_REFERENCE, "EasJWT configuartion error, issuer not loaded.");
if (_audience.IsNullOrEmpty()) throw new EasException(Error.NULL_REFERENCE, "EasJWT configuartion error, audience not loaded.");
}
/// <summary>
/// Generates a JWT token by ClaimsIdentity.
/// </summary>
/// <param name="claimsIdentity"></param>
/// <param name="expireMinutes"></param>
/// <returns></returns>
public string GenerateJWTToken(ClaimsIdentity claimsIdentity, int expireMinutes)
public static string GenerateJWTToken(ClaimsIdentity claimsIdentity, int expireMinutes)
{
CheckConfig();
try
{
var tokenHandler = new JwtSecurityTokenHandler();
Expand Down Expand Up @@ -57,8 +73,9 @@ public string GenerateJWTToken(ClaimsIdentity claimsIdentity, int expireMinutes)
/// <param name="validateIssuer"></param>
/// <param name="validateAudience"></param>
/// <returns></returns>
public ClaimsPrincipal ValidateJWTToken(string token, bool validateIssuer = false, bool validateAudience = false)
public static ClaimsPrincipal ValidateJWTToken(string token, bool validateIssuer = false, bool validateAudience = false)
{
CheckConfig();
try
{
var tokenHandler = new JwtSecurityTokenHandler();
Expand Down
2 changes: 1 addition & 1 deletion EasMe/EasLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void CheckConfig()
{
if (_config == null)
{
throw new EasException(EasMe.Error.CONFIGURATION_NOT_LOADED, "EasLogConfiguration not loaded, call LoadConfiguration() or LoadConfigurationDefault() in your application startup.");
throw new EasException(EasMe.Error.NOT_LOADED, "EasLogConfiguration not loaded, call LoadConfiguration() or LoadConfigurationDefault() in your application startup.");
}
}
/// <summary>
Expand Down
14 changes: 10 additions & 4 deletions EasMe/EasLogEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ public enum Error
//FAILED_TO_WRITE_LOG = 322,
//LOG_FILE_CONTENT_NULL = 323,
//NO_LOGS_FOUND = 324,
FAILED_TO_READ_CONFIGURATION = 325,
FAILED_TO_WRITE_CONFIGURATION = 326,
CONFIGURATION_NOT_LOADED = 327,
CONFIGURATION_IS_NOT_VALID = 328,
//FAILED_TO_READ_CONFIGURATION = 325,
//FAILED_TO_WRITE_CONFIGURATION = 326,
//CONFIGURATION_NOT_LOADED = 327,
//CONFIGURATION_IS_NOT_VALID = 328,
FAILED_TO_CONVERT = 329,
FAILED_TO_MOVE = 330,
FAILED_TO_PARSE = 331,
TOO_BIG_VALUE = 332,
TOO_SMALL_VALUE = 333,
SAME_VALUE = 334,

NOT_EXISTS = 400,
NOT_FOUND = 405,
Expand All @@ -104,6 +107,9 @@ public enum Error
EXPIRED = 421,
NOT_EXPIRED = 422,
IS_BEING_USED = 423,
NOT_LOADED = 424,
NOT_POSSIBLE = 425,
NOT_VALID = 426,

AUTHENTICATION_FAILED = 500,
AUTHORIZATION_FAILED = 501,
Expand Down
22 changes: 1 addition & 21 deletions EasMe/EasLogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,7 @@ public static class EasLogHelper
{
return Enum.GetName(typeof(Error), value);
}
/// <summary>
/// Convert an error code to a readable string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string? ConvertEnumStringToReadable(string value)
{
var result = value.Replace("_", " ").ToLower();
var firstChar = char.ToUpper(result[0]);
return firstChar + result.Substring(1) + ".";
}
/// <summary>
/// Convert an error code to a readable string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string? ConvertEnumStringToReadable(Error value)
{
var toStr = value.ToString();
return ConvertEnumStringToReadable(toStr);
}


}

Expand Down
37 changes: 14 additions & 23 deletions EasMe/EasLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,42 @@ namespace EasMe

public static class EasLogReader
{
private static string? _logFilePath;
private static string? _logFileContent;
public static string? LogFilePath { get; set; }
public static string[] LogFileContent { get; set; }

private static void CheckIfLoaded()

private static void CheckLoaded()
{
if (string.IsNullOrEmpty(_logFilePath)) throw new EasException(Error.NULL_REFERENCE, "Log file path not loaded.");
if (string.IsNullOrEmpty(_logFileContent)) throw new EasException(Error.NULL_REFERENCE, "Log file content not loaded or NULL.");
if (string.IsNullOrEmpty(LogFilePath)) throw new EasException(Error.NOT_LOADED, "Log file path not loaded.");
}
public static void Load(string logFilePath)
{
_logFilePath = logFilePath;
if (!File.Exists(_logFilePath)) throw new EasException(Error.NOT_EXISTS, "Could not locate log file with given path. => Path:" + _logFilePath);
LogFilePath = logFilePath;
if (!File.Exists(LogFilePath)) throw new EasException(Error.NOT_EXISTS, "Could not locate log file with given path. => Path:" + LogFilePath);
try
{
_logFileContent = File.ReadAllText(_logFilePath);
if (string.IsNullOrEmpty(_logFileContent)) throw new EasException(Error.NULL_REFERENCE, "Error occured while loading log file, Log file content is NULL.");
var fileContent = File.ReadAllText(LogFilePath);
string[] lines = fileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
LogFileContent = lines;
}
catch (Exception e)
{
throw new EasException(Error.FAILED_TO_READ, "Failed reading log file with given path => Path:" + _logFilePath, e);
throw new EasException(Error.FAILED_TO_READ, "Failed reading log file with given path => Path:" + LogFilePath, e);
}
}
/// <summary>
/// Gets all logs in string array.
/// </summary>
/// <returns></returns>
/// <exception cref="EasException"></exception>
public static string[] GetLogFileContentAsString()
{
if (string.IsNullOrEmpty(_logFileContent)) throw new EasException(Error.NULL_REFERENCE, "Failed getting log file content as string, Log file content is NULL");
string[] lines = _logFileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
return lines;
}

/// <summary>
/// Gets deserialized list of all logs.
/// </summary>
/// <returns</returns>
/// <exception cref="EasException"></exception>
public static List<BaseLogModel> GetLogFileContent()
{
CheckLoaded();
try
{
var list = new List<BaseLogModel>();
string[] lines = _logFileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
foreach (var line in lines)
foreach (var line in LogFileContent)
{
var deserialized = JsonConvert.DeserializeObject<BaseLogModel>(line);
if (deserialized == null) throw new EasException(Error.DESERIALIZATION_ERROR);
Expand Down
Loading

0 comments on commit 601d1e0

Please sign in to comment.