Преглед изворни кода

♻️ refactor(minio): 重构文件信息获取逻辑

- 移除Taro变量的条件声明,改为顶部直接导入
- 新增getFileInfoPromise工具函数,统一封装文件信息获取逻辑
- 替换多处重复的Taro.getFileSystemManager().getFileInfo调用为getFileInfoPromise
- 删除uploadFromChoose函数,移除未使用的功能代码
- 优化错误处理和返回值处理,确保size属性存在性

🔧 chore(minio): 代码结构优化

- 调整小程序工具函数位置,增加明确注释分隔
- 移除冗余的空行和注释
- 统一文件信息获取错误处理方式
- 简化fileSize赋值逻辑,移除冗余的空值判断
yourname пре 4 месеци
родитељ
комит
ccabbe7a46
1 измењених фајлова са 30 додато и 47 уклоњено
  1. 30 47
      mini/src/utils/minio.ts

+ 30 - 47
mini/src/utils/minio.ts

@@ -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