Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b9f191

Browse files
committed
重构
1 parent 8f11ade commit 5b9f191

24 files changed

+735
-874
lines changed

‎README.md

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
# 博客园快捷发布工具
1+
# dotNet 博客园工具
22

33
[![](https://img.shields.io/nuget/v/dotnet-cnblog.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/dotnet-cnblog)
44

5-
## 一.准备环境
5+
## 1.安装
66

7-
安装.NET Core SDK: https://www.microsoft.com/net/learn/get-started/windows
8-
9-
.NET Core SDK 2.1:[点我下载](https://download.microsoft.com/download/8/8/5/88544F33-836A-49A5-8B67-451C24709A8F/dotnet-sdk-2.1.300-win-gs-x64.exe)
10-
11-
>运行本程序必须需要.NET Core 2.1或者更高版本
12-
13-
## 二.第一种安装方法
14-
15-
.NET Core Global Tool 是 .NET Core 2.1的新特性,类似于NPM,可以直接从NUGET安装我们编写的工具。
16-
17-
### 1.安装
18-
19-
打开cmd,执行命令,便可以直接安装本工具:
20-
21-
````shell
22-
dotnet tool install -g dotnet-cnblog
23-
````
24-
25-
![install](./assets/install.gif)
26-
27-
### 2.配置
7+
## 2.配置
288

299
第一次运行需要配置博客ID,账号密码等,按照提示输入即可,对信息采用tea加密算法进行加密存储。
3010

@@ -41,7 +21,7 @@ dotnet tool install -g dotnet-cnblog
4121

4222
![add_path](./assets/add_path.png)
4323

44-
### 3.重置配置
24+
### 重置配置
4525

4626
使用下面的命令重置配置:
4727
````shell
@@ -50,7 +30,7 @@ dotnet-cnblog reset
5030

5131
![reset](./assets/reset.png)
5232

53-
### 4.使用
33+
##3.使用
5434

5535
使用命令对Markdown文件里的图片进行解析,并上传到博客园,并且转换内容保存到新的文件中。
5636

@@ -59,37 +39,11 @@ dotnet-cnblog <markdown文件路径>
5939
````
6040
![test](./assets/test.gif)
6141

62-
## 三.第二种安装方法
63-
64-
### 1.编译Release包
65-
66-
进入`shell`文件夹,运行`publish.bat`
67-
68-
>可能会引起杀毒软件误报,请允许。
69-
70-
若出现 error NETSDK1053: Pack as tool does not support self contained. 则表示未完成,
71-
72-
> 解决办法:进入`项目根目录\CnBlogPublishTool`文件夹,修改dotnet-cnblog.csproj,
73-
删除`<PackAsTool>true</PackAsTool>`这条语句即可。
74-
75-
76-
### 2.创建快捷方式
77-
78-
进入 `项目根目录\Publish` 文件夹,选中 `dotnet-cnblog.exe`,【右键菜单】->【创建快捷方式】
79-
80-
然后进入 `项目根目录\shell` 文件夹,运行`deploy.bat`
81-
82-
### 3.使用
83-
84-
选中一个MarkDown文件,【右键菜单】->【发送到】->【dotnet-cnblog】,便会开始解析图片并自动上传到博客园。
85-
86-
第一次使用会让您配置博客ID和博客园的用户名密码,密码采用tea加密存储,请放心使用。
87-
88-
## 四.使用演示
42+
## 4.使用演示
8943

9044
![ys](./assets/ys.gif)
9145

92-
## .说明
46+
## 5.说明
9347

9448
- 程序未加过多的容错机制,请勿暴力测试。比如发送一个非MarkDown文件到程序。
9549

‎assets/install.gif

-56 KB
Binary file not shown.

‎dotnet-cnblogs-tool.sln.DotSettings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=cnblog/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Cnblogs/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dotnetcnblog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using MetaWeblogClient;
2+
3+
namespace Dotnetcnblog.Command
4+
{
5+
public class CommandContext
6+
{
7+
public string AppConfigFilePath { get; set; }
8+
public byte[] EncryptKey => new byte[] { 21, 52, 33, 78, 52, 45 };
9+
public BlogConnectionInfo ConnectionInfo { get; set; }
10+
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Dotnetcnblog.Command
2+
{
3+
public class CommandContextStore
4+
{
5+
private static CommandContext _context;
6+
public static void Set(CommandContext context)
7+
{
8+
_context = context;
9+
}
10+
11+
public static CommandContext Get()
12+
{
13+
return _context;
14+
}
15+
}
16+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.IO;
5+
using System.Linq;
6+
using Dotnetcnblog.TagHandlers;
7+
using Dotnetcnblog.Utils;
8+
using McMaster.Extensions.CommandLineUtils;
9+
using Console = Colorful.Console;
10+
11+
namespace Dotnetcnblog.Command
12+
{
13+
[Command(Name = "proc", Description = "处理文件")]
14+
public class CommandProcessFile : ICommand
15+
{
16+
private static readonly Dictionary<string, string> ReplaceDic = new Dictionary<string, string>();
17+
18+
19+
[Option("-f|--file", Description = "需要处理的文件路径")]
20+
[Required]
21+
public string FilePath { get; set; }
22+
23+
public int OnExecute(CommandLineApplication app)
24+
{
25+
if (app.Options.Count == 1 && app.Options[0].ShortName == "h")
26+
{
27+
app.ShowHelp();
28+
}
29+
30+
Execute(CommandContextStore.Get());
31+
return 0;
32+
}
33+
34+
public void Execute(CommandContext context)
35+
{
36+
try
37+
{
38+
if (!File.Exists(FilePath))
39+
{
40+
ConsoleHelper.PrintError($"文件不存在:{FilePath}");
41+
}
42+
else
43+
{
44+
var fileDir = new FileInfo(FilePath).DirectoryName;
45+
var fileContent = File.ReadAllText(FilePath);
46+
var imgHandler = new ImageHandler();
47+
var imgList = imgHandler.Process(fileContent);
48+
49+
ConsoleHelper.PrintMsg($"提取图片成功,共 {imgList.Count} 个。");
50+
51+
//循环上传图片
52+
foreach (var img in imgList)
53+
{
54+
if (img.StartsWith("http"))
55+
{
56+
ConsoleHelper.PrintMsg($"图片跳过:{img} ");
57+
continue;
58+
}
59+
60+
try
61+
{
62+
var imgPhyPath = Path.Combine(fileDir!, img);
63+
if (File.Exists(imgPhyPath))
64+
{
65+
var imgUrl = ImageUploadHelper.Upload(imgPhyPath);
66+
if (!ReplaceDic.ContainsKey(img)) ReplaceDic.Add(img, imgUrl);
67+
ConsoleHelper.PrintMsg($"{img} 上传成功. {imgUrl}");
68+
}
69+
else
70+
{
71+
ConsoleHelper.PrintMsg($"{img} 未发现文件.");
72+
}
73+
}
74+
catch (Exception e)
75+
{
76+
Console.WriteLine(e.Message);
77+
}
78+
}
79+
80+
//替换
81+
fileContent = ReplaceDic.Keys.Aggregate(fileContent, (current, key) => current.Replace(key, ReplaceDic[key]));
82+
83+
var newFileName = FilePath.Substring(0, FilePath.LastIndexOf('.')) + "-cnblog" + Path.GetExtension(FilePath);
84+
File.WriteAllText(newFileName, fileContent, FileEncodingType.GetType(FilePath));
85+
86+
ConsoleHelper.PrintMsg($"处理完成!文件保存在:{newFileName}");
87+
}
88+
}
89+
catch (Exception e)
90+
{
91+
ConsoleHelper.PrintError(e.Message);
92+
}
93+
}
94+
}
95+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.IO;
2+
using Dotnetcnblog.Utils;
3+
using McMaster.Extensions.CommandLineUtils;
4+
5+
namespace Dotnetcnblog.Command
6+
{
7+
[Command(Name = "reset", Description = "重置配置")]
8+
public class CommandReset : ICommand
9+
{
10+
public int OnExecute(CommandLineApplication app)
11+
{
12+
Execute(CommandContextStore.Get());
13+
return 0;
14+
}
15+
16+
public void Execute(CommandContext context)
17+
{
18+
File.Delete(context.AppConfigFilePath);
19+
ConsoleHelper.PrintMsg("配置重置成功!");
20+
}
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.IO;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Dotnetcnblog.Utils;
7+
using McMaster.Extensions.CommandLineUtils;
8+
using MetaWeblogClient;
9+
using Newtonsoft.Json;
10+
using Console = Colorful.Console;
11+
12+
namespace Dotnetcnblog.Command
13+
{
14+
[Command(Name = "set", Description = "设置配置")]
15+
public class CommandSetConfig : ICommand
16+
{
17+
18+
public int OnExecute(CommandLineApplication app)
19+
{
20+
Execute(CommandContextStore.Get());
21+
return 0;
22+
}
23+
24+
public void Execute(CommandContext context)
25+
{
26+
ConsoleHelper.PrintMsg("请输入博客ID:(如:https://www.cnblogs.com/stulzq 的博客id为 stulzq )");
27+
var blogId = Console.ReadLine();
28+
29+
ConsoleHelper.PrintMsg("请输入用户名:");
30+
var userName = Console.ReadLine();
31+
32+
ConsoleHelper.PrintMsg("请输入密 码:");
33+
var pwd = Console.ReadLine();
34+
35+
var config = new BlogConnectionInfo(
36+
"https://www.cnblogs.com/" + blogId,
37+
"https://rpc.cnblogs.com/metaweblog/" + blogId,
38+
blogId,
39+
userName,
40+
Convert.ToBase64String(TeaHelper.Encrypt(Encoding.UTF8.GetBytes(pwd), context.EncryptKey)));
41+
42+
File.WriteAllText(context.AppConfigFilePath, JsonConvert.SerializeObject(config));
43+
44+
ConsoleHelper.PrintMsg("配置设置成功!");
45+
}
46+
}
47+
}

‎src/dotnet-cnblogs/Command/ICommand.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Dotnetcnblog.Command
4+
{
5+
public interface ICommand
6+
{
7+
void Execute(CommandContext context);
8+
}
9+
}

‎src/dotnet-cnblogs/ImageUploader.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /