Skip to content

Commit

Permalink
添加PicList图床支持 (#777)
Browse files Browse the repository at this point in the history
* 添加PicList图床支持

* 更新PicList相关配置项

* 移除不必要的注释

* 删除失误上传的调试信息

* Update src/client/utils/i18n/i18n.js

Co-authored-by: iMaeGoo <mail1st@qq.com>

* 移除isLskyProUrl和isPicListUrl函数

---------

Co-authored-by: iMaeGoo <mail1st@qq.com>
  • Loading branch information
duzhuoshanwai and imaegoo authored Jan 6, 2025
1 parent e2c696b commit 6378449
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
21 changes: 18 additions & 3 deletions src/client/utils/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ const highlightPlugins = [

const imageBedServices = [
'qcloud',
'7bu',
'https://7bu.top',
'smms'
'7bu (https://7bu.top)',
'smms (https://sm.ms)',
'lskypro',
'piclist'
].map(s => `"${s}"`)

const customImageBedServices = [
'lskypro',
'piclist'
].map(s => `"${s}"`)

const defaultGravatar = [
Expand Down Expand Up @@ -476,6 +482,15 @@ export default {
`Расм юклаш учун расм тўшаги. Қуйидагилардан танланг: ${imageBedServices.join(', ')}`,
`画像のアップロードに使用する画像ホスティングサービス。次のうちから選択してください:${imageBedServices.join('、')}`
],
// 翻译来自 Deepseek
[S.ACI + '_IMAGE_CDN_URL']: [
`IMAGE_CDN 设置的图床的 URL,如果你的 IMAGE_CDN 是这些:${customImageBedServices.join('、')} 需要填写`, // 简体中文
`IMAGE_CDN 設定的圖床的URL, 如果你的IMAGE_CDN是這些: ${customImageBedServices.join('、')} 需要填寫`, // 繁体中文
`IMAGE_CDN 設定的圖床的URL, 如果你的IMAGE_CDN是這些: ${customImageBedServices.join('、')} 需要填寫`, // 繁体中文(台湾)
`The URL for the image bed set by IMAGE_CDN. Required if your IMAGE_CDN is one of these: ${customImageBedServices.join(', ')}`, // 英文
`IMAGE_CDN томонидан белгиланган расм тўшаги URL. Агар сизнинг IMAGE_CDN шулардан бири бўлса: ${customImageBedServices.join(', ')}`, // 乌兹别克语
`IMAGE_CDNで設定した画像ホスティングサービスのURL。IMAGE_CDNが以下のいずれかの場合は入力が必要です:${customImageBedServices.join('、')}` // 日语
],
[S.ACI + '_IMAGE_CDN_TOKEN']: [
'图床 token。qcloud 图床无需设置',
'图床 token。qcloud 图床无需设置',
Expand Down
1 change: 1 addition & 0 deletions src/client/view/components/TkAdminConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default {
items: [
{ key: 'SHOW_IMAGE', desc: t('ADMIN_CONFIG_ITEM_SHOW_IMAGE'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}false`, value: '' },
{ key: 'IMAGE_CDN', desc: t('ADMIN_CONFIG_ITEM_IMAGE_CDN'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}qcloud`, value: '' },
{ key: 'IMAGE_CDN_URL', desc: t('ADMIN_CONFIG_ITEM_IMAGE_CDN_URL'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}https://piclist.example.com`, value: '' },
{ key: 'IMAGE_CDN_TOKEN', desc: t('ADMIN_CONFIG_ITEM_IMAGE_CDN_TOKEN'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}example`, value: '' },
{ key: 'SHOW_EMOTION', desc: t('ADMIN_CONFIG_ITEM_SHOW_EMOTION'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}false`, value: '' },
{ key: 'EMOTION_CDN', desc: t('ADMIN_CONFIG_ITEM_EMOTION_CDN'), ph: '', value: '' },
Expand Down
44 changes: 35 additions & 9 deletions src/server/function/twikoo/utils/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs')
const os = require('os')
const path = require('path')
const { isUrl } = require('.')
const { RES_CODE } = require('./constants')
const { getAxios, getFormData } = require('./lib')
const axios = getAxios()
Expand All @@ -17,12 +16,21 @@ const fn = {
throw new Error('未配置图片上传服务')
}
// tip: qcloud 图床走前端上传,其他图床走后端上传
if (config.IMAGE_CDN === '7bu') {
await fn.uploadImageToLskyPro({ photo, fileName, config, res, imageCdn: 'https://7bu.top' })
} else if (config.IMAGE_CDN === 'smms') {
await fn.uploadImageToSmms({ photo, fileName, config, res })
} else if (isUrl(config.IMAGE_CDN)) {
await fn.uploadImageToLskyPro({ photo, fileName, config, res, imageCdn: config.IMAGE_CDN })
switch (config.IMAGE_CDN) {
case '7bu':
await fn.uploadImageToLskyPro({ photo, fileName, config, res, imageCdn: 'https://7bu.top' })
break
case 'smms':
await fn.uploadImageToSmms({ photo, fileName, config, res, imageCdn: 'https://smms.app/api/v2/upload' })
break
case 'lskypro':
await fn.uploadImageToLskyPro({ photo, fileName, config, res, imageCdn: config.IMAGE_CDN_URL })
break
case 'piclist':
await fn.uploadImageToPicList({ photo, fileName, config, res, imageCdn: config.IMAGE_CDN_URL })
break
default:
throw new Error('不支持的图片上传服务')
}
} catch (e) {
logger.error(e)
Expand All @@ -31,11 +39,11 @@ const fn = {
}
return res
},
async uploadImageToSmms ({ photo, fileName, config, res }) {
async uploadImageToSmms ({ photo, fileName, config, res, imageCdn }) {
// SM.MS 图床 https://sm.ms
const formData = new FormData()
formData.append('smfile', fn.base64UrlToReadStream(photo, fileName))
const uploadResult = await axios.post('https://smms.app/api/v2/upload', formData, {
const uploadResult = await axios.post(imageCdn, formData, {
headers: {
...formData.getHeaders(),
Authorization: config.IMAGE_CDN_TOKEN
Expand Down Expand Up @@ -72,6 +80,24 @@ const fn = {
throw new Error(uploadResult.data.message)
}
},
async uploadImageToPicList ({ photo, fileName, config, res, imageCdn }) {
// PicList https://piclist.cn/ 高效的云存储和图床平台管理工具
// 鉴权使用query参数key
const formData = new FormData()
formData.append('file', fn.base64UrlToReadStream(photo, fileName))
let url = `${imageCdn}/upload`
// 如果填写了key则拼接url
if (config.IMAGE_CDN_TOKEN) {
url += `?key=${config.IMAGE_CDN_TOKEN}`
}
const uploadResult = await axios.post(url, formData)
if (uploadResult.data.success) {
res.data = uploadResult.data
res.data.url = uploadResult.data.result[0]
} else {
throw new Error(uploadResult.data.message)
}
},
base64UrlToReadStream (base64Url, fileName) {
const base64 = base64Url.split(';base64,').pop()
const writePath = path.resolve(os.tmpdir(), fileName)
Expand Down
3 changes: 0 additions & 3 deletions src/server/function/twikoo/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ const fn = {
return `https://${gravatarCdn}/avatar/${mailHash}?d=${defaultGravatar}`
}
},
isUrl (s) {
return /^http(s)?:\/\//.test(s)
},
isQQ (mail) {
return /^[1-9][0-9]{4,10}$/.test(mail) ||
/^[1-9][0-9]{4,10}@qq.com$/i.test(mail)
Expand Down

0 comments on commit 6378449

Please sign in to comment.