From 661174aab60f71ad56dc9effe3c759ed3e0d3172 Mon Sep 17 00:00:00 2001 From: Christian Hammacher Date: Wed, 30 Oct 2024 13:07:50 +0100 Subject: [PATCH] Additional fix for last change --- src/DuetControlServer/Files/FilePath.cs | 40 ++++++++++++++++--------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/DuetControlServer/Files/FilePath.cs b/src/DuetControlServer/Files/FilePath.cs index dee9b5bab..0c80636a5 100644 --- a/src/DuetControlServer/Files/FilePath.cs +++ b/src/DuetControlServer/Files/FilePath.cs @@ -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); } } @@ -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); } } } @@ -124,7 +126,8 @@ public static async Task 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); } } @@ -158,7 +161,8 @@ public static async Task 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); } } } @@ -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); } } @@ -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); } } } @@ -244,7 +250,8 @@ public static async Task 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); } } @@ -265,7 +272,8 @@ public static async Task 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); } } } @@ -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); } @@ -319,15 +329,17 @@ public static async Task 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); } }