Skip to content

Commit d114fb3

Browse files
authored
Merge pull request #80 from aisensiy/for-pr
修复 csdn 以及其他相关问题
2 parents de45fba + 0ce16b7 commit d114fb3

File tree

7 files changed

+5199
-58
lines changed

7 files changed

+5199
-58
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"logger2x": "0.0.3",
3535
"marked": "^0.8.0",
3636
"mime-types": "^2.1.27",
37+
"node-fetch": "2",
3738
"path": "^0.12.7",
3839
"picgo": "^1.4.12",
3940
"querystring": "^0.2.0",

src/app-menu.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
185185
{
186186
label: '配置',
187187
click: function () {
188-
if (!shell.openItem(picgo.configPath)) {
188+
if (!shell.openPath(picgo.configPath)) {
189189
appToast.toast({title: '打开配置文件失败', body: ""})
190190
}
191191
}
@@ -210,7 +210,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
210210
{
211211
label: '配置',
212212
click: function () {
213-
if (!shell.openItem(picgo.configPath)) {
213+
if (!shell.openPath(picgo.configPath)) {
214214
appToast.toast({title: '打开配置文件失败', body: ""})
215215
}
216216
}
@@ -235,7 +235,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
235235
{
236236
label: '配置',
237237
click: function () {
238-
if (!shell.openItem(picgo.configPath)) {
238+
if (!shell.openPath(picgo.configPath)) {
239239
appToast.toast({title: '打开配置文件失败', body: ""})
240240
}
241241
}
@@ -260,7 +260,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
260260
{
261261
label: '配置',
262262
click: function () {
263-
if (!shell.openItem(picgo.configPath)) {
263+
if (!shell.openPath(picgo.configPath)) {
264264
appToast.toast({title: '打开配置文件失败', body: ""})
265265
}
266266
}
@@ -285,7 +285,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
285285
{
286286
label: '配置',
287287
click: function () {
288-
if (!shell.openItem(picgo.configPath)) {
288+
if (!shell.openPath(picgo.configPath)) {
289289
appToast.toast({title: '打开配置文件失败', body: ""})
290290
}
291291
}
@@ -310,7 +310,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
310310
{
311311
label: '配置',
312312
click: function () {
313-
if (!shell.openItem(picgo.configPath)) {
313+
if (!shell.openPath(picgo.configPath)) {
314314
appToast.toast({title: '打开配置文件失败', body: ""})
315315
}
316316
}
@@ -335,7 +335,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
335335
{
336336
label: '配置',
337337
click: function () {
338-
if (!shell.openItem(picgo.configPath)) {
338+
if (!shell.openPath(picgo.configPath)) {
339339
appToast.toast({title: '打开配置文件失败', body: ""})
340340
}
341341
}
@@ -502,7 +502,7 @@ exports.buildContextMenu = function buildContextMenu(tray) {
502502
}, {
503503
label: '查看日志',
504504
click: function () {
505-
shell.openItem(require('path').join(require('os').homedir(), app.name));
505+
shell.openPath(require('path').join(require('os').homedir(), app.name));
506506
}
507507
}
508508
]
@@ -536,4 +536,4 @@ function closeMenuChecked(id, menu) {
536536
appUtil.myGetMenuItemById(pic, menu).checked = false
537537
}
538538
}
539-
}
539+
}

src/blog/csdn.js

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,81 @@
11
const https = require('https');
2+
const fetch = require('node-fetch');
23
const DataStore = require('../app-store');
34
const dataStore = new DataStore();
45
const FormData = require('form-data');
56
const fs = require('fs');
67

8+
const extMap = {
9+
'jpg': 'jpeg',
10+
'jpeg': 'jpeg',
11+
'png': 'png',
12+
'gif': 'gif',
13+
}
14+
715
//上传图片到CSDN
816
function uploadPictureToCSDN(filePath) {
917
return new Promise((resolve, reject) => {
10-
let formData = new FormData();
11-
formData.append('file', fs.createReadStream(filePath));
18+
const ext = extMap[filePath.split('.').pop().toLowerCase()] || 'png';
1219

13-
let headers = formData.getHeaders();
14-
headers.Cookie = dataStore.getCSDNCookies(); //获取Cookie
15-
headers["user-agent"] = "Mozilla/5.0";
16-
//自己的headers属性在这里追加
17-
let request = https.request({
18-
host: 'blog-console-api.csdn.net',
19-
method: 'POST',
20-
path: '/v1/upload/img?shuiyin=2',
21-
headers: headers
22-
}, function (res) {
23-
let str = '';
24-
res.on('data', function (buffer) {
25-
str += buffer;
26-
}
27-
);
28-
res.on('end', () => {
29-
if (res.statusCode === 200) {
30-
const result = JSON.parse(str);
31-
if (result.code === 200) {
32-
const url = result.data.url;
33-
resolve(url)
34-
} else {
35-
reject('上传图片失败,' + result.msg)
36-
}
37-
} else {
38-
console.log(filePath);
39-
try {
40-
const result = JSON.parse(str);
41-
reject(decodeURI(result.msg))
42-
} catch (e) {
43-
}
44-
reject('上传图片失败:' + res.statusCode)
45-
}
46-
});
47-
});
48-
formData.pipe(request);
20+
fetch("https://imgservice.csdn.net/direct/v1.0/image/upload?watermark=&type=blog&rtype=markdown", {
21+
"headers": {
22+
"content-type": "application/json",
23+
"x-image-app": "direct_blog",
24+
"x-image-dir": "direct",
25+
"x-image-suffix": ext,
26+
"cookie": dataStore.getCSDNCookies(),
27+
"Referer": "https://editor.csdn.net/",
28+
}
29+
}).then(result => result.json())
30+
.then(result => {
31+
console.log('get access info:', result);
32+
if (result.code === 200) {
33+
const accessId = result.data.accessId;
34+
const callbackUrl = result.data.callbackUrl;
35+
const remoteFilePath = result.data.filePath;
36+
const url = result.data.host;
37+
const policy = result.data.policy;
38+
const signature = result.data.signature;
4939

50-
request.on('error', function (e) {
51-
reject('网络连接异常'+e.message)
52-
});
40+
let formData = new FormData();
41+
formData.append('key', remoteFilePath);
42+
formData.append('OSSAccessKeyId', accessId);
43+
formData.append('policy', policy);
44+
formData.append('signature', signature);
45+
formData.append('success_action_status', '200');
46+
formData.append('callback', callbackUrl);
47+
formData.append('file', fs.createReadStream(filePath));
48+
49+
let headers = formData.getHeaders();
50+
headers.Cookie = dataStore.getCSDNCookies();
51+
headers["user-agent"] = "Mozilla/5.0";
52+
headers.Referer = "https://editor.csdn.net/";
53+
headers.ContentType = 'multipart/form-data';
54+
headers.Accept = 'application/json';
55+
56+
// post formData to host
57+
fetch(url, {
58+
method: 'POST',
59+
body: formData,
60+
headers: headers
61+
}).then(result => result.json())
62+
.then(result => {
63+
if (result.code === 200) {
64+
resolve(result.data.imageUrl)
65+
} else {
66+
reject('上传图片失败,' + result.msg)
67+
}
68+
})
69+
.catch(error => {
70+
reject('上传图片失败,' + error)
71+
})
72+
} else {
73+
reject('上传图片失败,' + result.msg)
74+
}
75+
})
76+
.catch(error => {
77+
reject('上传图片失败,' + error)
78+
})
5379
})
5480
}
5581

@@ -120,4 +146,4 @@ function publishArticleToCSDN(title, markdowncontent, content, isPublish) {
120146
}
121147

122148
exports.uploadPictureToCSDN = uploadPictureToCSDN;
123-
exports.publishArticleToCSDN = publishArticleToCSDN;
149+
exports.publishArticleToCSDN = publishArticleToCSDN;

src/common/app-save.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports.saveNewFileOrClipboard = function saveNewFileOrClipboard(file, content,
2020
numTwo = dialog.showMessageBoxSync({message: '保存成功,是否打开新文档?', buttons: ['不了,谢谢', '打开']})
2121
}
2222
if (numTwo === 1) {
23-
shell.openItem(file.filepath)
23+
shell.openPath(file.filepath)
2424
}
2525
} else if (numOne === 1) {
2626
// 3.写入剪贴板
@@ -45,12 +45,12 @@ exports.appendFileOrClipboard = function appendFileOrClipboard(file, content, i)
4545
numTwo = dialog.showMessageBoxSync({message: '保存成功,是否打开新文档?', buttons: ['不了,谢谢', '打开']})
4646
}
4747
if (numTwo === 1) {
48-
shell.openItem(file.filepath)
48+
shell.openPath(file.filepath)
4949
}
5050
} else if (numOne === 1) {
5151
// 3.写入剪贴板
5252
while (clipboard.readText() !== content) {
5353
clipboard.writeText(content)
5454
}
5555
}
56-
};
56+
};

src/cookie/app-login.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function getSiteCookie(url, callback) {
1414
// 查询所有与设置的 URL 相关的所有 cookies.
1515
if (url == 'https://www.cnblogs.com/') {
1616
url = '.cnblogs.com'
17+
} else if (url == 'https://blog.csdn.net/') {
18+
url = '.csdn.net'
19+
} else if (url.indexOf('jianshu') != -1) {
20+
url = '.jianshu.com'
21+
} else if (url.indexOf('oschina') != -1) {
22+
url = '.oschina.net'
1723
}
1824
session.defaultSession.cookies.get({ domain: url })
1925
.then((cookies) => {
@@ -128,4 +134,4 @@ const loginJianShu = function (item, focusedWindow, event) {
128134
})
129135
};
130136

131-
exports.loginJianShu = loginJianShu;
137+
exports.loginJianShu = loginJianShu;

src/shortcutkey/app-shortcutkey.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function loadShortcutKey(menu) {
4343
}
4444

4545
function openConfigFile() {
46-
shell.openItem(configPath)
46+
shell.openPath(configPath)
4747
}
4848

4949
function openHelpFile() {
@@ -53,9 +53,9 @@ function openHelpFile() {
5353
}
5454
fs.writeFileSync(helpFile, fs.readFileSync(Path.join(__dirname, Path.basename(helpFile))))
5555
}
56-
shell.openItem(helpFile)
56+
shell.openPath(helpFile)
5757
}
5858

5959
exports.openHelpFile = openHelpFile;
6060
exports.openConfigFile = openConfigFile;
61-
exports.loadShortcutKey = loadShortcutKey;
61+
exports.loadShortcutKey = loadShortcutKey;

0 commit comments

Comments
 (0)