Skip to content

Commit 70f6529

Browse files
committed
✨ Feature: support buffer image upload
1 parent 54c05be commit 70f6529

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/plugins/transformer/path.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import {
55
getURLFile
66
} from '../../utils/common'
77
import { IPicGo, IPathTransformedImgInfo, IImgInfo, IImgSize } from '../../types'
8+
import dayjs from 'dayjs'
89

910
const handle = async (ctx: IPicGo): Promise<IPicGo> => {
1011
const results: IImgInfo[] = ctx.output
11-
await Promise.all(ctx.input.map(async (item: string, index: number) => {
12+
await Promise.all(ctx.input.map(async (item: string | Buffer, index: number) => {
1213
let info: IPathTransformedImgInfo
1314
if (Buffer.isBuffer(item)) {
1415
info = {
1516
success: true,
1617
buffer: item,
17-
fileName: 'temporary.png',
18-
extname: '.png'
18+
fileName: '', // will use getImageSize result
19+
extname: '' // will use getImageSize result
1920
}
2021
} else if (isUrl(item)) {
2122
info = await getURLFile(item, ctx)
@@ -24,26 +25,31 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
2425
}
2526
if (info.success && info.buffer) {
2627
const imgSize = getImgSize(ctx, info.buffer, item)
28+
const extname = info.extname || imgSize.extname || '.png'
2729
results[index] = {
2830
buffer: info.buffer,
29-
fileName: info.fileName,
31+
fileName: info.fileName || `${dayjs().format('YYYYMMDDHHmmss')}${extname}}`,
3032
width: imgSize.width,
3133
height: imgSize.height,
32-
extname: info.extname
34+
extname
3335
}
3436
} else {
35-
throw new Error(info.reason)
37+
ctx.log.error(info.reason)
3638
}
3739
}))
3840
// remove empty item
3941
ctx.output = results.filter(item => item)
4042
return ctx
4143
}
4244

43-
const getImgSize = (ctx: IPicGo, file: Buffer, path: string): IImgSize => {
45+
const getImgSize = (ctx: IPicGo, file: Buffer, path: string | Buffer): IImgSize => {
4446
const imageSize = getImageSize(file)
4547
if (!imageSize.real) {
46-
ctx.log.warn(`can't get ${path}'s image size`)
48+
if (typeof path === 'string') {
49+
ctx.log.warn(`can't get ${path}'s image size`)
50+
} else {
51+
ctx.log.warn('can\'t get image size')
52+
}
4753
ctx.log.warn('fallback to 200 * 200')
4854
}
4955
return imageSize

src/plugins/uploader/tcyun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
277277
required: false,
278278
get confirmText () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_CONFIRM') },
279279
get cancelText () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_CANCEL') },
280-
get tips () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_TIP') },
280+
get tips () { return ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD_SLIM_TIP') }
281281
}
282282
]
283283
return config

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ export interface IImgSize {
511511
width: number
512512
height: number
513513
real?: boolean
514+
extname?: string
514515
}
515516

516517
/**

src/utils/common.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ export const handleUrlEncode = (url: string): string => {
3030

3131
export const getImageSize = (file: Buffer): IImgSize => {
3232
try {
33-
const { width = 0, height = 0 } = imageSize(file)
33+
const { width = 0, height = 0, type } = imageSize(file)
34+
const extname = type ? `.${type}` : '.png'
3435
return {
3536
real: true,
3637
width,
37-
height
38+
height,
39+
extname
3840
}
3941
} catch (e) {
4042
// fallback to 200 * 200
4143
return {
4244
real: false,
4345
width: 200,
44-
height: 200
46+
height: 200,
47+
extname: '.png'
4548
}
4649
}
4750
}

0 commit comments

Comments
 (0)