Skip to content

Commit 218a16f

Browse files
ignatandreiCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 5790b1f commit 218a16f

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

v2/Generator.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratorData", "GeneratorD
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GV.Steps", "GV.Steps\GV.Steps.csproj", "{57236574-B2E4-4742-BDD3-0C72E2220676}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenLiveWriterPost", "OpenLiveWriterPost\OpenLiveWriterPost.csproj", "{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLiveWriterPost", "OpenLiveWriterPost\OpenLiveWriterPost.csproj", "{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}"
1515
EndProject
1616
Global
1717
GlobalSection(SolutionConfigurationPlatforms) = preSolution

v2/OpenLiveWriterPost/OpenLiveWriterPostGenerator.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,58 @@ public static void SavePost(BlogPost post, string filePath)
1717
private static void SavePostAsStructuredStorage(BlogPost post, string filePath)
1818
{
1919
ApplicationEnvironment.Initialize(Assembly.GetExecutingAssembly(),
20-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"\Windows Live\Writer\"));
20+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Windows Live\Writer"));
2121

2222
PostEditorFile.Initialize();
2323

24-
PostEditorFile pef = PostEditorFile.CreateNew(new DirectoryInfo(Path.GetDirectoryName(filePath)));
24+
string? directoryPath = Path.GetDirectoryName(filePath);
25+
if (string.IsNullOrEmpty(directoryPath))
26+
{
27+
directoryPath = Directory.GetCurrentDirectory();
28+
}
29+
30+
DirectoryInfo targetDirectory = new DirectoryInfo(directoryPath);
31+
string[] existingDrafts = Directory.GetFiles(targetDirectory.FullName, "*.wpost");
32+
33+
PostEditorFile pef = PostEditorFile.CreateNew(targetDirectory);
2534
var bef = new BlogPostEditingContext(post.Id, post);
2635
pef.SaveBlogPost(bef);
36+
37+
string createdDraftPath = FindCreatedDraftPath(targetDirectory.FullName, existingDrafts);
38+
if (!string.Equals(createdDraftPath, filePath, StringComparison.OrdinalIgnoreCase))
39+
{
40+
if (File.Exists(filePath))
41+
{
42+
File.Delete(filePath);
43+
}
44+
45+
File.Move(createdDraftPath, filePath);
46+
}
47+
}
48+
49+
private static string FindCreatedDraftPath(string directoryPath, string[] existingDrafts)
50+
{
51+
string[] currentDrafts = Directory.GetFiles(directoryPath, "*.wpost");
52+
53+
foreach (string currentDraft in currentDrafts)
54+
{
55+
bool existedBefore = false;
56+
foreach (string existingDraft in existingDrafts)
57+
{
58+
if (string.Equals(currentDraft, existingDraft, StringComparison.OrdinalIgnoreCase))
59+
{
60+
existedBefore = true;
61+
break;
62+
}
63+
}
64+
65+
if (!existedBefore)
66+
{
67+
return currentDraft;
68+
}
69+
}
70+
71+
throw new IOException($"Unable to determine the draft file created in '{directoryPath}'.");
2772
}
2873

2974

v2/OpenLiveWriterPost/WritePost.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ public static void GeneratePostFromHtml(string name, string html)
1616
Contents = html,
1717
//Categories = [ "Programming", "C#", "Blogging" ],
1818
};
19-
var x = ImageEmbedType.Linked;
20-
if(name =="")x= ImageEmbedType.Embedded;
2119
// Save the post
22-
var fileName1 = $"{post1.Title}.wpost";
20+
var safeTitle = SanitizeFileName(post1.Title);
21+
var fileName1 = $"{safeTitle}.wpost";
2322
var filePath1 = Path.Combine(draftsFolder1, fileName1);
2423

2524
if(File.Exists(filePath1)) File.Delete(filePath1);
@@ -29,6 +28,16 @@ public static void GeneratePostFromHtml(string name, string html)
2928
OpenLiveWriterPostGenerator.SavePost(post1, filePath1);
3029

3130
}
31+
static string SanitizeFileName(string? name)
32+
{
33+
var sanitizedName = (name ?? string.Empty).Trim();
34+
foreach (var invalidChar in Path.GetInvalidFileNameChars())
35+
{
36+
sanitizedName = sanitizedName.Replace(invalidChar, '_');
37+
}
38+
39+
return string.IsNullOrWhiteSpace(sanitizedName) ? "untitled" : sanitizedName;
40+
}
3241
static string GetOpenLiveWriterDraftsFolder()
3342
{
3443
// OpenLive Writer drafts are typically stored in:

0 commit comments

Comments
 (0)