|
2 | 2 | using System.ComponentModel;
|
3 | 3 | using System.Globalization;
|
4 | 4 | using System.IO;
|
| 5 | +using System.Linq; |
5 | 6 | using System.Runtime.CompilerServices;
|
6 | 7 | using System.Threading;
|
7 | 8 | using System.Threading.Tasks;
|
@@ -157,22 +158,23 @@ public static string GetFileLastModifiedAt(string filePath, string previewPanelD
|
157 | 158 |
|
158 | 159 | public static string GetFolderSize(string folderPath)
|
159 | 160 | {
|
| 161 | + using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); |
| 162 | + |
160 | 163 | try
|
161 | 164 | {
|
| 165 | + // Use parallel enumeration for better performance |
162 | 166 | var directoryInfo = new DirectoryInfo(folderPath);
|
163 |
| - long size = 0; |
164 |
| - using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)); |
165 |
| - foreach (var file in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) |
166 |
| - { |
167 |
| - if (cancellationTokenSource.Token.IsCancellationRequested) |
168 |
| - { |
169 |
| - // Timeout occurred, return unknown size |
170 |
| - return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown"); |
171 |
| - } |
172 |
| - size += file.Length; |
173 |
| - } |
| 167 | + long size = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories) |
| 168 | + .AsParallel() |
| 169 | + .WithCancellation(timeoutCts.Token) |
| 170 | + .Sum(file => file.Length); |
| 171 | + |
174 | 172 | return ResultManager.ToReadableSize(size, 2);
|
175 | 173 | }
|
| 174 | + catch (OperationCanceledException) |
| 175 | + { |
| 176 | + return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown"); |
| 177 | + } |
176 | 178 | catch (Exception e)
|
177 | 179 | {
|
178 | 180 | Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e);
|
|
0 commit comments