piclist 1.8.1 → 1.8.3

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 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
- get(url, callback, urlparams) {
106
- this.router.set(url, { handler: callback, urlparams })
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.router.set(url, { handler: callback, urlparams })
118
+ this.addRoute('POST', url, callback, urlparams)
111
119
  }
112
120
 
113
- getHandler(url) {
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
- return this.router.get(url)
116
- } else {
117
- return null
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
- router.post('/heartbeat', async ({ response }) => {
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}] 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,
@@ -276,6 +359,15 @@ class Server {
276
359
  }
277
360
  })
278
361
  } else {
362
+ let urlSP = query ? new URLSearchParams(query) : undefined
363
+ console.log('[PicList Server] get the request from IP:', request.socket.remoteAddress)
364
+ if (request.socket.remoteAddress === '127.0.0.1' || request.socket.remoteAddress === '::1') {
365
+ if (urlSP) {
366
+ urlSP.set('key', key)
367
+ } else {
368
+ urlSP = new URLSearchParams('key=' + key)
369
+ }
370
+ }
279
371
  if (request.headers['content-type'] && request.headers['content-type'].startsWith('multipart/form-data')) {
280
372
  uploadMulter.any()(request, response, err => {
281
373
  if (err) {
@@ -291,12 +383,12 @@ class Server {
291
383
 
292
384
  const list = request.files.map(file => file.path)
293
385
 
294
- const handler = router.getHandler(url)?.handler
386
+ const handler = router.getHandler(url, 'POST')?.handler
295
387
  if (handler) {
296
388
  handler({
297
389
  list: list,
298
390
  response,
299
- urlparams: query ? new URLSearchParams(query) : undefined
391
+ urlparams:urlSP
300
392
  })
301
393
  }
302
394
  })
@@ -320,80 +412,32 @@ class Server {
320
412
  })
321
413
  }
322
414
  console.log('[PicList Server] get the request', body)
323
- const handler = router.getHandler(url)?.handler
415
+ const handler = router.getHandler(url, 'POST')?.handler
324
416
  if (handler) {
325
417
  handler({
326
418
  ...postObj,
327
419
  response,
328
- urlparams: query ? new URLSearchParams(query) : undefined
420
+ urlparams: urlSP
329
421
  })
330
422
  }
331
423
  })
332
424
  }
333
425
  }
334
426
  } else if (request.method === 'GET') {
335
- response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
336
- response.write(`
337
- <!DOCTYPE html>
338
- <html lang="en">
339
- <head>
340
- <meta charset="UTF-8">
341
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
342
- <title>Picgo-Server Usage</title>
343
- <style>
344
- body { font-family: Arial, sans-serif; margin: 20px; }
345
- pre { background-color: #f4f4f4; padding: 10px; }
346
- .tip { padding: 10px; background-color: #f0f9ff; border-left: 5px solid #2196F3; margin-bottom: 20px; }
347
- </style>
348
- </head>
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()
427
+ const [url, query] = (request.url || '').split('?')
428
+ if (!router.getHandler(url, 'GET')) {
429
+ console.log(`[PicList Server] don't support [${url}] endpoint`)
430
+ response.statusCode = 404
431
+ response.end()
432
+ } else {
433
+ const handler = router.getHandler(url, 'GET')?.handler
434
+ if (handler) {
435
+ handler({
436
+ response,
437
+ urlparams: query ? new URLSearchParams(query) : undefined
438
+ })
439
+ }
440
+ }
397
441
  } else {
398
442
  console.log(`[PicList Server] don't support [${request.method}] method`)
399
443
  response.statusCode = 405