forked from managedcode/markitdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkItDownPathResolver.cs
More file actions
127 lines (109 loc) · 3.74 KB
/
MarkItDownPathResolver.cs
File metadata and controls
127 lines (109 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.IO;
namespace MarkItDown;
/// <summary>
/// Provides centralized helpers for resolving MarkItDown's working directories.
/// </summary>
internal static class MarkItDownPathResolver
{
private static readonly object _gate = new();
private static string? _configuredRootPath;
private static string? _resolvedRootPath;
/// <summary>
/// Gets the absolute root directory used for MarkItDown workspaces.
/// Thread-safe; the value is resolved on first access and cached.
/// </summary>
public static string RootPath
{
get
{
if (_resolvedRootPath is not null)
{
return _resolvedRootPath;
}
lock (_gate)
{
_resolvedRootPath ??= CreateRootPath();
return _resolvedRootPath;
}
}
}
/// <summary>
/// Override the default root directory.
/// Must be called before any code accesses <see cref="RootPath"/> (typically
/// by setting <c>MarkItDownOptions.RootPath</c> before constructing a client).
/// Throws if the root has already resolved to a different path.
/// </summary>
internal static void Configure(string rootPath)
{
ArgumentException.ThrowIfNullOrWhiteSpace(rootPath);
var normalized = Path.GetFullPath(rootPath);
lock (_gate)
{
// Already resolved -- only allow if it matches.
if (_resolvedRootPath is not null)
{
if (!PathEquals(_resolvedRootPath, normalized))
{
throw new InvalidOperationException(
$"Root already resolved to '{_resolvedRootPath}'; cannot change to '{normalized}'.");
}
return;
}
// Not yet resolved -- only allow if no prior Configure set a different path.
if (_configuredRootPath is not null && !PathEquals(_configuredRootPath, normalized))
{
throw new InvalidOperationException(
$"Root already configured as '{_configuredRootPath}'; cannot change to '{normalized}'.");
}
_configuredRootPath = normalized;
}
}
/// <summary>
/// Ensure the root directory exists (also invoked by lazy initialization).
/// </summary>
public static void EnsureRootExists()
{
_ = RootPath;
}
/// <summary>
/// Combine the root directory with additional segments.
/// </summary>
public static string Combine(params string[] segments)
{
if (segments is null || segments.Length == 0)
{
return RootPath;
}
var paths = new string[segments.Length + 1];
paths[0] = RootPath;
for (var i = 0; i < segments.Length; i++)
{
paths[i + 1] = segments[i];
}
return Path.Combine(paths);
}
/// <summary>
/// Combine the root directory with segments and ensure the resulting directory exists.
/// </summary>
public static string Ensure(params string[] segments)
{
var path = Combine(segments);
Directory.CreateDirectory(path);
return path;
}
private static string CreateRootPath()
{
var candidate = _configuredRootPath
?? Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, ".markitdown"));
Directory.CreateDirectory(candidate);
return candidate;
}
private static bool PathEquals(string a, string b) =>
string.Equals(
Path.GetFullPath(a),
Path.GetFullPath(b),
OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal);
}