upload_start_handler.dart 16.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
import 'dart:convert';
import 'dart:io';

import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/config/env_config.dart';
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/services/api_service.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:appframe/utils/file_type_util.dart';
import 'package:appframe/utils/image_util.dart';
import 'package:appframe/utils/video_util.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

class UploadStartHandler extends MessageHandler {
  late WebCubit? _webCubit;

  /// 指令 unique
  late String _cmdUnique;

  /// 文件上传ID标识
  late String _cmdUploadId;

  late int _cmdTotalChunks;
  late int _cmdUploadedChunks;
  late int _cmdTotalByte;
  late int _cmdSentByte;

  @override
  void setCubit(WebCubit cubit) {
    _webCubit = cubit;
  }

  /// 设置指令 unique
  void setCmdUnique(String unique) {
    _cmdUnique = unique;
  }

  void _unfollowCubit() {
    _webCubit = null;
  }

  @override
  Future<dynamic> handleMessage(params) async {
    try {
      if (params is! Map<String, dynamic>) {
        throw Exception('参数错误');
      }

      final String? tempFilePath = params['tempFilePath'] as String?;
      if (tempFilePath == null || tempFilePath.isEmpty) {
        throw Exception('参数错误');
      }

      final String? busi = params['busi'] as String?;
      if (busi == null || busi.isEmpty) {
        throw Exception('参数错误');
      }

      final String? subBusi = params['subBusi'] as String?;
      if (subBusi == null || subBusi.isEmpty) {
        throw Exception('参数错误');
      }

      // 开始处理前,先生成唯一 cmdUploadId
      _cmdUploadId = const Uuid().v4();

      final startTime = DateTime.now();
      final result = await _handle(tempFilePath, busi, subBusi);
      final endTime = DateTime.now();
      debugPrint('====================>上传耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');

      // 处理结果 result, 发送 uploadEnd 指令
      final String url = result['url'] as String;
      _webCubit?.sendUploadEnd(_cmdUnique, _cmdUploadId, url);

      // 返回 null,让 MessageDispatcher 不处理返回指令
      return null;
    } finally {
      _unfollowCubit();
    }
  }

