Skip to content

Commit

Permalink
Additional fix for last change
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishamm committed Oct 30, 2024
1 parent 8e2b024 commit 661174a
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/DuetControlServer/Files/FilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static string ToPhysical(string filePath, FileDirectory directory)
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
return Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
return Path.Combine(path, match.Groups[2].Value);
}
}

Expand Down Expand Up @@ -92,7 +93,8 @@ public static string ToPhysical(string filePath, FileDirectory directory)

if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
directoryPath = Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
directoryPath = Path.Combine(path, match.Groups[2].Value);
}
}
}
Expand Down Expand Up @@ -124,7 +126,8 @@ public static async Task<string> ToPhysicalAsync(string filePath, FileDirectory
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
return Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
return Path.Combine(path, match.Groups[2].Value);
}
}

Expand Down Expand Up @@ -158,7 +161,8 @@ public static async Task<string> ToPhysicalAsync(string filePath, FileDirectory

if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
directoryPath = Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
directoryPath = Path.Combine(path, match.Groups[2].Value);
}
}
}
Expand Down Expand Up @@ -190,7 +194,8 @@ public static string ToPhysical(string filePath, string? directory = null)
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
return Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
return Path.Combine(path, match.Groups[2].Value);
}
}

Expand All @@ -211,7 +216,8 @@ public static string ToPhysical(string filePath, string? directory = null)
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
directory = Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
directory = Path.Combine(path, match.Groups[2].Value);
}
}
}
Expand Down Expand Up @@ -244,7 +250,8 @@ public static async Task<string> ToPhysicalAsync(string filePath, string? direct
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
return Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
return Path.Combine(path, match.Groups[2].Value);
}
}

Expand All @@ -265,7 +272,8 @@ public static async Task<string> ToPhysicalAsync(string filePath, string? direct
{
if (driveNumber > 0 && driveNumber < Model.Provider.Get.Volumes.Count)
{
directory = Path.Combine(Model.Provider.Get.Volumes[driveNumber].Path, match.Groups[2].Value);
string? path = Model.Provider.Get.Volumes[driveNumber].Path ?? throw new ArgumentException("Invalid drive index");
directory = Path.Combine(path, match.Groups[2].Value);
}
}
}
Expand All @@ -291,15 +299,17 @@ public static string ToVirtual(string filePath)

using (Model.Provider.AccessReadOnly())
{
foreach (DuetAPI.ObjectModel.Volume storage in Model.Provider.Get.Volumes)
for (int i = 1; i < Model.Provider.Get.Volumes.Count; i++)
{
if (filePath.StartsWith(storage.Path))
string? path = Model.Provider.Get.Volumes[i].Path;
if (path is not null && filePath.StartsWith(path))
{
return Path.Combine("0:/", filePath[storage.Path.Length..]);
return Path.Combine($"{i}:/", filePath[path.Length..]);
}
}
}

// Use default first volume as fallback
return Path.Combine("0:/", filePath);
}

Expand All @@ -319,15 +329,17 @@ public static async Task<string> ToVirtualAsync(string filePath)

using (await Model.Provider.AccessReadOnlyAsync())
{
foreach (DuetAPI.ObjectModel.Volume storage in Model.Provider.Get.Volumes)
for (int i = 1; i < Model.Provider.Get.Volumes.Count; i++)
{
if (filePath.StartsWith(storage.Path))
string? path = Model.Provider.Get.Volumes[i].Path;
if (path is not null && filePath.StartsWith(path))
{
return Path.Combine("0:/", filePath[storage.Path.Length..]);
return Path.Combine($"{i}:/", filePath[path.Length..]);
}
}
}

// Use default first volume as fallback
return Path.Combine("0:/", filePath);
}
}
Expand Down

0 comments on commit 661174a

Please sign in to comment.