Skip to content

Commit 8c5b402

Browse files
authored
Merge pull request #2 from managedcode/codex/analyze-project-and-provide-improvement-recommendations-6j8r71
Address review feedback on MIME detection and sync tool
2 parents 54b6f93 + a59c580 commit 8c5b402

File tree

13 files changed

+1442
-128
lines changed

13 files changed

+1442
-128
lines changed

.github/workflows/mime-sync.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: MIME database sync
2+
3+
on:
4+
schedule:
5+
- cron: '0 3 * * 1'
6+
workflow_dispatch:
7+
8+
jobs:
9+
sync:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup .NET
21+
uses: actions/setup-dotnet@v4
22+
with:
23+
dotnet-version: 8.0.x
24+
25+
- name: Restore dependencies
26+
run: dotnet restore
27+
28+
- name: Run MIME sync tool
29+
run: dotnet run --project ManagedCode.MimeTypes.Sync --configuration Release -- --prefer-remote
30+
31+
- name: Create Pull Request
32+
uses: peter-evans/create-pull-request@v6
33+
with:
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
commit-message: "chore: sync MIME database"
36+
title: "chore: sync MIME database"
37+
body: |
38+
Automated update of MIME database from configured sources.
39+
branch: chore/sync-mime-database
40+
delete-branch: true
41+
labels: |
42+
automation
43+
dependencies

ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ public void Execute(GeneratorExecutionContext context)
6565

6666
foreach (var item in properties)
6767
{
68-
defineDictionaryBuilder.AppendLine($"MimeTypes.Add(string.Intern(\"{item.Name}\"),string.Intern(\"{item.Value}\"));");
69-
types[ParseKey(item.Name)] = item.Value.ToString();
68+
var extension = item.Name.Trim();
69+
var mimeValue = item.Value.ToString()?.Trim() ?? string.Empty;
70+
71+
defineDictionaryBuilder.AppendLine($"RegisterMimeTypeInternal(\"{Escape(extension)}\", \"{Escape(mimeValue)}\");");
72+
types[ParseKey(extension)] = mimeValue;
7073
}
7174

7275
foreach (var item in types)
7376
{
74-
propertyBuilder.AppendLine($"public static string {item.Key} => \"{item.Value}\";");
77+
propertyBuilder.AppendLine($"public static string {item.Key} => \"{Escape(item.Value)}\";");
7578
}
7679

7780
context.AddSource("MimeHelper.Properties.cs", SourceText.From(@$"
@@ -124,17 +127,24 @@ private string GetMimeTypesPath(GeneratorExecutionContext context)
124127
return possiblePaths.FirstOrDefault(File.Exists) ?? possiblePaths[0];
125128
}
126129

127-
private string ParseKey(string key)
130+
private static string ParseKey(string key)
128131
{
129132
if (char.IsDigit(key[0]))
130133
{
131134
key = "_" + key;
132135
}
133-
134-
key = key.Replace("-", "_");
136+
137+
key = key.Replace("-", "_").Replace('.', '_');
135138

136139
return key.ToUpperInvariant();
137140
}
141+
142+
private static string Escape(string value)
143+
{
144+
return value
145+
.Replace("\\", "\\\\")
146+
.Replace("\"", "\\\"");
147+
}
138148
}
139149

140150

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="System.Text.Json" Version="8.0.5" />
11+
</ItemGroup>
12+
</Project>

0 commit comments

Comments
 (0)