twikoo-vercel 1.5.3 → 1.5.4

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.
Files changed (2) hide show
  1. package/api/index.js +73 -16
  2. package/package.json +1 -1
package/api/index.js CHANGED
@@ -1403,9 +1403,9 @@ async function emailTest (event) {
1403
1403
  const isAdminUser = await isAdmin()
1404
1404
  if (isAdminUser) {
1405
1405
  try {
1406
- if (!transporter) {
1407
- await initMailer({ throwErr: true })
1408
- }
1406
+ // 邮件测试前清除 transporter,保证读取的是最新的配置
1407
+ transporter = null
1408
+ await initMailer({ throwErr: true })
1409
1409
  const sendResult = await transporter.sendMail({
1410
1410
  from: config.SENDER_EMAIL,
1411
1411
  to: event.mail || config.BLOGGER_EMAIL || config.SENDER_EMAIL,
@@ -1427,21 +1427,16 @@ async function uploadImage (event) {
1427
1427
  const { photo, fileName } = event
1428
1428
  const res = {}
1429
1429
  try {
1430
- if (!config.IMAGE_CDN_TOKEN) {
1430
+ if (!config.IMAGE_CDN || !config.IMAGE_CDN_TOKEN) {
1431
1431
  throw new Error('未配置图片上传服务')
1432
1432
  }
1433
- const formData = new FormData()
1434
- formData.append('image', base64UrlToReadStream(photo, fileName))
1435
- const uploadResult = await axios.post('https://7bu.top/api/upload', formData, {
1436
- headers: {
1437
- ...formData.getHeaders(),
1438
- token: config.IMAGE_CDN_TOKEN
1439
- }
1440
- })
1441
- if (uploadResult.data.code === 200) {
1442
- res.data = uploadResult.data.data
1443
- } else {
1444
- throw new Error(uploadResult.data.msg)
1433
+ // tip: qcloud 图床走前端上传,其他图床走后端上传
1434
+ if (config.IMAGE_CDN === '7bu') {
1435
+ await uploadImageTo7Bu({ photo, fileName, config, res })
1436
+ } else if (config.IMAGE_CDN === 'smms') {
1437
+ await uploadImageToSmms({ photo, fileName, config, res })
1438
+ } else if (isUrl(config.IMAGE_CDN)) {
1439
+ await uploadImageToLskyPro({ photo, fileName, config, res })
1445
1440
  }
1446
1441
  } catch (e) {
1447
1442
  console.error(e)
@@ -1451,6 +1446,64 @@ async function uploadImage (event) {
1451
1446
  return res
1452
1447
  }
1453
1448
 
1449
+ async function uploadImageTo7Bu ({ photo, fileName, config, res }) {
1450
+ // 去不图床旧版本 https://7bu.top
1451
+ // TODO: 2022 年 4 月 30 日后去不图床将会升级新版本,此处逻辑要同步更新
1452
+ const formData = new FormData()
1453
+ formData.append('image', base64UrlToReadStream(photo, fileName))
1454
+ const uploadResult = await axios.post('https://7bu.top/api/upload', formData, {
1455
+ headers: {
1456
+ ...formData.getHeaders(),
1457
+ token: config.IMAGE_CDN_TOKEN
1458
+ }
1459
+ })
1460
+ if (uploadResult.data.code === 200) {
1461
+ res.data = uploadResult.data.data
1462
+ } else {
1463
+ throw new Error(uploadResult.data.msg)
1464
+ }
1465
+ }
1466
+
1467
+ async function uploadImageToSmms ({ photo, fileName, config, res }) {
1468
+ // SM.MS 图床 https://sm.ms
1469
+ const formData = new FormData()
1470
+ formData.append('smfile', base64UrlToReadStream(photo, fileName))
1471
+ const uploadResult = await axios.post('https://sm.ms/api/v2/upload', formData, {
1472
+ headers: {
1473
+ ...formData.getHeaders(),
1474
+ Authorization: config.IMAGE_CDN_TOKEN
1475
+ }
1476
+ })
1477
+ if (uploadResult.data.success) {
1478
+ res.data = uploadResult.data.data
1479
+ } else {
1480
+ throw new Error(uploadResult.data.message)
1481
+ }
1482
+ }
1483
+
1484
+ async function uploadImageToLskyPro ({ photo, fileName, config, res }) {
1485
+ // 自定义兰空图床(v2)URL
1486
+ const formData = new FormData()
1487
+ formData.append('file', base64UrlToReadStream(photo, fileName))
1488
+ const url = `${config.IMAGE_CDN}/api/v1/upload`
1489
+ let token = config.IMAGE_CDN_TOKEN
1490
+ if (!token.startsWith('Bearer')) {
1491
+ token = `Bearer ${token}`
1492
+ }
1493
+ const uploadResult = await axios.post(url, formData, {
1494
+ headers: {
1495
+ ...formData.getHeaders(),
1496
+ Authorization: token
1497
+ }
1498
+ })
1499
+ if (uploadResult.data.status) {
1500
+ res.data = uploadResult.data.data
1501
+ res.data.url = res.data.links.url
1502
+ } else {
1503
+ throw new Error(uploadResult.data.message)
1504
+ }
1505
+ }
1506
+
1454
1507
  function base64UrlToReadStream (base64Url, fileName) {
1455
1508
  const base64 = base64Url.split(';base64,').pop()
1456
1509
  const path = `/tmp/${fileName}`
@@ -1458,6 +1511,10 @@ function base64UrlToReadStream (base64Url, fileName) {
1458
1511
  return fs.createReadStream(path)
1459
1512
  }
1460
1513
 
1514
+ function isUrl (s) {
1515
+ return /^http(s)?:\/\//.test(s)
1516
+ }
1517
+
1461
1518
  function getAvatar (comment) {
1462
1519
  if (comment.avatar) {
1463
1520
  return comment.avatar
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "description": "A simple comment system based on Tencent CloudBase (tcb).",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",