  Future<Map<String, dynamic>> _handle(String filePath, String busi, String subBusi) async {
    ///
    /// 1 判断
    ///
    if (filePath.startsWith(Constant.localFileUrl)) {
      filePath = filePath.replaceFirst(Constant.localFileUrl, '');
    }

    if (filePath.startsWith(Constant.localServerTemp)) {
      filePath = filePath.replaceFirst(Constant.localServerTemp, '');
    }

    //判断文件
    File file = File(filePath);
    if (!file.existsSync()) {
      throw Exception('文件不存在');
    }
    var fileSize = file.lengthSync();
    debugPrint('原始文件大小:$fileSize 字节');

    /// 发送 uploadStart 响应指令
    _webCubit?.sendUploadStartResponse(_cmdUnique, _cmdUploadId);

    ///
    /// 视频文件上传之前进行压缩
    /// 非 mp4 格式的视频文件需先转码
    ///
    String? mimeType = await FileTypeUtil.getMimeType(file);
    if (mimeType?.toLowerCase().startsWith('video/') ?? false) {
      final inputPath = filePath;
      final tempDir = await getTemporaryDirectory();
      final outputPath = '${tempDir.path}/${Uuid().v4()}.mp4';

      bool success = false;
      var startTime = DateTime.now();
      if (mimeType != 'video/mp4') {
        success = await VideoUtil.convertToMp4(
          inputPath,
          outputPath,
          onProgress: (progress) {
            // progress 范围 0 ~ 100
            debugPrint('转码进度: $progress%');

            /// 发送转码进度
            _webCubit?.sendUploadProgress(_cmdUnique, _cmdUploadId, 1, progress, 0, 0, 0, 0);
          },
        );
      } else {
        success = await VideoUtil.compressVideo(
          inputPath,
          outputPath,
          'low',
          onProgress: (progress) {
            // progress 范围 0 ~ 100
            debugPrint('压缩进度: $progress%');

            /// 发送压缩进度
            _webCubit?.sendUploadProgress(_cmdUnique, _cmdUploadId, 1, progress, 0, 0, 0, 0);
          },
        );
      }
      var endTime = DateTime.now();
      debugPrint('====================>压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');

      if (success) {
        file = File(outputPath);
        fileSize = file.lengthSync();
        debugPrint('====================>视频压缩后大小:$fileSize 字节');
      }
    } else if (mimeType?.toLowerCase().startsWith('image/') ?? false) {
      // 对于图片文件,进行压缩
      final inputPath = filePath;
      final tempDir = await getTemporaryDirectory();
      final outputPath = '${tempDir.path}/${Uuid().v4()}.jpg';

      var startTime = DateTime.now();
      final success = await ImageUtil.compressImage(inputPath, outputPath, maxWidth: 1920, quality: 18);
      var endTime = DateTime.now();
      debugPrint('====================>图片压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');

      if (success) {
        file = File(outputPath);
        fileSize = file.lengthSync();
        debugPrint('====================>图片压缩后大小:$fileSize 字节');
      }
    }

    // 限制压缩后仍然大于300M的文件上传
    if (fileSize > 1024 * 1024 * 300) {
      throw Exception('上传的文件过大');
    }

    /// 2
    /// bucket 存储桶名称 : bxe-files | bxe-pics | bxe-videos
    ///
    String bucket;
    if (mimeType?.startsWith('image/') ?? false) {
      bucket = 'bxe-pics';
    } else if (mimeType?.startsWith('video/') ?? false) {
      bucket = 'bxe-videos';
    } else {
      bucket = 'bxe-files';
    }

    /// 3
    /// objectKey
    var uuid = Uuid();
    String logicPrefix = _getLoginPrefix(busi, subBusi);
    String objectKey = '$logicPrefix/${uuid.v4()}${path.extension(file.path)}';

    ///
    /// 4 计算分片
    ///
    final chunkSize = Constant.obsUploadChunkSize;
    final totalChunks = (fileSize / chunkSize).ceil();
    debugPrint('上传文件大小:$fileSize 字节');
    debugPrint('分片数量:$totalChunks');

    _cmdTotalChunks = totalChunks;
    _cmdUploadedChunks = 0;
    _cmdTotalByte = fileSize;
    _cmdSentByte = 0;

    ///
    /// 5 sig
    ///
    var startTime1 = DateTime.now();
    debugPrint('====================>签名开始 $startTime1');
    final bxeApiService = ApiService(baseUrl: Constant.iotAppBaseUrl);
    late String uploadId;
    var signUrls = [];
    for (int i = 0; i < totalChunks; i++) {
      if (i == 0) {
        final initResult = await _init(bxeApiService, objectKey, bucket);
        uploadId = initResult['upload_id'] as String;
        var signUrl = initResult['signed_url'] as String;
        signUrls.add(signUrl);
      } else {
        final nextResult = await _next(bxeApiService, objectKey, bucket, uploadId, i + 1);
        var signUrl = nextResult['signed_url'] as String;
        signUrls.add(signUrl);
      }
    }
    var endTime1 = DateTime.now();
    debugPrint('====================>签名耗时:${endTime1.millisecondsSinceEpoch - startTime1.millisecondsSinceEpoch} 毫秒');

    ///
    /// 6 上传(带进度反馈)
    ///
    final dio = Dio()
      ..options = BaseOptions(
        baseUrl: '',
        connectTimeout: Duration(milliseconds: 30000),
        receiveTimeout: Duration(milliseconds: 30000),
        headers: {'Content-Type': '', 'Accept': ''},
      );

    final randomAccessFile = await file.open();
    Map<int, String> tagsMap = {};

    // 创建分片上传任务列表
    final uploadTasks = <Future<Map<String, dynamic>>>[];

    for (int i = 0; i < totalChunks; i++) {
      final chunkSize = Constant.obsUploadChunkSize;
      final start = i * chunkSize;
      final actualChunkSize = (i + 1) * chunkSize > fileSize ? fileSize - start : chunkSize;

      final chunk = Uint8List(actualChunkSize);
      randomAccessFile.setPositionSync(start);
      await randomAccessFile.readInto(chunk, 0, actualChunkSize);

      uploadTasks.add(_uploadChunkWithProgress(
        dio,
        signUrls[i],
        i,
        chunk,
        onChunkComplete: () {

          _cmdUploadedChunks++;
          _cmdSentByte = _cmdSentByte + actualChunkSize;

          /// 发送 uploadProgress 指令,传递上传进度
          _webCubit?.sendUploadProgress(
            _cmdUnique,
            _cmdUploadId,
            2,
            ((_cmdUploadedChunks / _cmdTotalChunks) * 100).floor(),
            _cmdTotalChunks,
            _cmdUploadedChunks,
            _cmdTotalByte,
            _cmdSentByte,
          );
        },
      ));
    }

    var resultList = await Future.wait(uploadTasks);
    for (var result in resultList) {
      if (result is Map<String, dynamic>) {
        tagsMap[result['idx'] as int] = result['etag'] as String;
      }
    }

    await randomAccessFile.close();

    ///
    /// 7 合并
    ///
    var startTime2 = DateTime.now();
    String location = await _merge(bxeApiService, objectKey, bucket, uploadId, tagsMap);
    var endTime2 = DateTime.now();
    debugPrint('====================>合并签名耗时:${endTime2.millisecondsSinceEpoch - startTime2.millisecondsSinceEpoch} 毫秒');

    ///
    /// 8 针对视频生成封面
    ///
    if (mimeType?.startsWith('video/') ?? false) {
      await _genHwVideoCover(dio, objectKey);
    }

    dio.close(force: true);
    bxeApiService.close();

    return {'url': _addPreUrl(location)};
  }

