<PackageReference Include="Relativity.Server.Import.SDK" Version="2.9.2" />

PathWrap

class PathWrap : IPath
Represents a class object wrapper for the Path class.
using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Security; using System.Text.RegularExpressions; namespace Relativity.DataExchange.Io { [CLSCompliant(false)] internal class PathWrap : IPath { public const int MaxSupportedPathLength = 248; public static readonly string UncSignature = string.Empty.PadLeft(2, Path.DirectorySeparatorChar); private const int WindowsAppendFileNameLength = 12; private const int WindowsMaxPathLength = 260; private const string TempSubdirectoryName = "RelativityDataExchange"; private static readonly string DirectorySeparatorString; private static readonly string AltDirectorySeparatorString; private string customTempDirectory; public bool SupportLongPaths { get; set; } public string CustomTempPath { get { return customTempDirectory; } set { if (!string.IsNullOrEmpty(value)) { value = TryGetFullPath(value); if (!string.IsNullOrEmpty(value) && !PathEndsWithTrailingBackSlash(value)) value = AddTrailingBackSlash(value); } customTempDirectory = value; } } internal PathWrap() { SupportLongPaths = false; CustomTempPath = string.Empty; } public string AddTrailingBackSlash(string path) { if (!PathEndsWithTrailingBackSlash(path)) path += DirectorySeparatorString; return path; } public string ChangeExtension(string path, string extension) { path = NormalizePath(path); return Path.ChangeExtension(path, extension); } public string Combine(string path1, string path2) { path1 = NormalizePath(path1); path2 = NormalizePath(path2); return Path.Combine(path1, path2).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar); } public string ConvertIllegalCharactersInFilename(string fileName) { return ConvertIllegalCharactersInFilename(fileName, "_"); } public string ConvertIllegalCharactersInFilename(string fileName, string replacement) { return string.Copy(fileName).Replace("\\", replacement).Replace("/", replacement) .Replace("?", replacement) .Replace(":", replacement) .Replace("*", replacement) .Replace(">", replacement) .Replace("<", replacement) .Replace("|", replacement) .Replace("\"", replacement); } public string GetDirectoryName(string path) { path = NormalizePath(path); return Path.GetDirectoryName(path); } public string GetExtension(string path) { path = NormalizePath(path); return Path.GetExtension(path); } public string GetFileName(string path) { path = NormalizePath(path); return Path.GetFileName(path); } public string GetFullPath(string path) { path = NormalizePath(path); string text = TryGetFullPath(path); if (!string.IsNullOrEmpty(text)) return text; return path; } [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "The original implementation was explicitly designed to throw NullReferenceException.")] public string GetFullyQualifiedPath(Uri baseUri, string path) { if (IsPathFullyQualified(path)) { if (!path.EndsWith("/", StringComparison.OrdinalIgnoreCase)) path += "/"; return path; } return UrlHelper.GetBaseUrlAndCombine(baseUri.ToString(), path); } [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to ignore any exception and fallback to standard temp path.")] public string GetTempPath() { string customTempPath = CustomTempPath; if (string.IsNullOrEmpty(customTempPath)) { string tempPath = Path.GetTempPath(); try { string text = Path.Combine(tempPath, "RelativityDataExchange"); Directory.CreateDirectory(text); return text; } catch (Exception) { return tempPath; } } return customTempPath; } public string GetTempFileName() { return GetTempFileName(null); } [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Maintaining backwards compatibility.")] public string GetTempFileName(string fileNameSuffix) { string text = TemporaryFileNameWithoutCreatingEmptyFile(fileNameSuffix); try { using (File.Create(text)) return text; } catch { using (File.Create(text)) return text; } } public string TemporaryFileNameWithoutCreatingEmptyFile(string fileNameSuffix) { if (string.IsNullOrEmpty(fileNameSuffix)) fileNameSuffix = "rel-default"; string tempPath = GetTempPath(); string path = string.Join("-", DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture), Guid.NewGuid().ToString("D").ToUpperInvariant(), fileNameSuffix); return Combine(tempPath, Path.ChangeExtension(path, "tmp")); } public bool IsPathFullyQualified(string path) { return new Regex("\\w+:\\/\\/", RegexOptions.IgnoreCase).Matches(path).Count > 0; } public bool IsPathRooted(string path) { path = NormalizePath(path); return Path.IsPathRooted(path); } public bool IsPathUnc(string path) { if (string.IsNullOrEmpty(path)) return false; if (!string.IsNullOrEmpty(path)) return path.StartsWith(UncSignature, StringComparison.OrdinalIgnoreCase); return false; } public string NormalizePath(string path) { if (!SupportLongPaths || string.IsNullOrEmpty(path) || path.StartsWith("\\\\?\\", StringComparison.OrdinalIgnoreCase) || !Path.IsPathRooted(path)) return path; if (path.Length < 248) return path; if (IsPathUnc(path)) return "\\\\?\\UNC\\" + path.Substring(2); return "\\\\?\\" + path; } public bool PathEndsWithTrailingBackSlash(string path) { if (!string.IsNullOrEmpty(path)) { if (!path.EndsWith(DirectorySeparatorString, StringComparison.OrdinalIgnoreCase)) return path.EndsWith(AltDirectorySeparatorString, StringComparison.OrdinalIgnoreCase); return true; } return false; } public string TrimLeadingSlash(string path) { if (!string.IsNullOrEmpty(path)) return path.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); return string.Empty; } public string TrimTrailingSlash(string path) { if (!string.IsNullOrEmpty(path)) return path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); return string.Empty; } private static string TryGetFullPath(string path) { try { path = Path.GetFullPath(path); return path; } catch (ArgumentException) { return string.Empty; } catch (IOException) { return string.Empty; } catch (SecurityException) { return string.Empty; } catch (NotSupportedException) { return string.Empty; } } static PathWrap() { char c = Path.DirectorySeparatorChar; DirectorySeparatorString = c.ToString(CultureInfo.InvariantCulture); c = Path.AltDirectorySeparatorChar; AltDirectorySeparatorString = c.ToString(); } } }