|
|
@@ -1,13 +1,13 @@
|
|
|
import type { InferResponseType } from 'hono/client';
|
|
|
import { fileClient } from "../api";
|
|
|
import { isWeapp, isH5 } from './platform';
|
|
|
+import Taro from '@tarojs/taro';
|
|
|
|
|
|
// 平台检测 - 使用统一的 platform.ts
|
|
|
const isMiniProgram = isWeapp();
|
|
|
const isBrowser = isH5();
|
|
|
|
|
|
-// 如果支持 Taro,优先使用 Taro
|
|
|
-let Taro: any = null;
|
|
|
+
|
|
|
if (isMiniProgram) {
|
|
|
try {
|
|
|
Taro = require('@tarojs/taro');
|
|
|
@@ -348,9 +348,7 @@ export class TaroMinIOMultipartUploader {
|
|
|
const partSize = PART_SIZE;
|
|
|
|
|
|
// 获取文件信息
|
|
|
- const fileInfo = await Taro?.getFileSystemManager?.()?.getFileInfo({
|
|
|
- filePath
|
|
|
- }) || { size: 0 };
|
|
|
+ const fileInfo = await getFileInfoPromise(filePath);
|
|
|
const totalSize = fileInfo.size;
|
|
|
const totalParts = Math.ceil(totalSize / partSize);
|
|
|
const uploadedParts: UploadPart[] = [];
|
|
|
@@ -524,9 +522,7 @@ export class TaroMinIOUploader {
|
|
|
callbacks?: MinioProgressCallbacks
|
|
|
): Promise<UploadResult> {
|
|
|
// 获取文件信息
|
|
|
- const fileInfo = await Taro?.getFileSystemManager?.()?.getFileInfo({
|
|
|
- filePath
|
|
|
- }) || { size: 0 };
|
|
|
+ const fileInfo = await getFileInfoPromise(filePath);
|
|
|
const totalSize = fileInfo.size;
|
|
|
|
|
|
callbacks?.onProgress?.({
|
|
|
@@ -696,8 +692,8 @@ export async function uploadMinIOWithPolicy(
|
|
|
fileName = (file as File).name || fileKey;
|
|
|
} else if (isMiniProgram && typeof file === 'string') {
|
|
|
try {
|
|
|
- const fileInfo = await Taro?.getFileSystemManager?.()?.getFileInfo({ filePath: file });
|
|
|
- fileSize = fileInfo?.size || 0;
|
|
|
+ const fileInfo = await getFileInfoPromise(file);
|
|
|
+ fileSize = fileInfo.size;
|
|
|
fileType = undefined;
|
|
|
fileName = fileKey;
|
|
|
} catch {
|
|
|
@@ -741,6 +737,30 @@ export async function uploadMinIOWithPolicy(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// ==================== 小程序工具函数 ====================
|
|
|
+/**
|
|
|
+ * Promise封装的getFileInfo函数
|
|
|
+ */
|
|
|
+async function getFileInfoPromise(filePath: string): Promise<{ size: number }> {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const fs = Taro?.getFileSystemManager?.();
|
|
|
+ if (!fs) {
|
|
|
+ reject(new Error('小程序文件系统不可用'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ fs.getFileInfo({
|
|
|
+ filePath,
|
|
|
+ success: (res) => {
|
|
|
+ resolve({ size: res.size });
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ reject(new Error(`获取文件信息失败: ${error.errMsg}`));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
// ==================== 小程序专用函数 ====================
|
|
|
/**
|
|
|
* 小程序专用:从选择器上传
|
|
|
@@ -758,43 +778,6 @@ export async function uploadMinIOWithTaroFile(
|
|
|
return uploadMinIOWithPolicy(uploadPath, tempFilePath, fileKey, callbacks);
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * 小程序专用:从相册或相机选择并上传
|
|
|
- */
|
|
|
-export async function uploadFromChoose(
|
|
|
- sourceType: ('album' | 'camera')[] = ['album', 'camera'],
|
|
|
- uploadPath: string = '',
|
|
|
- callbacks?: MinioProgressCallbacks
|
|
|
-): Promise<UploadResult> {
|
|
|
- if (!isMiniProgram) {
|
|
|
- throw new Error('此功能仅支持小程序环境');
|
|
|
- }
|
|
|
-
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- if (typeof wx === 'undefined') {
|
|
|
- reject(new Error('小程序环境未找到'));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- wx.chooseImage({
|
|
|
- count: 1,
|
|
|
- sourceType,
|
|
|
- success: async (res) => {
|
|
|
- const tempFilePath = res.tempFilePaths[0];
|
|
|
- const fileName = res.tempFiles[0]?.name || tempFilePath.split('/').pop() || 'unnamed-file';
|
|
|
-
|
|
|
- try {
|
|
|
- const result = await uploadMinIOWithPolicy(uploadPath, tempFilePath, fileName, callbacks);
|
|
|
- resolve(result);
|
|
|
- } catch (error) {
|
|
|
- reject(error);
|
|
|
- }
|
|
|
- },
|
|
|
- fail: reject
|
|
|
- });
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
// 新增:自动适应运行环境的文件选择并上传函数
|
|
|
/**
|
|
|
* 自动适应运行环境:选择文件并上传到 MinIO
|