Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
542 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
declare module '*.json' { | ||
const value: { [key: string]: any } | ||
export default value | ||
} | ||
} | ||
|
||
declare module "ali-oss" | ||
declare module "arraybuffer-to-buffer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
libs/Universal-PicGo-Core/src/plugins/uploader/aliyun-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 } |
100 changes: 100 additions & 0 deletions
100
libs/Universal-PicGo-Core/src/plugins/uploader/aliyun-web.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { IAliyunConfig, IPicGo } from "../../types" | ||
import crypto from "crypto" | ||
import mime from "mime-types" | ||
import { IBuildInEvent } from "../../utils/enums" | ||
import { ILocalesKey } from "../../i18n/zh-CN" | ||
import { AxiosRequestConfig } from "axios" | ||
import { base64ToBuffer, safeParse } from "../../utils/common" | ||
|
||
// generate OSS signature | ||
const generateSignature = (options: IAliyunConfig, fileName: string): string => { | ||
const date = new Date().toUTCString() | ||
const mimeType = mime.lookup(fileName) | ||
if (!mimeType) throw Error(`No mime type found for file ${fileName}`) | ||
|
||
const signString = `PUT\n\n${mimeType}\n${date}\n/${options.bucket}/${options.path}${fileName}` | ||
|
||
const signature = crypto.createHmac("sha1", options.accessKeySecret).update(signString).digest("base64") | ||
return `OSS ${options.accessKeyId}:${signature}` | ||
} | ||
|
||
const postOptions = ( | ||
options: IAliyunConfig, | ||
fileName: string, | ||
signature: string, | ||
image: Buffer | ||
): AxiosRequestConfig => { | ||
const xCorsHeaders = { | ||
Host: `${options.bucket}.${options.area}.aliyuncs.com`, | ||
Date: new Date().toUTCString(), | ||
} | ||
|
||
return { | ||
method: "PUT", | ||
url: `https://${options.bucket}.${options.area}.aliyuncs.com/${encodeURI(options.path)}${encodeURI(fileName)}`, | ||
headers: { | ||
Authorization: signature, | ||
"Content-Type": mime.lookup(fileName), | ||
"x-cors-headers": JSON.stringify(xCorsHeaders), | ||
}, | ||
data: image, | ||
resolveWithFullResponse: true, | ||
} as AxiosRequestConfig | ||
} | ||
|
||
const handleWeb = async (ctx: IPicGo): Promise<IPicGo> => { | ||
const aliYunOptions = ctx.getConfig<IAliyunConfig>("picBed.aliyun") | ||
if (!aliYunOptions) { | ||
throw new Error("Can't find aliYun OSS config") | ||
} | ||
|
||
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 signature = generateSignature(aliYunOptions, img.fileName) | ||
const options = postOptions(aliYunOptions, img.fileName, signature, image) | ||
const res: any = await ctx.request(options) | ||
const body = safeParse<any>(res) | ||
if (body.statusCode === 200) { | ||
delete img.base64Image | ||
delete img.buffer | ||
const optionUrl = aliYunOptions.options || "" | ||
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 { handleWeb } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.