piclist 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/picgo-server +109 -74
- package/dist/index.cjs.js +2 -2
- package/dist/index.esm.js +2 -2
- package/dist/utils/common.d.ts +3 -1
- package/package.json +1 -1
package/bin/picgo-server
CHANGED
|
@@ -68,6 +68,7 @@ if (configPath !== true && configPath !== '') {
|
|
|
68
68
|
} else {
|
|
69
69
|
configPath = ''
|
|
70
70
|
}
|
|
71
|
+
|
|
71
72
|
const { PicGo } = require('..')
|
|
72
73
|
const picgo = new PicGo(configPath)
|
|
73
74
|
const errorMessage = 'Upload failed, please check your network and config'
|
|
@@ -102,20 +103,34 @@ class Router {
|
|
|
102
103
|
this.router = new Map()
|
|
103
104
|
}
|
|
104
105
|
|
|
105
|
-
|
|
106
|
-
this.router.
|
|
106
|
+
addRoute (method, url, callback, urlparams) {
|
|
107
|
+
if (!this.router.has(url)) {
|
|
108
|
+
this.router.set(url, new Map())
|
|
109
|
+
}
|
|
110
|
+
this.router.get(url).set(method, { handler: callback, urlparams })
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get (url, callback, urlparams) {
|
|
114
|
+
this.addRoute('GET', url, callback, urlparams)
|
|
107
115
|
}
|
|
108
116
|
|
|
109
117
|
post(url, callback, urlparams) {
|
|
110
|
-
this.
|
|
118
|
+
this.addRoute('POST', url, callback, urlparams)
|
|
111
119
|
}
|
|
112
120
|
|
|
113
|
-
|
|
121
|
+
any(url, callback, urlparams) {
|
|
122
|
+
this.addRoute('GET', url, callback, urlparams)
|
|
123
|
+
this.addRoute('POST', url, callback, urlparams)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
getHandler (url, method) {
|
|
114
127
|
if (this.router.has(url)) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
128
|
+
const methods = this.router.get(url)
|
|
129
|
+
if (methods.has(method)) {
|
|
130
|
+
return methods.get(method)
|
|
131
|
+
}
|
|
118
132
|
}
|
|
133
|
+
return null
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
136
|
|
|
@@ -142,6 +157,9 @@ const uploadMulter = multer({
|
|
|
142
157
|
storage: multerStorage
|
|
143
158
|
})
|
|
144
159
|
|
|
160
|
+
router.get('/', responseForGet)
|
|
161
|
+
router.get('/upload', responseForGet)
|
|
162
|
+
|
|
145
163
|
router.post('/upload', async ({ response, list = [], urlparams }) => {
|
|
146
164
|
try {
|
|
147
165
|
const picbed = urlparams?.get('picbed')
|
|
@@ -243,7 +261,72 @@ router.post('/upload', async ({ response, list = [], urlparams }) => {
|
|
|
243
261
|
}
|
|
244
262
|
})
|
|
245
263
|
|
|
246
|
-
|
|
264
|
+
async function responseForGet ({ response }) {
|
|
265
|
+
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
|
|
266
|
+
response.write(`
|
|
267
|
+
<!DOCTYPE html>
|
|
268
|
+
<html lang="en">
|
|
269
|
+
<head>
|
|
270
|
+
<meta charset="UTF-8">
|
|
271
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
272
|
+
<title>Picgo-Server Usage</title>
|
|
273
|
+
<style>
|
|
274
|
+
body { font-family: Arial, sans-serif; margin: 20px; }
|
|
275
|
+
pre { background-color: #f4f4f4; padding: 10px; }
|
|
276
|
+
.tip { padding: 10px; background-color: #f0f9ff; border-left: 5px solid #2196F3; margin-bottom: 20px; }
|
|
277
|
+
</style>
|
|
278
|
+
</head>
|
|
279
|
+
<body>
|
|
280
|
+
<h2>picgo-server的使用</h2>
|
|
281
|
+
<p><code>picgo-server</code>命令用于启动web服务,接收来自其他应用或其他主机的HTTP请求来上传图片。</p>
|
|
282
|
+
<p>默认监听地址:<code>0.0.0.0</code>,默认监听端口:<code>36677</code></p>
|
|
283
|
+
|
|
284
|
+
<h3>接口鉴权</h3>
|
|
285
|
+
<p>当将接口暴露于公网时,为了防止恶意上传,可以使用接口鉴权功能。通过在运行<code>picgo-server</code>时添加<code>-k</code>或<code>--key</code>参数来设置一个密钥。</p>
|
|
286
|
+
<p>发送请求时添加URL查询参数<code>key</code>即可,例如:<code>http://xxx:36677/upload?key=xxx</code>。</p>
|
|
287
|
+
|
|
288
|
+
<h3>表单上传</h3>
|
|
289
|
+
<ul>
|
|
290
|
+
<li>请求方法: <code>POST</code></li>
|
|
291
|
+
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
292
|
+
<li>请求body: <code>multipart/form-data</code>格式,key任选,value为图片文件</li>
|
|
293
|
+
</ul>
|
|
294
|
+
|
|
295
|
+
<h3>HTTP调用上传剪贴板图片</h3>
|
|
296
|
+
<ul>
|
|
297
|
+
<li>请求方法: <code>POST</code></li>
|
|
298
|
+
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
299
|
+
<li>请求body: <code>{list: ['xxx.jpg']}</code> 必须是JSON格式</li>
|
|
300
|
+
</ul>
|
|
301
|
+
|
|
302
|
+
<div class="tip">Tip: PicList支持通过设置<code>picbed</code>和<code>configName</code>两个URL查询参数来指定上传图床和配置文件。例如:<code>http://127.0.0.1:36677/upload?picbed=aws-s3&configName=piclist-test</code> 该配置将会使用<code>aws-s3</code>图床,并且使用<code>piclist-test</code>配置文件。</div>
|
|
303
|
+
|
|
304
|
+
<p>返回的数据:</p>
|
|
305
|
+
<pre>{
|
|
306
|
+
"success": true, // or false
|
|
307
|
+
"result": ["url"]
|
|
308
|
+
}</pre>
|
|
309
|
+
|
|
310
|
+
<h3>HTTP调用上传具体路径图片</h3>
|
|
311
|
+
<ul>
|
|
312
|
+
<li>method: <code>POST</code></li>
|
|
313
|
+
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
314
|
+
<li>request body: <code>{list: ['xxx.jpg']}</code> 必须是JSON格式</li>
|
|
315
|
+
</ul>
|
|
316
|
+
|
|
317
|
+
<p>返回的数据:</p>
|
|
318
|
+
<pre>{
|
|
319
|
+
"success": true, // or false
|
|
320
|
+
"result": ["url"]
|
|
321
|
+
}</pre>
|
|
322
|
+
|
|
323
|
+
</body>
|
|
324
|
+
</html>
|
|
325
|
+
`)
|
|
326
|
+
response.end()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
router.any('/heartbeat', async ({ response }) => {
|
|
247
330
|
handleResponse({
|
|
248
331
|
response,
|
|
249
332
|
body: {
|
|
@@ -266,8 +349,8 @@ class Server {
|
|
|
266
349
|
|
|
267
350
|
if (request.method === 'POST') {
|
|
268
351
|
const [url, query] = (request.url || '').split('?')
|
|
269
|
-
if (!router.getHandler(url)) {
|
|
270
|
-
console.log(`[PicList Server] don't support [${url}]
|
|
352
|
+
if (!router.getHandler(url, 'POST')) {
|
|
353
|
+
console.log(`[PicList Server] don't support [${url}] endpoint`)
|
|
271
354
|
handleResponse({
|
|
272
355
|
response,
|
|
273
356
|
statusCode: 404,
|
|
@@ -291,7 +374,7 @@ class Server {
|
|
|
291
374
|
|
|
292
375
|
const list = request.files.map(file => file.path)
|
|
293
376
|
|
|
294
|
-
const handler = router.getHandler(url)?.handler
|
|
377
|
+
const handler = router.getHandler(url, 'POST')?.handler
|
|
295
378
|
if (handler) {
|
|
296
379
|
handler({
|
|
297
380
|
list: list,
|
|
@@ -320,7 +403,7 @@ class Server {
|
|
|
320
403
|
})
|
|
321
404
|
}
|
|
322
405
|
console.log('[PicList Server] get the request', body)
|
|
323
|
-
const handler = router.getHandler(url)?.handler
|
|
406
|
+
const handler = router.getHandler(url, 'POST')?.handler
|
|
324
407
|
if (handler) {
|
|
325
408
|
handler({
|
|
326
409
|
...postObj,
|
|
@@ -332,68 +415,20 @@ class Server {
|
|
|
332
415
|
}
|
|
333
416
|
}
|
|
334
417
|
} else if (request.method === 'GET') {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
<body>
|
|
350
|
-
<h2>picgo-server的使用</h2>
|
|
351
|
-
<p><code>picgo-server</code>命令用于启动web服务,接收来自其他应用或其他主机的HTTP请求来上传图片。</p>
|
|
352
|
-
<p>默认监听地址:<code>0.0.0.0</code>,默认监听端口:<code>36677</code></p>
|
|
353
|
-
|
|
354
|
-
<h3>接口鉴权</h3>
|
|
355
|
-
<p>当将接口暴露于公网时,为了防止恶意上传,可以使用接口鉴权功能。通过在运行<code>picgo-server</code>时添加<code>-k</code>或<code>--key</code>参数来设置一个密钥。</p>
|
|
356
|
-
<p>发送请求时添加URL查询参数<code>key</code>即可,例如:<code>http://xxx:36677/upload?key=xxx</code>。</p>
|
|
357
|
-
|
|
358
|
-
<h3>表单上传</h3>
|
|
359
|
-
<ul>
|
|
360
|
-
<li>请求方法: <code>POST</code></li>
|
|
361
|
-
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
362
|
-
<li>请求body: <code>multipart/form-data</code>格式,key任选,value为图片文件</li>
|
|
363
|
-
</ul>
|
|
364
|
-
|
|
365
|
-
<h3>HTTP调用上传剪贴板图片</h3>
|
|
366
|
-
<ul>
|
|
367
|
-
<li>请求方法: <code>POST</code></li>
|
|
368
|
-
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
369
|
-
<li>请求body: <code>{list: ['xxx.jpg']}</code> 必须是JSON格式</li>
|
|
370
|
-
</ul>
|
|
371
|
-
|
|
372
|
-
<div class="tip">Tip: PicList支持通过设置<code>picbed</code>和<code>configName</code>两个URL查询参数来指定上传图床和配置文件。例如:<code>http://127.0.0.1:36677/upload?picbed=aws-s3&configName=piclist-test</code> 该配置将会使用<code>aws-s3</code>图床,并且使用<code>piclist-test</code>配置文件。</div>
|
|
373
|
-
|
|
374
|
-
<p>返回的数据:</p>
|
|
375
|
-
<pre>{
|
|
376
|
-
"success": true, // or false
|
|
377
|
-
"result": ["url"]
|
|
378
|
-
}</pre>
|
|
379
|
-
|
|
380
|
-
<h3>HTTP调用上传具体路径图片</h3>
|
|
381
|
-
<ul>
|
|
382
|
-
<li>method: <code>POST</code></li>
|
|
383
|
-
<li>url: <code>http://127.0.0.1:36677/upload</code> (此处以默认配置为例)</li>
|
|
384
|
-
<li>request body: <code>{list: ['xxx.jpg']}</code> 必须是JSON格式</li>
|
|
385
|
-
</ul>
|
|
386
|
-
|
|
387
|
-
<p>返回的数据:</p>
|
|
388
|
-
<pre>{
|
|
389
|
-
"success": true, // or false
|
|
390
|
-
"result": ["url"]
|
|
391
|
-
}</pre>
|
|
392
|
-
|
|
393
|
-
</body>
|
|
394
|
-
</html>
|
|
395
|
-
`)
|
|
396
|
-
response.end()
|
|
418
|
+
const [url, query] = (request.url || '').split('?')
|
|
419
|
+
if (!router.getHandler(url, 'GET')) {
|
|
420
|
+
console.log(`[PicList Server] don't support [${url}] endpoint`)
|
|
421
|
+
response.statusCode = 404
|
|
422
|
+
response.end()
|
|
423
|
+
} else {
|
|
424
|
+
const handler = router.getHandler(url, 'GET')?.handler
|
|
425
|
+
if (handler) {
|
|
426
|
+
handler({
|
|
427
|
+
response,
|
|
428
|
+
urlparams: query ? new URLSearchParams(query) : undefined
|
|
429
|
+
})
|
|
430
|
+
}
|
|
431
|
+
}
|
|
397
432
|
} else {
|
|
398
433
|
console.log(`[PicList Server] don't support [${request.method}] method`)
|
|
399
434
|
response.statusCode = 405
|