chore: release v2.4
- Add theme system (Dark/Light palettes, ModernTheme, ThemeManager) - Add InputDialog, Spinner common view - Add DuplicatesCsvExportService - Refresh views, dialogs, and view models across tabs - Update localization strings (en/fr) - Tweak services (transfer, permissions, search, user access, ownership elevation, bulk operations) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,13 @@ namespace SharepointToolbox.Services;
|
||||
/// </summary>
|
||||
public class StorageService : IStorageService
|
||||
{
|
||||
/// <summary>
|
||||
/// Collects per-library and per-folder storage metrics for a single
|
||||
/// SharePoint site. Depth and indentation are controlled via
|
||||
/// <paramref name="options"/>; libraries flagged <c>Hidden</c> are skipped.
|
||||
/// Traversal is breadth-first and leans on <see cref="SharePointPaginationHelper"/>
|
||||
/// so libraries above the 5,000-item threshold remain scannable.
|
||||
/// </summary>
|
||||
public async Task<IReadOnlyList<StorageNode>> CollectStorageAsync(
|
||||
ClientContext ctx,
|
||||
StorageScanOptions options,
|
||||
@@ -54,7 +61,7 @@ public class StorageService : IStorageService
|
||||
if (options.FolderDepth > 0)
|
||||
{
|
||||
await CollectSubfoldersAsync(
|
||||
ctx, lib.RootFolder.ServerRelativeUrl,
|
||||
ctx, lib, lib.RootFolder.ServerRelativeUrl,
|
||||
libNode, 1, options.FolderDepth,
|
||||
siteTitle, lib.Title, progress, ct);
|
||||
}
|
||||
@@ -65,6 +72,11 @@ public class StorageService : IStorageService
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aggregates file counts and total sizes by extension across every
|
||||
/// non-hidden document library on the site. Extensions are normalised to
|
||||
/// lowercase; files without an extension roll up into a single bucket.
|
||||
/// </summary>
|
||||
public async Task<IReadOnlyList<FileTypeMetric>> CollectFileTypeMetricsAsync(
|
||||
ClientContext ctx,
|
||||
IProgress<OperationProgress> progress,
|
||||
@@ -96,24 +108,19 @@ public class StorageService : IStorageService
|
||||
progress.Report(new OperationProgress(libIdx, libs.Count,
|
||||
$"Scanning files by type: {lib.Title} ({libIdx}/{libs.Count})"));
|
||||
|
||||
// Use CamlQuery to enumerate all files in the library
|
||||
// Paginate with 500 items per batch to avoid list view threshold issues
|
||||
// Paginated CAML without a WHERE clause — WHERE on non-indexed fields
|
||||
// (FSObjType) throws list-view threshold on libraries > 5,000 items.
|
||||
// Filter files client-side via FSObjType.
|
||||
var query = new CamlQuery
|
||||
{
|
||||
ViewXml = @"<View Scope='RecursiveAll'>
|
||||
<Query>
|
||||
<Where>
|
||||
<Eq>
|
||||
<FieldRef Name='FSObjType' />
|
||||
<Value Type='Integer'>0</Value>
|
||||
</Eq>
|
||||
</Where>
|
||||
</Query>
|
||||
<Query></Query>
|
||||
<ViewFields>
|
||||
<FieldRef Name='FSObjType' />
|
||||
<FieldRef Name='FileLeafRef' />
|
||||
<FieldRef Name='File_x0020_Size' />
|
||||
</ViewFields>
|
||||
<RowLimit Paged='TRUE'>500</RowLimit>
|
||||
<RowLimit Paged='TRUE'>5000</RowLimit>
|
||||
</View>"
|
||||
};
|
||||
|
||||
@@ -124,12 +131,15 @@ public class StorageService : IStorageService
|
||||
items = lib.GetItems(query);
|
||||
ctx.Load(items, ic => ic.ListItemCollectionPosition,
|
||||
ic => ic.Include(
|
||||
i => i["FSObjType"],
|
||||
i => i["FileLeafRef"],
|
||||
i => i["File_x0020_Size"]));
|
||||
await ExecuteQueryRetryHelper.ExecuteQueryRetryAsync(ctx, progress, ct);
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item["FSObjType"]?.ToString() != "0") continue; // skip folders
|
||||
|
||||
string fileName = item["FileLeafRef"]?.ToString() ?? string.Empty;
|
||||
string sizeStr = item["File_x0020_Size"]?.ToString() ?? "0";
|
||||
|
||||
@@ -137,7 +147,6 @@ public class StorageService : IStorageService
|
||||
fileSize = 0;
|
||||
|
||||
string ext = Path.GetExtension(fileName).ToLowerInvariant();
|
||||
// ext is "" for extensionless files, ".docx" etc. for others
|
||||
|
||||
if (extensionMap.TryGetValue(ext, out var existing))
|
||||
extensionMap[ext] = (existing.totalSize + fileSize, existing.count + 1);
|
||||
@@ -145,7 +154,6 @@ public class StorageService : IStorageService
|
||||
extensionMap[ext] = (fileSize, 1);
|
||||
}
|
||||
|
||||
// Move to next page
|
||||
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
|
||||
}
|
||||
while (items.ListItemCollectionPosition != null);
|
||||
@@ -198,21 +206,31 @@ public class StorageService : IStorageService
|
||||
var folderLookup = new Dictionary<string, StorageNode>(StringComparer.OrdinalIgnoreCase);
|
||||
BuildFolderLookup(libNode, libRootSrl, folderLookup);
|
||||
|
||||
// Capture original TotalSizeBytes before reset — StorageMetrics.TotalSize
|
||||
// includes version overhead, which cannot be rederived from a file scan
|
||||
// (File_x0020_Size is the current stream size only).
|
||||
var originalTotals = new Dictionary<StorageNode, long>();
|
||||
CaptureTotals(libNode, originalTotals);
|
||||
|
||||
// Reset all nodes in this tree to zero before accumulating
|
||||
ResetNodeCounts(libNode);
|
||||
|
||||
// Enumerate all files with their folder path
|
||||
// Paginated CAML without WHERE (filter folders client-side via FSObjType).
|
||||
// SMTotalSize = per-file total including all versions (version-aware).
|
||||
// SMTotalFileStreamSize = current stream only. File_x0020_Size is a fallback
|
||||
// when SMTotalSize is unavailable (older tenants / custom fields stripped).
|
||||
var query = new CamlQuery
|
||||
{
|
||||
ViewXml = @"<View Scope='RecursiveAll'>
|
||||
<Query><Where>
|
||||
<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>
|
||||
</Where></Query>
|
||||
<Query></Query>
|
||||
<ViewFields>
|
||||
<FieldRef Name='FSObjType' />
|
||||
<FieldRef Name='FileDirRef' />
|
||||
<FieldRef Name='File_x0020_Size' />
|
||||
<FieldRef Name='SMTotalSize' />
|
||||
<FieldRef Name='SMTotalFileStreamSize' />
|
||||
</ViewFields>
|
||||
<RowLimit Paged='TRUE'>500</RowLimit>
|
||||
<RowLimit Paged='TRUE'>5000</RowLimit>
|
||||
</View>"
|
||||
};
|
||||
|
||||
@@ -223,29 +241,38 @@ public class StorageService : IStorageService
|
||||
items = lib.GetItems(query);
|
||||
ctx.Load(items, ic => ic.ListItemCollectionPosition,
|
||||
ic => ic.Include(
|
||||
i => i["FSObjType"],
|
||||
i => i["FileDirRef"],
|
||||
i => i["File_x0020_Size"]));
|
||||
i => i["File_x0020_Size"],
|
||||
i => i["SMTotalSize"],
|
||||
i => i["SMTotalFileStreamSize"]));
|
||||
await ExecuteQueryRetryHelper.ExecuteQueryRetryAsync(ctx, progress, ct);
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
long size = 0;
|
||||
if (long.TryParse(item["File_x0020_Size"]?.ToString() ?? "0", out long s))
|
||||
size = s;
|
||||
if (item["FSObjType"]?.ToString() != "0") continue; // skip folders
|
||||
|
||||
long streamSize = ParseLong(item["File_x0020_Size"]);
|
||||
long smStream = ParseLong(SafeGet(item, "SMTotalFileStreamSize"));
|
||||
long smTotal = ParseLong(SafeGet(item, "SMTotalSize"));
|
||||
|
||||
// Prefer SM fields when present; fall back to File_x0020_Size otherwise.
|
||||
if (smStream > 0) streamSize = smStream;
|
||||
long totalSize = smTotal > 0 ? smTotal : streamSize;
|
||||
|
||||
string fileDirRef = item["FileDirRef"]?.ToString() ?? "";
|
||||
|
||||
// Always count toward the library root
|
||||
libNode.TotalSizeBytes += size;
|
||||
libNode.FileStreamSizeBytes += size;
|
||||
libNode.TotalSizeBytes += totalSize;
|
||||
libNode.FileStreamSizeBytes += streamSize;
|
||||
libNode.TotalFileCount++;
|
||||
|
||||
// Also count toward the most specific matching subfolder
|
||||
var matchedFolder = FindDeepestFolder(fileDirRef, folderLookup);
|
||||
if (matchedFolder != null && matchedFolder != libNode)
|
||||
{
|
||||
matchedFolder.TotalSizeBytes += size;
|
||||
matchedFolder.FileStreamSizeBytes += size;
|
||||
matchedFolder.TotalSizeBytes += totalSize;
|
||||
matchedFolder.FileStreamSizeBytes += streamSize;
|
||||
matchedFolder.TotalFileCount++;
|
||||
}
|
||||
}
|
||||
@@ -253,9 +280,37 @@ public class StorageService : IStorageService
|
||||
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
|
||||
}
|
||||
while (items.ListItemCollectionPosition != null);
|
||||
|
||||
// Restore original TotalSizeBytes where it exceeded the recomputed value.
|
||||
// Preserves StorageMetrics.TotalSize for nodes whose original metrics were
|
||||
// valid but SMTotalSize was missing on individual files.
|
||||
foreach (var kv in originalTotals)
|
||||
{
|
||||
if (kv.Value > kv.Key.TotalSizeBytes)
|
||||
kv.Key.TotalSizeBytes = kv.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static long ParseLong(object? value)
|
||||
{
|
||||
if (value == null) return 0;
|
||||
return long.TryParse(value.ToString(), out long n) ? n : 0;
|
||||
}
|
||||
|
||||
private static object? SafeGet(ListItem item, string fieldName)
|
||||
{
|
||||
try { return item[fieldName]; }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
private static void CaptureTotals(StorageNode node, Dictionary<StorageNode, long> map)
|
||||
{
|
||||
map[node] = node.TotalSizeBytes;
|
||||
foreach (var child in node.Children)
|
||||
CaptureTotals(child, map);
|
||||
}
|
||||
|
||||
private static bool HasZeroChild(StorageNode node)
|
||||
{
|
||||
foreach (var child in node.Children)
|
||||
@@ -349,6 +404,7 @@ public class StorageService : IStorageService
|
||||
|
||||
private static async Task CollectSubfoldersAsync(
|
||||
ClientContext ctx,
|
||||
List list,
|
||||
string parentServerRelativeUrl,
|
||||
StorageNode parentNode,
|
||||
int currentDepth,
|
||||
@@ -361,31 +417,42 @@ public class StorageService : IStorageService
|
||||
if (currentDepth > maxDepth) return;
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
// Load direct child folders of this folder
|
||||
Folder parentFolder = ctx.Web.GetFolderByServerRelativeUrl(parentServerRelativeUrl);
|
||||
ctx.Load(parentFolder,
|
||||
f => f.Folders.Include(
|
||||
sf => sf.Name,
|
||||
sf => sf.ServerRelativeUrl));
|
||||
await ExecuteQueryRetryHelper.ExecuteQueryRetryAsync(ctx, progress, ct);
|
||||
// Enumerate direct child folders via paginated CAML scoped to the parent.
|
||||
// Folder.Folders lazy loading hits the list-view threshold on libraries
|
||||
// > 5,000 items; a paged CAML query with no WHERE bypasses it.
|
||||
var subfolders = new List<(string Name, string ServerRelativeUrl)>();
|
||||
|
||||
foreach (Folder subFolder in parentFolder.Folders)
|
||||
await foreach (var item in SharePointPaginationHelper.GetItemsInFolderAsync(
|
||||
ctx, list, parentServerRelativeUrl, recursive: false,
|
||||
viewFields: new[] { "FSObjType", "FileLeafRef", "FileRef" },
|
||||
ct: ct))
|
||||
{
|
||||
if (item["FSObjType"]?.ToString() != "1") continue; // folders only
|
||||
|
||||
string name = item["FileLeafRef"]?.ToString() ?? string.Empty;
|
||||
string url = item["FileRef"]?.ToString() ?? string.Empty;
|
||||
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(url)) continue;
|
||||
|
||||
// Skip SharePoint system folders
|
||||
if (name.Equals("Forms", StringComparison.OrdinalIgnoreCase) ||
|
||||
name.StartsWith("_", StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
subfolders.Add((name, url));
|
||||
}
|
||||
|
||||
foreach (var sub in subfolders)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
// Skip SharePoint system folders
|
||||
if (subFolder.Name.Equals("Forms", StringComparison.OrdinalIgnoreCase) ||
|
||||
subFolder.Name.StartsWith("_", StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
var childNode = await LoadFolderNodeAsync(
|
||||
ctx, subFolder.ServerRelativeUrl, subFolder.Name,
|
||||
ctx, sub.ServerRelativeUrl, sub.Name,
|
||||
siteTitle, library, currentDepth, progress, ct);
|
||||
|
||||
if (currentDepth < maxDepth)
|
||||
{
|
||||
await CollectSubfoldersAsync(
|
||||
ctx, subFolder.ServerRelativeUrl, childNode,
|
||||
ctx, list, sub.ServerRelativeUrl, childNode,
|
||||
currentDepth + 1, maxDepth,
|
||||
siteTitle, library, progress, ct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user