  static const _signatureNewUrl = '/api/v1/obs/multipart/signaturenew';
  static const _signatureNextUrl = '/api/v1/obs/multipart/signaturenext';
  static const _completeUrl = '/api/v1/obs/multipart/complete';

  /// 初始化,请求后端获取签名信息和上传任务ID
  Future<Map<String, dynamic>> _init(ApiService bxeApiService, String objectKey, String bucket) async {
    var endpoint = '$_signatureNewUrl?objectKey=$objectKey&bucket=$bucket';
    final resp = await bxeApiService.get(endpoint);
    return resp.data;
  }

  /// 每次上传前,请求后端获取签名信息
  Future<Map<String, dynamic>> _next(
    ApiService bxeApiService,
    String objectKey,
    String bucket,
    String uploadId,
    int partNum,
  ) async {
    var endpoint = '$_signatureNextUrl?objectKey=$objectKey&bucket=$bucket&uploadId=$uploadId&partNum=$partNum';
    final resp = await bxeApiService.get(endpoint);
    return resp.data;
  }

  /// 上传段(带进度回调)
  Future<Map<String, dynamic>> _uploadChunkWithProgress(
    Dio dio,
    String signUrl,
    int chunkIndex,
    Uint8List chunk, {
    VoidCallback? onChunkComplete,
    int maxRetries = 3,
  }) async {
    for (int attempt = 0; attempt <= maxRetries; attempt++) {
      try {
        var starTime = DateTime.now();
        final resp = await _uploadChunk(dio, signUrl, chunk, chunkIndex);
        var endTime = DateTime.now();
        if (resp.statusCode == 200) {
          debugPrint(
              '====================> 分片$chunkIndex${attempt + 1}次, $endTime 上传耗时:${endTime.millisecondsSinceEpoch - starTime.millisecondsSinceEpoch} 毫秒');
          final etags = resp.headers['etag'] as List<String>;

          // 分片上传成功,触发回调
          onChunkComplete?.call();

          return {'idx': chunkIndex + 1, 'etag': etags[0]};
        } else {
          throw Exception('Chunk $chunkIndex upload failed: ${resp.statusCode}');
        }
      } catch (e) {
        debugPrint('====================> 分片$chunkIndex${attempt + 1}次, 上传失败:${e.toString()}');
        if (attempt == maxRetries) {
          throw Exception('Chunk $chunkIndex upload failed after $maxRetries attempts: $e');
        }
        // 等待后重试
        await Future.delayed(Duration(seconds: 2 * attempt));
      }
    }
    throw Exception('上传失败');
  }

  /// 上传段,按照最大重试次数进行上传重试
  Future<Map<String, dynamic>> _uploadChunkWithRetry(
    Dio dio,
    String signUrl,
    int chunkIndex,
    Uint8List chunk, {
    int maxRetries = 3,
  }) async {
    //print('====================> 分片$chunkIndex , 开始上传 ${DateTime.now()}');
    for (int attempt = 0; attempt <= maxRetries; attempt++) {
      try {
        var starTime = DateTime.now();
        final resp = await _uploadChunk(dio, signUrl, chunk, chunkIndex);
        var endTime = DateTime.now();
        if (resp.statusCode == 200) {
          debugPrint(
              '====================> 分片$chunkIndex${attempt + 1}次, $endTime 上传耗时:${endTime.millisecondsSinceEpoch - starTime.millisecondsSinceEpoch} 毫秒');
          final etags = resp.headers['etag'] as List<String>;
          return Future.value({'idx': chunkIndex + 1, 'etag': etags[0]}); // 上传成功
        } else {
          throw Exception('Chunk $chunkIndex upload failed: ${resp.statusCode}');
        }
      } catch (e) {
        debugPrint('====================> 分片$chunkIndex${attempt + 1}次, 上传失败:${e.toString()}');
        if (attempt == maxRetries) {
          throw Exception('Chunk $chunkIndex upload failed after $maxRetries attempts: $e');
        }
        // 等待后重试
        await Future.delayed(Duration(seconds: 2 * attempt));
      }
    }
    throw Exception('上传失败');
  }

