feat: 图片扫描和发布命令 #5
|
@ -35,6 +35,11 @@ public partial class EssayScanService(
|
||||||
? new FileInfo(Path.Combine(drafts.FullName, content.FileName + ".md"))
|
? new FileInfo(Path.Combine(drafts.FullName, content.FileName + ".md"))
|
||||||
: new FileInfo(Path.Combine(posts.FullName, content.FileName + ".md"));
|
: new FileInfo(Path.Combine(posts.FullName, content.FileName + ".md"));
|
||||||
|
|
||||||
|
if (!isDraft)
|
||||||
|
{
|
||||||
|
content.Metadata.Date = DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
if (targetFile.Exists)
|
if (targetFile.Exists)
|
||||||
{
|
{
|
||||||
logger.LogWarning("Blog {} exists, overriding.", targetFile.Name);
|
logger.LogWarning("Blog {} exists, overriding.", targetFile.Name);
|
||||||
|
@ -45,7 +50,15 @@ public partial class EssayScanService(
|
||||||
await writer.WriteAsync("---\n");
|
await writer.WriteAsync("---\n");
|
||||||
await writer.WriteAsync(yamlSerializer.Serialize(content.Metadata));
|
await writer.WriteAsync(yamlSerializer.Serialize(content.Metadata));
|
||||||
await writer.WriteAsync("---\n");
|
await writer.WriteAsync("---\n");
|
||||||
await writer.WriteAsync("<!--more-->\n");
|
|
||||||
|
if (isDraft)
|
||||||
|
{
|
||||||
|
await writer.WriteLineAsync("<!--more-->");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await writer.WriteAsync(content.FileContent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ConcurrentBag<BlogContent>> ScanContentsInternal(DirectoryInfo directory)
|
private async Task<ConcurrentBag<BlogContent>> ScanContentsInternal(DirectoryInfo directory)
|
||||||
|
@ -116,7 +129,7 @@ public partial class EssayScanService(
|
||||||
return new ImageScanResult(unusedFiles, notFoundFiles);
|
return new ImageScanResult(unusedFiles, notFoundFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<ImageScanResult> ScanUnusedImagesInternal(IEnumerable<BlogContent> contents,
|
private static Task<ImageScanResult> ScanUnusedImagesInternal(IEnumerable<BlogContent> contents,
|
||||||
DirectoryInfo root)
|
DirectoryInfo root)
|
||||||
{
|
{
|
||||||
Regex imageRegex = ImageRegex();
|
Regex imageRegex = ImageRegex();
|
||||||
|
|
|
@ -81,6 +81,14 @@ public static class CommandExtensions
|
||||||
|
|
||||||
newCommand.SetHandler(async (file, _, _, essayScanService) =>
|
newCommand.SetHandler(async (file, _, _, essayScanService) =>
|
||||||
{
|
{
|
||||||
|
BlogContents contents = await essayScanService.ScanContents();
|
||||||
|
|
||||||
|
if (contents.Posts.Any(content => content.FileName == file))
|
||||||
|
{
|
||||||
|
Console.WriteLine("There exists the same title blog in posts.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await essayScanService.SaveBlogContent(new BlogContent
|
await essayScanService.SaveBlogContent(new BlogContent
|
||||||
{
|
{
|
||||||
FileName = file,
|
FileName = file,
|
||||||
|
@ -103,13 +111,13 @@ public static class CommandExtensions
|
||||||
BlogContents contents = await essyScanService.ScanContents();
|
BlogContents contents = await essyScanService.ScanContents();
|
||||||
|
|
||||||
Console.WriteLine($"All {contents.Posts.Count} Posts:");
|
Console.WriteLine($"All {contents.Posts.Count} Posts:");
|
||||||
foreach (BlogContent content in contents.Posts)
|
foreach (BlogContent content in contents.Posts.OrderBy(x => x.FileName))
|
||||||
{
|
{
|
||||||
Console.WriteLine($" - {content.FileName}");
|
Console.WriteLine($" - {content.FileName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"All {contents.Drafts.Count} Drafts:");
|
Console.WriteLine($"All {contents.Drafts.Count} Drafts:");
|
||||||
foreach (BlogContent content in contents.Drafts)
|
foreach (BlogContent content in contents.Drafts.OrderBy(x => x.FileName))
|
||||||
{
|
{
|
||||||
Console.WriteLine($" - {content.FileName}");
|
Console.WriteLine($" - {content.FileName}");
|
||||||
}
|
}
|
||||||
|
@ -156,4 +164,53 @@ public static class CommandExtensions
|
||||||
}
|
}
|
||||||
}, new BlogOptionsBinder(), new LoggerBinder<EssayScanService>(), new EssayScanServiceBinder(), removeOption);
|
}, new BlogOptionsBinder(), new LoggerBinder<EssayScanService>(), new EssayScanServiceBinder(), removeOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddPublishCommand(this RootCommand rootCommand)
|
||||||
|
{
|
||||||
|
Command command = new("publish", "Publish a new blog file.");
|
||||||
|
rootCommand.AddCommand(command);
|
||||||
|
|
||||||
|
Argument<string> filenameArgument = new(name: "blog name", description: "The published blog filename.");
|
||||||
|
command.AddArgument(filenameArgument);
|
||||||
|
|
||||||
|
command.SetHandler(async (blogOptions, _, essayScanService, filename) =>
|
||||||
|
{
|
||||||
|
BlogContents contents = await essayScanService.ScanContents();
|
||||||
|
|
||||||
|
BlogContent? content = (from blog in contents.Drafts
|
||||||
|
where blog.FileName == filename
|
||||||
|
select blog).FirstOrDefault();
|
||||||
|
|
||||||
|
if (content is null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Target blog does not exist.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将选中的博客文件复制到posts
|
||||||
|
await essayScanService.SaveBlogContent(content, isDraft: false);
|
||||||
|
|
||||||
|
// 复制图片文件夹
|
||||||
|
DirectoryInfo sourceImageDirectory =
|
||||||
|
new(Path.Combine(blogOptions.Value.Root, "drafts", content.FileName));
|
||||||
|
DirectoryInfo targetImageDirectory =
|
||||||
|
new(Path.Combine(blogOptions.Value.Root, "posts", content.FileName));
|
||||||
|
|
||||||
|
if (sourceImageDirectory.Exists)
|
||||||
|
{
|
||||||
|
targetImageDirectory.Create();
|
||||||
|
foreach (FileInfo file in sourceImageDirectory.EnumerateFiles())
|
||||||
|
{
|
||||||
|
file.CopyTo(Path.Combine(targetImageDirectory.FullName, file.Name), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceImageDirectory.Delete(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除原始的文件
|
||||||
|
FileInfo sourceBlogFile = new(Path.Combine(blogOptions.Value.Root, "drafts", content.FileName + ".md"));
|
||||||
|
sourceBlogFile.Delete();
|
||||||
|
}, new BlogOptionsBinder(),
|
||||||
|
new LoggerBinder<EssayScanService>(), new EssayScanServiceBinder(), filenameArgument);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,5 +8,6 @@ rootCommand.AddNewCommand();
|
||||||
rootCommand.AddListCommand();
|
rootCommand.AddListCommand();
|
||||||
rootCommand.AddWatchCommand();
|
rootCommand.AddWatchCommand();
|
||||||
rootCommand.AddScanCommand();
|
rootCommand.AddScanCommand();
|
||||||
|
rootCommand.AddPublishCommand();
|
||||||
|
|
||||||
await rootCommand.InvokeAsync(args);
|
await rootCommand.InvokeAsync(args);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user