Skip to content

Commit b9a6035

Browse files
committed
refactor: load noise list from embedded json resource
1 parent c30ecb9 commit b9a6035

5 files changed

Lines changed: 75 additions & 37 deletions

File tree

docs/specs/SPEC-noise-file-detection.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
This document lists the known noise file names filtered by the client inventory pipeline and their platform origin.
66

7+
The runtime source of truth is the embedded JSON resource:
8+
`src/ByteSync.Client/Services/Inventories/noise-files.json`.
9+
710
## Known noise file names
811

912
| File name | Origin platform | Typical purpose |

src/ByteSync.Client/ByteSync.Client.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
</EmbeddedResource>
112112
<None Remove="local.settings.json"/>
113113
<EmbeddedResource Include="local.settings.json"/>
114+
<EmbeddedResource Include="Services\Inventories\noise-files.json"/>
114115
</ItemGroup>
115116
<ItemGroup>
116117
<UpToDateCheckInput Remove="Assets\Resources\Resources.en.resx"/>
@@ -258,4 +259,4 @@
258259
<SubType>Code</SubType>
259260
</Compile>
260261
</ItemGroup>
261-
</Project>
262+
</Project>
Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1+
using System.Reflection;
2+
using System.Text.Json;
13
using ByteSync.Common.Business.Misc;
24

35
namespace ByteSync.Services.Inventories;
46

57
public static class NoiseFileDetector
68
{
7-
private static readonly string[] KnownNoiseFileNames =
8-
[
9-
"desktop.ini",
10-
"thumbs.db",
11-
"ehthumbs.db",
12-
"ehthumbs_vista.db",
13-
".desktop.ini",
14-
".thumbs.db",
15-
".DS_Store",
16-
".AppleDouble",
17-
".LSOverride",
18-
".Spotlight-V100",
19-
".Trashes",
20-
".fseventsd",
21-
".TemporaryItems",
22-
".VolumeIcon.icns",
23-
".directory"
24-
];
9+
private const string NoiseFileResourceSuffix = ".Services.Inventories.noise-files.json";
10+
private static readonly string[] KnownNoiseFileNames = LoadNoiseFileNames();
2511

2612
private static readonly HashSet<string> CaseSensitiveNoiseFileNames = new(KnownNoiseFileNames, StringComparer.Ordinal);
2713
private static readonly HashSet<string> CaseInsensitiveNoiseFileNames = new(KnownNoiseFileNames, StringComparer.OrdinalIgnoreCase);
@@ -37,4 +23,33 @@ public static bool IsNoiseFileName(string? fileName, OSPlatforms os)
3723
? CaseSensitiveNoiseFileNames.Contains(fileName)
3824
: CaseInsensitiveNoiseFileNames.Contains(fileName);
3925
}
26+
27+
private static string[] LoadNoiseFileNames()
28+
{
29+
var assembly = typeof(NoiseFileDetector).Assembly;
30+
var resourceName = assembly.GetManifestResourceNames()
31+
.SingleOrDefault(rn => rn.EndsWith(NoiseFileResourceSuffix, StringComparison.Ordinal));
32+
33+
if (resourceName == null)
34+
{
35+
throw new InvalidOperationException($"Embedded resource not found: '*{NoiseFileResourceSuffix}'");
36+
}
37+
38+
using var stream = assembly.GetManifestResourceStream(resourceName);
39+
if (stream == null)
40+
{
41+
throw new InvalidOperationException($"Unable to open embedded resource stream: '{resourceName}'");
42+
}
43+
44+
var parsed = JsonSerializer.Deserialize<string[]>(stream);
45+
if (parsed == null)
46+
{
47+
throw new InvalidOperationException($"Unable to deserialize embedded resource: '{resourceName}'");
48+
}
49+
50+
return parsed
51+
.Where(s => !string.IsNullOrWhiteSpace(s))
52+
.Distinct(StringComparer.Ordinal)
53+
.ToArray();
54+
}
4055
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
"desktop.ini",
3+
"thumbs.db",
4+
"ehthumbs.db",
5+
"ehthumbs_vista.db",
6+
".desktop.ini",
7+
".thumbs.db",
8+
".DS_Store",
9+
".AppleDouble",
10+
".LSOverride",
11+
".Spotlight-V100",
12+
".Trashes",
13+
".fseventsd",
14+
".TemporaryItems",
15+
".VolumeIcon.icns",
16+
".directory"
17+
]

tests/ByteSync.Client.UnitTests/Services/Inventories/NoiseFileDetectorTests.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Reflection;
2+
using System.Text.Json;
13
using ByteSync.Common.Business.Misc;
24
using ByteSync.Services.Inventories;
35
using FluentAssertions;
@@ -7,24 +9,7 @@ namespace ByteSync.Client.UnitTests.Services.Inventories;
79

810
public class NoiseFileDetectorTests
911
{
10-
private static readonly string[] KnownNoiseFileNames =
11-
[
12-
"desktop.ini",
13-
"thumbs.db",
14-
"ehthumbs.db",
15-
"ehthumbs_vista.db",
16-
".desktop.ini",
17-
".thumbs.db",
18-
".DS_Store",
19-
".AppleDouble",
20-
".LSOverride",
21-
".Spotlight-V100",
22-
".Trashes",
23-
".fseventsd",
24-
".TemporaryItems",
25-
".VolumeIcon.icns",
26-
".directory"
27-
];
12+
private static readonly string[] KnownNoiseFileNames = LoadNoiseFileNamesFromEmbeddedResource();
2813

2914
[TestCaseSource(nameof(KnownNoiseFileNames))]
3015
public void IsNoiseFileName_ShouldReturnTrue_ForKnownNoiseFiles_OnWindows(string fileName)
@@ -107,4 +92,21 @@ public void IsNoiseFileName_ShouldReturnFalse_ForEmptyValues(string? fileName)
10792
windowsResult.Should().BeFalse();
10893
linuxResult.Should().BeFalse();
10994
}
95+
96+
private static string[] LoadNoiseFileNamesFromEmbeddedResource()
97+
{
98+
var assembly = typeof(NoiseFileDetector).Assembly;
99+
var resourceName = assembly.GetManifestResourceNames()
100+
.SingleOrDefault(rn => rn.EndsWith(".Services.Inventories.noise-files.json", StringComparison.Ordinal));
101+
102+
resourceName.Should().NotBeNull();
103+
104+
using var stream = assembly.GetManifestResourceStream(resourceName!);
105+
stream.Should().NotBeNull();
106+
107+
var data = JsonSerializer.Deserialize<string[]>(stream!);
108+
data.Should().NotBeNull();
109+
110+
return data!;
111+
}
110112
}

0 commit comments

Comments
 (0)