-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
160 lines (156 loc) · 5.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// const logger = require('@varnxy/logger')
// logger.setDirectory('/Users/zhang/Work/WorkSpaces/WebWorkSpace/picgo-plugin-web-uploader/logs')
let crypto = require('crypto');
module.exports = (ctx) => {
const register = () => {
ctx.helper.uploader.register('onemanager', {
async handle(ctx) {
let userConfig = ctx.getConfig('picBed.onemanager');
if (!userConfig) {
throw new Error('Can\'t find uploader config')
}
let url = userConfig.url
if (!url) {
ctx.emit('notification', {
title: '上传失败',
body: "请填写onemanager的图床url"
});
}
if (url.charAt(url.length - 1) != "/") {
url = url + "/";
}
let imgList = ctx.output
for (let i in imgList) {
const uploadInfo = await getUploadInfo(ctx, url, imgList[i]);
if (imgList[i].url) continue;
await upload(ctx, uploadInfo.uploadUrl, imgList[i], uploadInfo.imgUrl);
}
return ctx;
},
name: 'onemanager图床',
config: config
})
}
return {
register,
uploader: 'onemanager',
}
}
const upload = async (ctx, url, img, imgUrl) => {
const totalsize = img.buffer.length;
const asize = 0;
const bsize = totalsize - 1;
const customHeader = {
"content-length": totalsize,
"Content-Range": 'bytes ' + asize + '-' + bsize + '/' + totalsize/*,"contentType":"base64Encoded"*/
}
const upOpts = getOpts(customHeader, img.buffer, url, "PUT");
upOpts.body = img.buffer;
try {
let resp = await ctx.request(upOpts);
resp = JSON.parse(resp);
if (resp.size > 0) {
delete img.base64Image
delete img.buffer
img.url = imgUrl;
img.imgUrl = imgUrl;
}
} catch (e) {
return ctx.emit('notification', {
title: '上传发生错误',
body: JSON.stringify(e)
});
}
}
const getUploadInfo = async (ctx, url, img) => {
let md5 = crypto.createHash("md5");
// 获取图片上传url
let customHeader = {'x-requested-with': 'XMLHttpRequest'};
md5.update(img.buffer);
md5 = md5.digest("hex");
let filesize = img.buffer.length;
let getUpload = {
upbigfilename: md5 + img.extname,
filesize: filesize,
filelastModified: Date.now(),
filemd5: md5
}
let getUrlOpts = getOpts(customHeader, getUpload, url + "?action=upbigfile", "POST", true);
let resp;
try {
resp = await ctx.request(getUrlOpts);
} catch (e) {
if (JSON.stringify(e).indexOf("exists") != -1) {
ctx.emit('notification', {
title: '图片已存在',
body: img.fileName + " 已存在!"
});
delete img.base64Image
delete img.buffer
img.url = url + getUpload.upbigfilename;
img.imgUrl = url + getUpload.upbigfilename;
return null;
} else {
if (JSON.stringify(e).indexOf("exists") != -1) {
ctx.emit('notification', {
title: '上传失败',
body: JSON.stringify(e)
});
ctx.log.warn("上传失败", JSON.stringify(e));
}
}
}
resp = JSON.parse(resp);
resp.imgName = getUpload.upbigfilename;
resp.imgUrl = url + getUpload.upbigfilename;
return resp;
}
const getOpts = (customHeader = {}, body, url = "", method = "POST", toStrBody = false) => {
if (toStrBody) {
try {
let stringBody = "";
for (let key in body) {
stringBody += (key + "=" + body[key] + "&");
}
body = encodeURI(stringBody.substr(0, stringBody.length - 1));
} catch (e) {
// TO DO...
}
} else {
try {
body = JSON.stringify(body);
} catch (e) {
// TO DO
}
}
let headers = {
contentType: 'multipart/form-data',
'User-Agent': 'PicGo'
}
if (customHeader) {
headers = Object.assign(headers, customHeader)
}
const opts = {
method: method,
url: url,
headers: headers,
body: body
}
return opts
}
const config = ctx => {
let userConfig = ctx.getConfig('picBed.onemanager')
if (!userConfig) {
userConfig = {}
}
return [
{
name: 'url',
type: 'input',
default: userConfig.url,
required: true,
message: '示例:https://pan.laoxin.top/od1/ykfile/ 其中ykfile为图床文件夹',
alias: 'onemanager图床文件夹url'
}
]
}