/
aliyun-node.ts
73 lines (65 loc) · 2.27 KB
/
aliyun-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// noinspection ES6PreferShortImport
import { IAliyunConfig, IPicGo } from "../../types"
import { IBuildInEvent } from "../../utils/enums"
import { ILocalesKey } from "../../i18n/zh-CN"
import { base64ToBuffer } from "../../utils/common"
import OSS from "ali-oss"
const handleNode = async (ctx: IPicGo): Promise<IPicGo> => {
const aliYunOptions = ctx.getConfig<IAliyunConfig>("picBed.aliyun")
if (!aliYunOptions) {
throw new Error("Can't find aliYun OSS config")
}
const store = new OSS({
region: aliYunOptions.area,
accessKeyId: aliYunOptions.accessKeyId,
accessKeySecret: aliYunOptions.accessKeySecret,
bucket: aliYunOptions.bucket,
})
const imgList = ctx.output
const customUrl = aliYunOptions.customUrl
const path = aliYunOptions.path
for (const img of imgList) {
if (img.fileName) {
let image = img.buffer
if (!image && img.base64Image) {
image = base64ToBuffer(img.base64Image)
}
if (!image) {
ctx.log.error("Can not find image buffer")
throw new Error("Can not find image buffer")
}
try {
const optionUrl = aliYunOptions.options || ""
const remotePath = `${path}${img.fileName}${optionUrl}`
const result = await store.put(remotePath, new Blob([image]))
console.log("Using aliyun SDK for upload, result=>", result)
if (result?.res?.status && result.res.status === 200) {
delete img.base64Image
delete img.buffer
if (customUrl) {
img.imgUrl = `${customUrl}/${path}${img.fileName}${optionUrl}`
} else {
img.imgUrl = `https://${aliYunOptions.bucket}.${aliYunOptions.area}.aliyuncs.com/${path}${img.fileName}${optionUrl}`
}
} else {
throw new Error("Upload failed")
}
} catch (e: any) {
let errMsg: any
if (e?.statusCode) {
errMsg = e.response?.body ?? e.stack ?? "unknown error"
} else {
errMsg = e.toString()
}
ctx.log.error(errMsg)
ctx.emit(IBuildInEvent.NOTIFICATION, {
title: ctx.i18n.translate<ILocalesKey>("UPLOAD_FAILED"),
body: ctx.i18n.translate<ILocalesKey>("CHECK_SETTINGS"),
})
throw errMsg
}
}
}
return ctx
}
export { handleNode }