Commit 35d54674 by tanghuan

针对文件上传,限制视频文件压缩后仍然大于300M的文件进行上传

1 parent 2aa2e800
......@@ -41,7 +41,7 @@ class UploadFileHandler extends MessageHandler {
final startTime = DateTime.now();
final result = await _handle(tempFilePath, busi, subBusi);
final endTime = DateTime.now();
print('====================>上传耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
debugPrint('====================>上传耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
// result['startTime'] = startTime.toString();
// result['endTime'] = endTime.toString();
......@@ -66,12 +66,9 @@ class UploadFileHandler extends MessageHandler {
if (!file.existsSync()) {
throw Exception('文件不存在');
}
//暂时仅支持200M的文件上传
var fileSize = file.lengthSync();
if (fileSize > 1024 * 1024 * 200) {
throw Exception('上传的文件过大');
}
print('原始文件大小:$fileSize 字节');
debugPrint('原始文件大小:$fileSize 字节');
///
/// 视频文件上传之前进行压缩
......@@ -83,17 +80,22 @@ class UploadFileHandler extends MessageHandler {
final tempDir = await getTemporaryDirectory();
final outputPath = '${tempDir.path}/${Uuid().v4()}.mp4';
bool success = false;
var startTime = DateTime.now();
if (mimeType != 'video/mp4') {
await VideoUtil.convertToMp4(inputPath, outputPath);
success = await VideoUtil.convertToMp4(inputPath, outputPath);
} else {
await VideoUtil.compressVideo(inputPath, outputPath, 'low');
success = await VideoUtil.compressVideo(inputPath, outputPath, 'low');
}
var endTime = DateTime.now();
print('====================>压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
debugPrint('====================>压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
if (success) {
file = File(outputPath);
fileSize = file.lengthSync();
debugPrint('====================>视频压缩后大小:$fileSize 字节');
}
file = File(outputPath);
fileSize = file.lengthSync();
} else if (mimeType?.toLowerCase().startsWith('image/') ?? false) { // 对于图片文件,进行压缩
// 对于图片文件,进行压缩
final inputPath = filePath;
......@@ -103,15 +105,20 @@ class UploadFileHandler extends MessageHandler {
var startTime = DateTime.now();
final success = await ImageUtil.compressImage(inputPath, outputPath, maxWidth: 1920, quality: 18);
var endTime = DateTime.now();
print('====================>图片压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
debugPrint('====================>图片压缩耗时:${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch} 毫秒');
if (success) {
file = File(outputPath);
fileSize = file.lengthSync();
print('====================>图片压缩后大小:$fileSize 字节');
debugPrint('====================>图片压缩后大小:$fileSize 字节');
}
}
// 限制压缩后仍然大于300M的文件上传
if (fileSize > 1024 * 1024 * 300) {
throw Exception('上传的文件过大');
}
/// 2
/// bucket 存储桶名称 : bxe-files | bxe-pics | bxe-videos
///
......@@ -135,14 +142,14 @@ class UploadFileHandler extends MessageHandler {
///
final chunkSize = Constant.obsUploadChunkSize;
final totalChunks = (fileSize / chunkSize).ceil();
print('上传文件大小:$fileSize 字节');
print('分片数量:$totalChunks');
debugPrint('上传文件大小:$fileSize 字节');
debugPrint('分片数量:$totalChunks');
///
/// 5 sig
///
var startTime1 = DateTime.now();
print('====================>签名开始 $startTime1');
debugPrint('====================>签名开始 $startTime1');
final bxeApiService = ApiService(baseUrl: Constant.iotAppBaseUrl);
late String uploadId;
var signUrls = [];
......@@ -159,7 +166,7 @@ class UploadFileHandler extends MessageHandler {
}
}
var endTime1 = DateTime.now();
print('====================>签名耗时:${endTime1.millisecondsSinceEpoch - startTime1.millisecondsSinceEpoch} 毫秒');
debugPrint('====================>签名耗时:${endTime1.millisecondsSinceEpoch - startTime1.millisecondsSinceEpoch} 毫秒');
///
/// 6 上传
......@@ -204,7 +211,7 @@ class UploadFileHandler extends MessageHandler {
var startTime2 = DateTime.now();
String location = await _merge(bxeApiService, objectKey, bucket, uploadId, tagsMap);
var endTime2 = DateTime.now();
print('====================>合并签名耗时:${endTime2.millisecondsSinceEpoch - startTime2.millisecondsSinceEpoch} 毫秒');
debugPrint('====================>合并签名耗时:${endTime2.millisecondsSinceEpoch - startTime2.millisecondsSinceEpoch} 毫秒');
///
/// 8 针对视频生成封面
......@@ -258,7 +265,7 @@ class UploadFileHandler extends MessageHandler {
final resp = await _uploadChunk(dio, signUrl, chunk, chunkIndex);
var endTime = DateTime.now();
if (resp.statusCode == 200) {
print(
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]}); // 上传成功
......@@ -266,7 +273,7 @@ class UploadFileHandler extends MessageHandler {
throw Exception('Chunk $chunkIndex upload failed: ${resp.statusCode}');
}
} catch (e) {
print('====================> 分片$chunkIndex${attempt + 1}次, 上传失败:${e.toString()}');
debugPrint('====================> 分片$chunkIndex${attempt + 1}次, 上传失败:${e.toString()}');
if (attempt == maxRetries) {
throw Exception('Chunk $chunkIndex upload failed after $maxRetries attempts: $e');
}
......@@ -282,14 +289,14 @@ class UploadFileHandler extends MessageHandler {
var url = signUrl.replaceFirst('AWSAccessKeyId=', 'AccessKeyId=').replaceFirst(':443', '');
try {
// Response response = await _put(url, chunk);
print('====================> 分片$chunkIndex , 开始上传 ${DateTime.now()}');
debugPrint('====================> 分片$chunkIndex , 开始上传 ${DateTime.now()}');
final response = await dio.put(
url,
// data: Stream.fromIterable(chunk.map((e) => [e])),
// data: Stream.fromIterable([chunk]),
data: chunk,
);
print('====================> 分片$chunkIndex , 上传成功 ${DateTime.now()}');
debugPrint('====================> 分片$chunkIndex , 上传成功 ${DateTime.now()}');
return response;
} catch (e) {
......@@ -382,7 +389,7 @@ class UploadFileHandler extends MessageHandler {
),
);
} catch (e) {
print(e);
debugPrint(e.toString());
}
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!