Bladeren bron

[new]文件选择增加类型判断

zk 1 jaar geleden
bovenliggende
commit
9d3aeaf631

+ 2 - 0
assets/string/base/string.xml

@@ -133,4 +133,6 @@
     <string name="select_and_import">选择并导入</string>
     <string name="authorization_failed">授权失败</string>
     <string name="please_choice_local_audio_file">请选择本地音频文件</string>
+    <string name="recording_duration_cannot_less_than_3s">录音时长不能低于3秒</string>
+    <string name="audio_not_support_type">不支持该格式</string>
 </resources>

+ 20 - 0
lib/module/audiopicker/controller.dart

@@ -92,6 +92,26 @@ class AudioPickerController extends BaseController {
       ToastUtil.showToast('文件不存在');
       return;
     }
+    //文件格式是否允许
+    if (!FileUploadCheckHelper.isAllowAudioFile(file.path)) {
+      ToastUtil.showToast(StringName.audioNotSupportType.tr);
+      return;
+    }
+    //文件大小不能超过500M
+    if (file.lengthSync() > 500 * 1024 * 1024) {
+      ToastUtil.showToast(StringName.fileChoiceSizeLimit.tr);
+      return;
+    }
+    //录音时长不能超过5小时不能低于3s
+    if (entity.duration < 3) {
+      ToastUtil.showToast(StringName.recordingDurationCannotLessThan3s.tr);
+      return;
+    }
+    if (entity.duration > 5 * 60 * 60) {
+      ToastUtil.showToast(StringName.fileAudioDurationLimit.tr);
+      return;
+    }
+
     //上传文件
     try {
       TalkBean bean = await talkRepository.talkCreate(

+ 17 - 14
lib/utils/audio_picker_utils.dart

@@ -6,6 +6,20 @@ import 'package:photo_manager/photo_manager.dart';
 class AudioPickerUtils {
   AudioPickerUtils._();
 
+  static const List<String> allowFileType = [
+    'wav',
+    'mp3',
+    'm4a',
+    'flv',
+    'mp4',
+    'wma',
+    '3gp',
+    'amr',
+    'aac',
+    'ogg-opus',
+    'flac'
+  ];
+
   //申请权限
   static Future<bool> requestPermissionExtend() async {
     final PermissionState ps = await PhotoManager.requestPermissionExtend(
@@ -76,21 +90,10 @@ class AudioPickerUtils {
   }
 
   //通过平台文件管理器的方式选择单一文件
-  static Future<File?> pickSingleFileByPlatform() async {
+  static Future<File?> pickSingleFileByPlatform(
+      {List<String> type = allowFileType}) async {
     FilePickerResult? result = await FilePicker.platform
-        .pickFiles(type: FileType.custom, allowedExtensions: [
-      'wav',
-      'mp3',
-      'm4a',
-      'flv',
-      'mp4',
-      'wma',
-      '3gp',
-      'amr',
-      'aac',
-      'ogg-opus',
-      'flac'
-    ]);
+        .pickFiles(type: FileType.custom, allowedExtensions: type);
     if (result != null) {
       String filePath = result.files.single.path!;
       File file = File(filePath);

+ 12 - 0
lib/utils/file_upload_check_helper.dart

@@ -19,6 +19,15 @@ import 'error_handler.dart';
 class FileUploadCheckHelper {
   FileUploadCheckHelper._();
 
+  static bool isAllowAudioFile(String path) {
+    try {
+      String ext = path.split('.').last;
+      return AudioPickerUtils.allowFileType.contains(ext);
+    } catch (e) {
+      return false;
+    }
+  }
+
   static Future<void> choicePlatformLocalFileAndCreateOrder() async {
     TalkBean? bean;
     try {
@@ -61,6 +70,9 @@ class FileUploadCheckHelper {
       if (duration == null) {
         throw PickerException(StringName.fileAudioDurationCannotObtained.tr);
       }
+      if (duration.inSeconds < 3) {
+        throw PickerException(StringName.recordingDurationCannotLessThan3s.tr);
+      }
       //录音时长不能超过5小时
       if (duration.inHours > 5) {
         throw PickerException(StringName.fileAudioDurationLimit.tr);