  /// 上传段
  Future<Response> _uploadChunk(Dio dio, String signUrl, Uint8List chunk, int chunkIndex) async {
    var url = signUrl.replaceFirst('AWSAccessKeyId=', 'AccessKeyId=').replaceFirst(':443', '');
    try {
      // Response response = await _put(url, chunk);
      debugPrint('====================> 分片$chunkIndex , 开始上传 ${DateTime.now()}');
      final response = await dio.put(
        url,
        // data: Stream.fromIterable(chunk.map((e) => [e])),
        // data: Stream.fromIterable([chunk]),
        data: chunk,
      );
      debugPrint('====================> 分片$chunkIndex , 上传成功 ${DateTime.now()}');

      return response;
    } catch (e) {
      throw Exception('Chunk upload failed: $e');
    }
  }

  /// 请求合并文件
  Future<String> _merge(
    ApiService bxeApiService,
    String objectKey,
    String bucket,
    String uploadId,
    Map<int, String> tagsMap,
  ) async {
    final parts = [];
    for (int i = 1; i <= tagsMap.length; i++) {
      parts.add({'partNumber': i, 'etag': tagsMap[i]});
    }

    final response = await bxeApiService.post(_completeUrl, {
      'objectKey': objectKey,
      'bucket': bucket,
      'uploadId': uploadId,
      'parts': parts,
    });

    if (response.statusCode != 200) {
      throw Exception('合并文件失败');
    }

    return response.data["location"];
  }

  String _getLoginPrefix(String busi, String subBusi) {
    var now = DateTime.now();
    var year = now.year;
    var month = now.month;
    var day = now.day;

    String userCode = getIt.get<SharedPreferences>().getString('auth_userCode') ?? 'na';
    String classCode = getIt.get<SharedPreferences>().getString('auth_classCode') ?? 'nac';

    String obsLogicPrefix = "d2";
    if (EnvConfig.env == 'pro') {
      obsLogicPrefix = "p2";
    }

    String busiCode = '${busi}_$subBusi';
    // 属于该特定业务范围的素材,都被看作日后运维可以优先删除的文件,规则:http://wiki.zbuku.cn/confluence/pages/viewpage.action?pageId=137172780
    if (Constant.obsPridelFileConfigs.contains(subBusi.toLowerCase())) {
      obsLogicPrefix = '$obsLogicPrefix/pridel/user/';
    } else {
      obsLogicPrefix = '$obsLogicPrefix/unpridel/user/';
    }
    return '$obsLogicPrefix$year$month$day/app/$classCode/$busiCode/$userCode';
  }

  String _addPreUrl(String location) {
    // /bxe-pics/d2/pridel/user/20251017/bxe/bxe_homework/f4ea233d-9e1b-4a3f-bc8f-b64e776f42a6.jpg
    if (location.startsWith('/bxe-files')) {
      return 'https://files-obs.banxiaoer.com${location.substring(10)}';
    } else if (location.startsWith('/bxe-pics')) {
      return 'https://pics-obs.banxiaoer.com${location.substring(9)}';
    } else if (location.startsWith('/bxe-video')) {
      return 'https://video-obs.banxiaoer.com${location.substring(10)}';
    } else {
      return location;
    }
  }

  /// 生成封面
  Future<void> _genHwVideoCover(Dio dio, String keys) async {
    try {
      var headers = {
        "api-key": 'FJ9qv53Bxp',
      };
      var params = {
        "videoKeys": [keys],
        "outputSuffix": "_p1",
      };
      await dio.post(
        '${Constant.bxeBaseUrl}/go/mpc/create_covers',
        data: jsonEncode(params),
        options: Options(
          headers: headers,
          contentType: 'application/json',
          responseType: ResponseType.json,
        ),
      );
    } catch (e) {
      debugPrint(e.toString());
    }
  }
}