using Serilog; using System; using System.IO; using System.Collections.Generic; using LCU.Library.Internal.FileOperationsModels.Models; using System.Linq; namespace LCU.Api.Internal.FileOperations.Validations; public class DirectoryValidation { private const string DirectoryDoesNotExist = "Directory does not exist at path"; private const string DirectoryExist = "Directory exists at path"; private const string DirectoriesNotWithinRange = "Number of expected Directories are not within range."; public static RequestResponse ValidateDirectoryExists(string path, RequestResponse requestResponse) { bool doesDirectoryExists = false; try { doesDirectoryExists = Directory.Exists(path); } catch (Exception exception) { Log.Error(exception, $"{DirectoryDoesNotExist} {path}"); } if (!doesDirectoryExists) { requestResponse.IsSuccessful = false; requestResponse.MessageStack.Add($"{DirectoryDoesNotExist} {path}"); } return requestResponse; } public static RequestResponse ValidateDirectoryNotExists(string path, RequestResponse requestResponse) { bool directoryExists = false; try { directoryExists = !Directory.Exists(path); } catch (Exception exception) { Log.Error(exception, $"{DirectoryExist} {path}"); } if (!directoryExists) { requestResponse.IsSuccessful = false; requestResponse.MessageStack.Add($"{DirectoryExist} {path}"); } return requestResponse; } public static RequestResponse ValidateDirectoryRequestsWithinLimit(List requests, int maxAmountOfDirectories, RequestResponse requestResponse) { bool countWithRange = false; try { countWithRange = requests.Count >= 0 && requests.Count <= maxAmountOfDirectories; } catch (Exception exception) { var message = $"Unexpected issue while validate directory requests within limit."; Log.Error(exception, $"Unexpected issue while validate directory requests within limit."); requestResponse.MessageStack.Add($"{message} Exception Message: {exception.Message}"); } if (!countWithRange) { requestResponse.IsSuccessful = countWithRange; requestResponse.MessageStack.Add($"{DirectoriesNotWithinRange} Expected Range: 0-{maxAmountOfDirectories}, Actual Directories: {requests.Count} "); } return requestResponse; } public static RequestResponse ValidateDirectoryIsEmpty(string path, RequestResponse requestResponse) { bool directoryIsEmpty = false; try { directoryIsEmpty = !Directory.EnumerateFileSystemEntries(path).Any(); } catch (Exception exception) { var message = $"Unexpected issue while validate directory is empty."; Log.Error(exception, message); requestResponse.MessageStack.Add($"{message} Exception Message: {exception.Message}"); } if(!directoryIsEmpty) { requestResponse.IsSuccessful = directoryIsEmpty; requestResponse.MessageStack.Add($"{path} is not empty."); } return requestResponse; } }