223 lines
6.9 KiB
C#
223 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Serilog;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
|
|
namespace LCU.Api.Internal.FileOperations.Validations;
|
|
|
|
public static class FileValidation
|
|
{
|
|
private const string DestinationField = "Destination";
|
|
private const string SourceField = "Source";
|
|
private const string FilenameField = "Filename";
|
|
private const string FieldEmptyResponse = " is empty and is required.";
|
|
private const string FileNameInvalid = "File name is invalid.";
|
|
private const string FileDoesNotExists = "File does not exist at path";
|
|
private const string FileExists = "File exists at path";
|
|
private const string FilesDoNotExists = "Files do not exist at path";
|
|
private const string FilesNotWithinRange = "Number of expected Files are not within range.";
|
|
private const string DirectoryDoesNotExists = "Directory does not exists at path";
|
|
private const string DirectoryExist = "Directory exists at path";
|
|
|
|
public static RequestResponse ValidateSourceNotEmpty(string source, RequestResponse requestResponse)
|
|
{
|
|
requestResponse = ValidateNotEmpty(source, SourceField, requestResponse);
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateDestinationNotEmpty(string destination, RequestResponse requestResponse)
|
|
{
|
|
requestResponse = ValidateNotEmpty(destination, DestinationField, requestResponse);
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateFilenameNotEmpty(string filename, RequestResponse requestResponse)
|
|
{
|
|
requestResponse = ValidateNotEmpty(filename, FilenameField, requestResponse);
|
|
return requestResponse;
|
|
}
|
|
|
|
private static RequestResponse ValidateNotEmpty(string field, string fieldName, RequestResponse requestResponse)
|
|
{
|
|
if (string.IsNullOrEmpty(field))
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add($"{fieldName}{FieldEmptyResponse}");
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateFilename(string filename, RequestResponse requestResponse)
|
|
{
|
|
if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add(FileNameInvalid);
|
|
}
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateFileExists(string path, RequestResponse requestResponse)
|
|
{
|
|
bool doesFileExists = false;
|
|
|
|
try
|
|
{
|
|
doesFileExists = File.Exists(path);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"{FileDoesNotExists} {path}");
|
|
}
|
|
|
|
if (!doesFileExists)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add($"{FileDoesNotExists} {path}");
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateFileNotExists(string path, RequestResponse requestResponse)
|
|
{
|
|
bool doesFileExists = false;
|
|
|
|
try
|
|
{
|
|
doesFileExists = File.Exists(path);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"{FileExists} {path}");
|
|
}
|
|
|
|
if (doesFileExists)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add($"{FileExists} {path}");
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateIfAnyRequest<TRequest>(List<TRequest> requests, string path, RequestResponse requestResponse)
|
|
{
|
|
bool anyRequest = false;
|
|
|
|
try
|
|
{
|
|
anyRequest = requests.Any();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"{FilesDoNotExists} {path}");
|
|
}
|
|
|
|
if (!anyRequest)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add($"{FilesDoNotExists} {path}");
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateIfAnyRequest<TRequest>(List<TRequest> requests,
|
|
string path,
|
|
bool succeedOnError,
|
|
RequestResponse requestResponse)
|
|
{
|
|
bool anyRequest = false;
|
|
try
|
|
{
|
|
anyRequest = requests.Any();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"{FilesDoNotExists} {path}");
|
|
}
|
|
if (!anyRequest)
|
|
{
|
|
requestResponse.IsSuccessful = succeedOnError;
|
|
requestResponse.MessageStack.Add($"{FilesDoNotExists} {path}");
|
|
}
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateFileRequestsWithinLimit<TRequest>(List<TRequest> requests,
|
|
int maxAmountOfFiles,
|
|
RequestResponse requestResponse)
|
|
{
|
|
bool countWithRange = false;
|
|
try
|
|
{
|
|
if (maxAmountOfFiles == 0)
|
|
{
|
|
return requestResponse;
|
|
}
|
|
countWithRange = requests.Count >= 0 && requests.Count <= maxAmountOfFiles;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
var message = $"Unexpected issue while validate file requests within limit.";
|
|
Log.Error(exception, $"Unexpected issue while validate file requests within limit.");
|
|
requestResponse.MessageStack.Add($"{message} Exception Message: {exception.Message}");
|
|
}
|
|
|
|
if (!countWithRange)
|
|
{
|
|
requestResponse.IsSuccessful = countWithRange;
|
|
requestResponse.MessageStack.Add($"{FilesNotWithinRange} Expected Range: 0-{maxAmountOfFiles}, Actual Files: {requests.Count} ");
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
|
|
public static RequestResponse ValidateDirectoryExists(string path, RequestResponse requestResponse)
|
|
{
|
|
bool doesDirectoryExists = false;
|
|
|
|
try
|
|
{
|
|
doesDirectoryExists = Directory.Exists(path);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"{DirectoryDoesNotExists} {path}");
|
|
}
|
|
|
|
if (!doesDirectoryExists)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add($"{DirectoryDoesNotExists} {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;
|
|
}
|
|
} |