This commit is contained in:
2026-04-25 16:36:34 +08:00
commit db90e7579b
1876 changed files with 189777 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import { showToasts, t } from '..';
import { request } from '../api/request';
import _get from 'lodash/get';
import { getGlobalConfig } from '../model/config';
import { showErrorToasts } from '../manager/ui';
import filesize from 'filesize';
export type UploadFileUsage = 'chat' | 'group' | 'user' | 'unknown';
interface UploadFileOptions {
usage?: UploadFileUsage;
onProgress?: (percent: number, progressEvent: unknown) => void;
}
export interface UploadFileResult {
etag: string;
path: string;
url: string;
}
/**
* 上传文件
*/
export async function uploadFile(
file: File,
options: UploadFileOptions = {}
): Promise<UploadFileResult> {
const form = new FormData();
form.append('file', file);
form.append('usage', options.usage ?? 'unknown');
const uploadFileLimit = getGlobalConfig().uploadFileLimit;
if (file.size > uploadFileLimit) {
// 文件过大
showErrorToasts(
`${t('上传失败, 支持的文件最大大小为:')} ${filesize(uploadFileLimit, {
base: 2,
})}`
);
throw new Error('File Too Large');
}
try {
const { data } = await request.post('/upload', form, {
onUploadProgress(progressEvent) {
if (progressEvent.lengthComputable) {
if (typeof options.onProgress === 'function') {
options.onProgress(
progressEvent.loaded / progressEvent.total,
progressEvent
);
}
}
},
});
return data;
} catch (e) {
showToasts(`${t('上传失败')}: ${t('可能是文件体积过大')}`, 'error');
console.error(`${t('上传失败')}: ${_get(e, 'message')}`);
throw e;
}
}