Skip to content

Commit 6bc8257

Browse files
committed
加入配置引导
1 parent f42f0e3 commit 6bc8257

File tree

8 files changed

+299
-64
lines changed

8 files changed

+299
-64
lines changed

CnBlogPublishTool/CnBlogPublishTool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="MetaWeblogClient.Core" Version="1.1.0" />
10+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1011
</ItemGroup>
1112

1213
<ItemGroup>

CnBlogPublishTool/ImageUploader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using CnBlogPublishTool.Util;
34
using MetaWeblogClient;
45

56
namespace CnBlogPublishTool
@@ -8,9 +9,9 @@ public class ImageUploader
89
{
910
public static Client BlogClient;
1011

11-
static ImageUploader()
12+
public static void Init(BlogConnectionInfo info)
1213
{
13-
BlogClient=new Client(new BlogConnectionInfo("http://www.cnblogs.com/stulzq", "https://rpc.cnblogs.com/metaweblog/stulzq", "stulzq","stulzq",""));
14+
BlogClient=new Client(info);
1415
}
1516

1617
public static string Upload(string filePath)

CnBlogPublishTool/ITagProcessor.cs renamed to CnBlogPublishTool/Processor/ITagProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace CnBlogPublishTool
3+
namespace CnBlogPublishTool.Processor
44
{
55
public interface ITagProcessor
66
{

CnBlogPublishTool/ImageProcessor.cs renamed to CnBlogPublishTool/Processor/ImageProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Text.RegularExpressions;
33

4-
namespace CnBlogPublishTool
4+
namespace CnBlogPublishTool.Processor
55
{
66
public class ImageProcessor:ITagProcessor
77
{

CnBlogPublishTool/Program.cs

Lines changed: 105 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Text.RegularExpressions;
4+
using System.Text;
5+
using CnBlogPublishTool.Processor;
6+
using CnBlogPublishTool.Util;
7+
using MetaWeblogClient;
8+
using Newtonsoft.Json;
59

610
namespace CnBlogPublishTool
711
{
@@ -10,77 +14,130 @@ class Program
1014
private static string _filePath;
1115
private static string _fileDir;
1216
private static string _fileContent;
17+
private static byte[] _teaKey=new byte[]{21,52,33,78,52,45};
18+
private const string ConfigFilePath = "config.json";
19+
private static BlogConnectionInfo _connInfo;
1320
private static readonly Dictionary<string,string> ReplaceDic=new Dictionary<string, string>();
1421
static void Main(string[] args)
1522
{
23+
//加载配置
24+
LoadConfig();
25+
1626
if (args.Length == 0)
1727
{
1828
Console.WriteLine("请输入文件路径!");
1929
}
2030
else
2131
{
22-
try
32+
ProcessFile(args);
33+
}
34+
35+
Console.WriteLine("按任意键退出...");
36+
Console.ReadKey();
37+
}
38+
39+
static void LoadConfig()
40+
{
41+
if (File.Exists(ConfigFilePath))
42+
{
43+
_connInfo = JsonConvert.DeserializeObject<BlogConnectionInfo>(File.ReadAllText(ConfigFilePath));
44+
_connInfo.Password =
45+
Encoding.UTF8.GetString(TeaHelper.Decrypt(Convert.FromBase64String(_connInfo.Password), _teaKey));
46+
ImageUploader.Init(_connInfo);
47+
}
48+
else
49+
{
50+
SetConfig();
51+
}
52+
}
53+
54+
static void SetConfig()
55+
{
56+
Console.WriteLine("您是第一次运行本程序,请配置以下参数:");
57+
58+
Console.WriteLine("请输入博客ID:(如:https://www.cnblogs.com/stulzq 的博客id为 stulzq )");
59+
string blogid = Console.ReadLine();
60+
61+
Console.WriteLine("请输入用户名:");
62+
string uname = Console.ReadLine();
63+
64+
Console.WriteLine("请输入密 码:");
65+
string pwd = Console.ReadLine();
66+
67+
_connInfo =new BlogConnectionInfo(
68+
"https://www.cnblogs.com/"+blogid,
69+
"https://rpc.cnblogs.com/metaweblog/"+blogid,
70+
blogid,
71+
uname,
72+
Convert.ToBase64String(TeaHelper.Encrypt(Encoding.UTF8.GetBytes(pwd), _teaKey)));
73+
74+
File.WriteAllText(ConfigFilePath,JsonConvert.SerializeObject(_connInfo));
75+
}
76+
77+
static void ProcessFile(string[] args)
78+
{
79+
try
80+
{
81+
_filePath = args[0];
82+
if (!File.Exists(_filePath))
2383
{
24-
_filePath = args[0];
25-
if (!File.Exists(_filePath))
26-
{
27-
Console.WriteLine("指定的文件不存在!");
28-
}
29-
else
84+
Console.WriteLine("指定的文件不存在!");
85+
}
86+
else
87+
{
88+
_fileDir = new FileInfo(_filePath).DirectoryName;
89+
_fileContent = File.ReadAllText(_filePath);
90+
var imgProcessor = new ImageProcessor();
91+
var imgList = imgProcessor.Process(_fileContent);
92+
Console.WriteLine($"提取图片成功,共{imgList.Count}个.");
93+
94+
//循环上传图片
95+
foreach (var img in imgList)
3096
{
31-
_fileDir=new FileInfo(_filePath).DirectoryName;
32-
_fileContent = File.ReadAllText(_filePath);
33-
var imgProcessor = new ImageProcessor();
34-
var imgList = imgProcessor.Process(_fileContent);
35-
Console.WriteLine($"提取图片成功,共{imgList.Count}个.");
36-
37-
//循环上传图片
38-
foreach (var img in imgList)
97+
if (img.StartsWith("http"))
3998
{
40-
if (img.StartsWith("http")) { continue;}
99+
Console.WriteLine($"{img} 跳过.");
100+
continue;
101+
}
41102

42-
try
103+
try
104+
{
105+
string imgPhyPath = Path.Combine(_fileDir, img);
106+
if (File.Exists(imgPhyPath))
43107
{
44-
string imgPhyPath = Path.Combine(_fileDir, img);
45-
if (File.Exists(imgPhyPath))
46-
{
47-
var imgUrl = ImageUploader.Upload(imgPhyPath);
48-
ReplaceDic.Add(img,imgUrl);
49-
Console.WriteLine($"{img} upload success. {imgUrl}");
50-
}
51-
else
52-
{
53-
Console.WriteLine($"{img} Not Found.");
54-
}
55-
108+
var imgUrl = ImageUploader.Upload(imgPhyPath);
109+
ReplaceDic.Add(img, imgUrl);
110+
Console.WriteLine($"{img} 上传成功. {imgUrl}");
56111
}
57-
catch (Exception e)
112+
else
58113
{
59-
Console.WriteLine(e.Message);
114+
Console.WriteLine($"{img} 未发现文件.");
60115
}
61-
}
62116

63-
//替换
64-
foreach (var key in ReplaceDic.Keys)
117+
}
118+
catch (Exception e)
65119
{
66-
_fileContent = _fileContent.Replace(key, ReplaceDic[key]);
120+
Console.WriteLine(e.Message);
67121
}
122+
}
68123

69-
string newFileName = _filePath.Substring(0, _filePath.LastIndexOf('.')) +"-cnlog-"+
70-
new FileInfo(_filePath).Extension;
71-
File.WriteAllText(newFileName, _fileContent,TxtFileEncoder.GetEncoding(_filePath));
72-
73-
Console.WriteLine($"处理完成!文件保存在:{newFileName}");
124+
//替换
125+
foreach (var key in ReplaceDic.Keys)
126+
{
127+
_fileContent = _fileContent.Replace(key, ReplaceDic[key]);
74128
}
75-
}
76-
catch (Exception e)
77-
{
78-
Console.WriteLine(e.Message);
129+
130+
string newFileName = _filePath.Substring(0, _filePath.LastIndexOf('.')) + "-cnblog-" +
131+
new FileInfo(_filePath).Extension;
132+
File.WriteAllText(newFileName, _fileContent, TxtFileEncoder.GetEncoding(_filePath));
133+
134+
Console.WriteLine($"处理完成!文件保存在:{newFileName}");
79135
}
80136
}
81-
82-
Console.WriteLine("按任意键退出...");
83-
Console.ReadKey();
137+
catch (Exception e)
138+
{
139+
Console.WriteLine(e.Message);
140+
}
84141
}
85142
}
86143
}

CnBlogPublishTool/MimeMapping.cs renamed to CnBlogPublishTool/Util/MimeMapping.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
using System;
22
using System.Collections;
33

4-
namespace CnBlogPublishTool
4+
namespace CnBlogPublishTool.Util
55
{
66
public class MimeMapping
77
{
88
private static Hashtable _mimeMappingTable;
99

10-
private static void AddMimeMapping(string extension, string MimeType)
10+
private static void AddMimeMapping(string extension, string mimeType)
1111
{
12-
MimeMapping._mimeMappingTable.Add(extension, MimeType);
12+
MimeMapping._mimeMappingTable.Add(extension, mimeType);
1313
}
1414

15-
public static string GetMimeMapping(string FileName)
15+
public static string GetMimeMapping(string fileName)
1616
{
1717
string text = null;
18-
int num = FileName.LastIndexOf('.');
19-
if (0 < num && num > FileName.LastIndexOf('\\'))
18+
int num = fileName.LastIndexOf('.');
19+
if (0 < num && num > fileName.LastIndexOf('\\'))
2020
{
21-
text = (string)MimeMapping._mimeMappingTable[FileName.Substring(num)];
21+
text = (string)MimeMapping._mimeMappingTable[fileName.Substring(num)];
2222
}
2323
if (text == null)
2424
{

0 commit comments

Comments
 (0)