Skip to content

Commit

Permalink
fix(feishu): 修复飞书下载并发限制
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Jul 6, 2024
1 parent afd6f7c commit 3288661
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
4 changes: 3 additions & 1 deletion packages/plugin-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"cos-nodejs-sdk-v5": "^2.11.16",
"mkdirp": "^1.0.4",
"qiniu": "^7.8.0",
"upyun": "^3.4.6"
"upyun": "^3.4.6",
"tiny-async-pool": "^1.3.0"
},
"devDependencies": {
"@types/tiny-async-pool": "^1.0.1",
"@types/ali-oss": "^6.16.6",
"@types/mime-types": "^2.1.1",
"@types/mkdirp": "^1.0.2"
Expand Down
86 changes: 44 additions & 42 deletions packages/plugin-image/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { DocDetail } from '@elog/types'
import { ImagePlatformEnum } from './platform/const'
import { ImageSource, ImageUrl } from './types'
import asyncPool from 'tiny-async-pool'

class ImageUploader {
config: ImageConfig
Expand Down Expand Up @@ -155,7 +156,6 @@ class ImageUploader {
* 从飞书下载图片
* @param articleList
* @param feishuClient
* @param doc
*/

async replaceImagesFromFeiShu(articleList: DocDetail[], feishuClient: any) {
Expand All @@ -180,57 +180,59 @@ class ImageUploader {
}
return articleList
}

/**
* 从飞书上传图片
* @param urlList
* @param feishuClient
* @param doc
* @param failBack
*/
async uploadFromFeiShu(
urlList: ImageUrl[],
feishuClient: any,
doc: DocDetail,
failBack?: (image: ImageUrl) => void,
) {
const toUploadURLs = urlList.map(async (image) => {
return await new Promise<ImageSource | undefined>(async (resolve) => {
try {
// 从飞书下载图片
const res = await feishuClient.getResourceItem(image.url)
// 完整文件名
const fullName = res.name
// out.info('处理图片', `生成文件名: ${fullName}`)
// 检查图床是否存在该文件
let exist = await this.ctx.hasImage(fullName)
if (exist) {
out.info('忽略上传', `图片已存在: ${exist}`)
// 图片已存在
resolve({
fileName: fullName,
original: image.original,
url: exist,
upload: false,
})
} else {
const buffer = res.buffer
if (!buffer) {
failBack?.(image)
resolve(undefined)
return
}
// 上传图片
resolve({
buffer,
fileName: fullName,
original: image.original,
upload: true,
})
const toUploadURLs: ImageSource[] = []
const promise = async (image: ImageUrl) => {
try {
// 从飞书下载图片
const res = await feishuClient.getResourceItem(image.url)
// 完整文件名
const fullName = res.name
// out.info('处理图片', `生成文件名: ${fullName}`)
// 检查图床是否存在该文件
let exist = await this.ctx.hasImage(fullName)
if (exist) {
out.info('忽略上传', `图片已存在: ${exist}`)
// 图片已存在
toUploadURLs.push({
fileName: fullName,
original: image.original,
url: exist,
upload: false,
})
} else {
const buffer = res.buffer
if (!buffer) {
failBack?.(image)
return
}
} catch (err: any) {
resolve(undefined)
// 上传图片
toUploadURLs.push({
buffer,
fileName: fullName,
original: image.original,
upload: true,
})
}
})
})
const toUploadImgs = (await Promise.all(toUploadURLs).then((imgs) =>
imgs.filter((img) => img !== undefined),
)) as ImageSource[]
} catch (err: any) {}
}
await asyncPool(this.config.limit || 3, urlList, promise)
let output: ImageUrl[] = []

for (const img of toUploadImgs) {
for (const img of toUploadURLs) {
let newUrl: string | undefined = ''
if (img.upload) {
newUrl = await this.ctx.uploadImg(img.buffer!, img.fileName, doc)
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-image/src/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ export type ImageConfig = {
/** 不开启图床使用图床插件 */
enableForExt?: string
plugin?: ImagePlugin
/** 下载并发数 */
limit?: number
[key: string]: any
} & ImagePlatformConfig

0 comments on commit 3288661

Please sign in to comment.