mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
* v2 init * chore: update eslint suppressions and package dependencies - Removed several eslint suppressions related to array sorting and reversing from eslint-suppressions.json to clean up the configuration. - Updated @lobehub/lint package version from 2.0.0-beta.6 to 2.0.0-beta.7 in package.json for improvements and bug fixes. - Made minor formatting adjustments in vitest.config.mts and various SKILL.md files for better readability and consistency. Signed-off-by: Innei <tukon479@gmail.com> * fix: clean up import statements and formatting - Removed unnecessary whitespace in replaceComponentImports.ts for improved readability. - Standardized import statements in contextEngineering.ts and createAgentExecutors.ts by adding missing spaces for consistency. Signed-off-by: Innei <tukon479@gmail.com> * chore: update eslint suppressions and clean up code formatting * 🐛 fix: use vi.hoisted for mock variable initialization Fix TDZ error in persona service test by using vi.hoisted() to ensure mock variables are available when vi.mock factory runs. --------- Signed-off-by: Innei <tukon479@gmail.com>
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { consola } from 'consola';
|
|
import dotenv from 'dotenv';
|
|
|
|
import s3 from './s3';
|
|
import type { ImgInfo, S3UserConfig, UploadResult } from './s3/types';
|
|
import { formatPath } from './s3/utils';
|
|
|
|
dotenv.config();
|
|
|
|
if (!process.env.DOC_S3_ACCESS_KEY_ID) {
|
|
consola.error('请配置 Doc S3 存储的环境变量: DOC_S3_ACCESS_KEY_ID');
|
|
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!process.env.DOC_S3_SECRET_ACCESS_KEY) {
|
|
consola.error('请配置 Doc S3 存储的环境变量: DOC_S3_SECRET_ACCESS_KEY');
|
|
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!process.env.DOC_S3_PUBLIC_DOMAIN) {
|
|
consola.error('请配置 Doc S3 存储的环境变量: DOC_S3_PUBLIC_DOMAIN');
|
|
|
|
process.exit(1);
|
|
}
|
|
|
|
export const BASE_PATH = 'blog/assets';
|
|
|
|
export const uploader = async (
|
|
file: File,
|
|
filename: string,
|
|
basePath: string = BASE_PATH,
|
|
uploadPath?: string,
|
|
) => {
|
|
const item: ImgInfo = {
|
|
buffer: Buffer.from(await file.arrayBuffer()),
|
|
extname: file.name.split('.').pop() as string,
|
|
fileName: file.name,
|
|
mimeType: file.type,
|
|
};
|
|
|
|
const userConfig: S3UserConfig = {
|
|
accessKeyId: process.env.DOC_S3_ACCESS_KEY_ID || '',
|
|
bucketName: 'hub-apac-1',
|
|
endpoint: 'https://d35842305b91be4b48e06ff9a9ad83f5.r2.cloudflarestorage.com',
|
|
pathPrefix: process.env.DOC_S3_PUBLIC_DOMAIN || '',
|
|
pathStyleAccess: true,
|
|
region: 'auto',
|
|
secretAccessKey: process.env.DOC_S3_SECRET_ACCESS_KEY || '',
|
|
uploadPath: uploadPath || `${basePath}${filename}.{extName}`,
|
|
};
|
|
|
|
const client = s3.createS3Client(userConfig);
|
|
|
|
let results: UploadResult;
|
|
|
|
try {
|
|
results = await s3.createUploadTask({
|
|
acl: 'public-read',
|
|
bucketName: userConfig.bucketName,
|
|
client,
|
|
item: item,
|
|
path: formatPath(item, userConfig.uploadPath),
|
|
urlPrefix: userConfig.pathPrefix,
|
|
});
|
|
|
|
return results.url;
|
|
} catch (error) {
|
|
consola.error('上传到 S3 存储发生错误,请检查网络连接和配置是否正确');
|
|
consola.error(error);
|
|
}
|
|
};
|