Skip to content

Commit 7c9cabb

Browse files
author
Meyn
committed
Fix path check for UNIX systems in LoadRequest
1 parent f17ebc8 commit 7c9cabb

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

DownloadAssistant/DownloadAssistant.csproj

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<Nullable>enable</Nullable>
77
<Title>DownloadAssistant</Title>
88
<PackageId>Shard.DownloadAssistant</PackageId>
9-
<Copyright>Shard © 2024</Copyright>
9+
<Copyright>Shard © 2025</Copyright>
1010
<PackageProjectUrl></PackageProjectUrl>
1111
<PackageIcon>logo.png</PackageIcon>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
13-
<Version>1.0.6</Version>
13+
<Version>1.0.7</Version>
1414
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1515
<Description>A free to use library as a download manager.
1616
Includes retry, priority, cancel, etc.. function.
@@ -26,13 +26,7 @@ Features:
2626
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2727
<Company>Shard</Company>
2828
<Authors>Meyn</Authors>
29-
<PackageReleaseNotes>Implement InterceptCompletionAsync for GetRequest.
30-
Fix SubsequentRequest functionality in LoadRequest.
31-
Resolve file information parsing issues in GetRequest partial downloads.
32-
Fix exception handling for Append mode in LoadRequest.
33-
Address chunk merging issues in LoadRequest.
34-
Update `Shard.Requests` to version 2.2.1.
35-
Update unit tests</PackageReleaseNotes>
29+
<PackageReleaseNotes>Fix path check for UNIX systems in LoadRequest</PackageReleaseNotes>
3630
<RepositoryUrl>https://github.com/TypNull/DownloadAssistant</RepositoryUrl>
3731
</PropertyGroup>
3832

DownloadAssistant/Utilities/IOManager.cs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class IOManager
1313
/// <summary>
1414
/// Array of characters that are not allowed in file names.
1515
/// </summary>
16-
public static char[] InvalidFileNameChars = new char[]
16+
public static char[] InvalidFileNameCharsWindows = new char[]
1717
{
1818
'\"', '<', '>', '|', '\0',
1919
(char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10,
@@ -22,6 +22,8 @@ internal static class IOManager
2222
(char)31, ':', '?', '\\', '/'
2323
};
2424

25+
public static char[] InvalidFileNameCharsUnix = new char[] { '/', '\0' };
26+
2527
/// <summary>
2628
/// Converts bytes to megabytes.
2729
/// </summary>
@@ -40,8 +42,12 @@ internal static class IOManager
4042
public static string RemoveInvalidFileNameChars(string name)
4143
{
4244
StringBuilder fileBuilder = new(name);
43-
foreach (char c in InvalidFileNameChars)
44-
fileBuilder.Replace(c.ToString(), string.Empty);
45+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
46+
foreach (char c in InvalidFileNameCharsWindows)
47+
fileBuilder.Replace(c.ToString(), string.Empty);
48+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
49+
foreach (char c in InvalidFileNameCharsUnix)
50+
fileBuilder.Replace(c.ToString(), string.Empty);
4551
return fileBuilder.ToString();
4652
}
4753

@@ -58,24 +64,32 @@ public static string RemoveInvalidFileNameChars(string name)
5864
public static bool IsValidPath(string path) => TryGetFullPath(path, out _);
5965

6066

61-
/// <summary>
62-
/// Tries to get the absolute path for the specified path string.
63-
/// </summary>
64-
/// <param name="path">The file or directory for which to obtain absolute path information.</param>
65-
/// <param name="result">
66-
/// When this method returns, contains the absolute path representation of <paramref name="path"/>,
67-
/// if the conversion succeeded, or <see cref="string.Empty"/> if the conversion failed.
68-
/// </param>
69-
/// <returns>
70-
/// <c>true</c> if <paramref name="path"/> was converted to an absolute path successfully; otherwise, <c>false</c>.
71-
/// </returns>
7267
public static bool TryGetFullPath(string path, out string result)
7368
{
7469
result = string.Empty;
75-
if (string.IsNullOrWhiteSpace(path) || path[1] != ':')
70+
71+
if (string.IsNullOrWhiteSpace(path))
7672
return false;
77-
bool status = false;
7873

74+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
75+
{
76+
if (path.Length < 2 || path[1] != ':')
77+
return false;
78+
}
79+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
80+
{
81+
char[] invalidChars = Path.GetInvalidPathChars();
82+
if (path.IndexOfAny(invalidChars) >= 0)
83+
return false;
84+
85+
if (Encoding.UTF8.GetByteCount(path) > 4096)
86+
return false;
87+
88+
if (!path.StartsWith("/") && !path.StartsWith("./") && !path.StartsWith("../"))
89+
return false;
90+
}
91+
92+
bool status = false;
7993
try
8094
{
8195
result = Path.GetFullPath(path);
@@ -97,9 +111,11 @@ public static bool TryGetFullPath(string path, out string result)
97111
/// <exception cref="SecurityException">Thrown when the caller does not have the required permission to perform this operation.</exception>
98112
public static string? GetHomePath()
99113
{
100-
if (Environment.OSVersion.Platform == PlatformID.Unix)
114+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
115+
return Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
116+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
101117
return Environment.GetEnvironmentVariable("HOME");
102-
return Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
118+
throw new PlatformNotSupportedException("The platform is not supported.");
103119
}
104120

105121
/// <summary>
@@ -112,14 +128,7 @@ public static bool TryGetFullPath(string path, out string result)
112128
/// <exception cref="SecurityException">Thrown when the caller does not have the required permission to perform this operation.</exception>
113129
public static string? GetDownloadFolderPath()
114130
{
115-
if (Environment.OSVersion.Platform == PlatformID.Unix)
116-
{
117-
string? homePath = GetHomePath();
118-
if (string.IsNullOrEmpty(homePath))
119-
return null;
120-
return Path.Combine(homePath, "Downloads");
121-
}
122-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
131+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
123132
{
124133
string path = SHGetKnownFolderPath(new("374DE290-123F-4565-9164-39C4925E467B"), 0);
125134
if (string.IsNullOrEmpty(path))
@@ -130,6 +139,13 @@ public static bool TryGetFullPath(string path, out string result)
130139
, string.Empty));
131140
else return path;
132141
}
142+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
143+
{
144+
string? homePath = Environment.GetEnvironmentVariable("HOME");
145+
if (string.IsNullOrEmpty(homePath))
146+
return null;
147+
return Path.Combine(homePath, "Downloads");
148+
}
133149
else return null;
134150
}
135151

0 commit comments

Comments
 (0)