PathLengthValidator
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Relativity.Transfer
{
internal class PathLengthValidator : IPathValidator
{
private readonly ClientLimits clientLimits;
private readonly IFileSystemService fileSystemService;
private readonly ITransferLog logger;
public PathLengthValidator(ClientLimits clientLimits, IFileSystemService fileSystemService, ITransferLog log)
{
this.fileSystemService = fileSystemService;
this.clientLimits = clientLimits;
logger = log;
}
public PathValidationResult Validate(TransferPath transferPath)
{
if (transferPath == (TransferPath)null)
throw new ArgumentNullException("transferPath");
if (!IsPathValid(transferPath.SourcePath))
return CreateErrorResult(transferPath, transferPath.SourcePath, PathValidationStatus.SourcePathTooLong);
string text = ResolveTargetPath(transferPath);
if (!IsPathValid(text))
return CreateErrorResult(transferPath, text, PathValidationStatus.TargetPathToLong);
return PathValidationResult.Ok(transferPath);
}
public void Validate(IEnumerable<string> searchPaths, string targetPath)
{
List<string> list = searchPaths.ToList();
list.Add(targetPath);
foreach (string item in list) {
if (!string.IsNullOrEmpty(item) && !IsPathValid(item)) {
logger.LogError(CoreStrings.TransferPathProvidedTooLongExceptionMessage, item.Truncate(200), item.Length, clientLimits.MaxSupportedPathLength, LogRedaction.OnPositions(default(int)));
throw new PathTooLongException(CreateErrorMessage(item.Length));
}
}
}
private PathValidationResult CreateErrorResult(TransferPath transferPath, string validatedPath, PathValidationStatus errorStatus)
{
string text = CreateErrorMessage(validatedPath.Length);
if (GlobalSettings.Instance.SkipTooLongPaths) {
logger.LogWarning(CoreStrings.TransferPathProvidedTooLongExceptionMessage, validatedPath.Truncate(200), validatedPath.Length, clientLimits.MaxSupportedPathLength, LogRedaction.OnPositions(default(int)));
return PathValidationResult.Error(transferPath, errorStatus, text);
}
logger.LogError(CoreStrings.TransferPathProvidedTooLongExceptionMessage, validatedPath.Truncate(200), validatedPath.Length, clientLimits.MaxSupportedPathLength, LogRedaction.OnPositions(default(int)));
throw new PathTooLongException(text);
}
private bool IsPathValid(string path)
{
if (path.Length <= clientLimits.MaxSupportedPathLength)
return true;
return false;
}
private string CreateErrorMessage(int pathLength)
{
return $"""{pathLength}""{clientLimits.MaxSupportedPathLength}""";
}
private string ResolveTargetPath(TransferPath transferPath)
{
string text = string.IsNullOrEmpty(transferPath.TargetFileName) ? fileSystemService.GetFileName(transferPath.SourcePath) : transferPath.TargetFileName;
if (string.IsNullOrEmpty(transferPath.TargetPath))
return text;
return fileSystemService.Combine(transferPath.TargetPath, text);
}
}
}