Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 35d54674
authored
2026-03-20 16:05:05 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
针对文件上传,限制视频文件压缩后仍然大于300M的文件进行上传
1 parent
2aa2e800
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
21 deletions
lib/data/repositories/message/upload_file.dart
lib/data/repositories/message/upload_file.dart
View file @
35d5467
...
@@ -41,7 +41,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -41,7 +41,7 @@ class UploadFileHandler extends MessageHandler {
final
startTime
=
DateTime
.
now
();
final
startTime
=
DateTime
.
now
();
final
result
=
await
_handle
(
tempFilePath
,
busi
,
subBusi
);
final
result
=
await
_handle
(
tempFilePath
,
busi
,
subBusi
);
final
endTime
=
DateTime
.
now
();
final
endTime
=
DateTime
.
now
();
p
rint
(
'====================>上传耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
debugP
rint
(
'====================>上传耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
// result['startTime'] = startTime.toString();
// result['startTime'] = startTime.toString();
// result['endTime'] = endTime.toString();
// result['endTime'] = endTime.toString();
...
@@ -66,12 +66,9 @@ class UploadFileHandler extends MessageHandler {
...
@@ -66,12 +66,9 @@ class UploadFileHandler extends MessageHandler {
if
(!
file
.
existsSync
())
{
if
(!
file
.
existsSync
())
{
throw
Exception
(
'文件不存在'
);
throw
Exception
(
'文件不存在'
);
}
}
//暂时仅支持200M的文件上传
var
fileSize
=
file
.
lengthSync
();
var
fileSize
=
file
.
lengthSync
();
if
(
fileSize
>
1024
*
1024
*
200
)
{
debugPrint
(
'原始文件大小:
$fileSize
字节'
);
throw
Exception
(
'上传的文件过大'
);
}
print
(
'原始文件大小:
$fileSize
字节'
);
///
///
/// 视频文件上传之前进行压缩
/// 视频文件上传之前进行压缩
...
@@ -83,17 +80,22 @@ class UploadFileHandler extends MessageHandler {
...
@@ -83,17 +80,22 @@ class UploadFileHandler extends MessageHandler {
final
tempDir
=
await
getTemporaryDirectory
();
final
tempDir
=
await
getTemporaryDirectory
();
final
outputPath
=
'
${tempDir.path}
/
${Uuid().v4()}
.mp4'
;
final
outputPath
=
'
${tempDir.path}
/
${Uuid().v4()}
.mp4'
;
bool
success
=
false
;
var
startTime
=
DateTime
.
now
();
var
startTime
=
DateTime
.
now
();
if
(
mimeType
!=
'video/mp4'
)
{
if
(
mimeType
!=
'video/mp4'
)
{
await
VideoUtil
.
convertToMp4
(
inputPath
,
outputPath
);
success
=
await
VideoUtil
.
convertToMp4
(
inputPath
,
outputPath
);
}
else
{
}
else
{
await
VideoUtil
.
compressVideo
(
inputPath
,
outputPath
,
'low'
);
success
=
await
VideoUtil
.
compressVideo
(
inputPath
,
outputPath
,
'low'
);
}
}
var
endTime
=
DateTime
.
now
();
var
endTime
=
DateTime
.
now
();
p
rint
(
'====================>压缩耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
debugP
rint
(
'====================>压缩耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
if
(
success
)
{
file
=
File
(
outputPath
);
file
=
File
(
outputPath
);
fileSize
=
file
.
lengthSync
();
fileSize
=
file
.
lengthSync
();
debugPrint
(
'====================>视频压缩后大小:
$fileSize
字节'
);
}
}
else
if
(
mimeType
?.
toLowerCase
().
startsWith
(
'image/'
)
??
false
)
{
// 对于图片文件,进行压缩
}
else
if
(
mimeType
?.
toLowerCase
().
startsWith
(
'image/'
)
??
false
)
{
// 对于图片文件,进行压缩
// 对于图片文件,进行压缩
// 对于图片文件,进行压缩
final
inputPath
=
filePath
;
final
inputPath
=
filePath
;
...
@@ -103,13 +105,18 @@ class UploadFileHandler extends MessageHandler {
...
@@ -103,13 +105,18 @@ class UploadFileHandler extends MessageHandler {
var
startTime
=
DateTime
.
now
();
var
startTime
=
DateTime
.
now
();
final
success
=
await
ImageUtil
.
compressImage
(
inputPath
,
outputPath
,
maxWidth:
1920
,
quality:
18
);
final
success
=
await
ImageUtil
.
compressImage
(
inputPath
,
outputPath
,
maxWidth:
1920
,
quality:
18
);
var
endTime
=
DateTime
.
now
();
var
endTime
=
DateTime
.
now
();
p
rint
(
'====================>图片压缩耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
debugP
rint
(
'====================>图片压缩耗时:
${endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch}
毫秒'
);
if
(
success
)
{
if
(
success
)
{
file
=
File
(
outputPath
);
file
=
File
(
outputPath
);
fileSize
=
file
.
lengthSync
();
fileSize
=
file
.
lengthSync
();
print
(
'====================>图片压缩后大小:
$fileSize
字节'
);
debugPrint
(
'====================>图片压缩后大小:
$fileSize
字节'
);
}
}
}
// 限制压缩后仍然大于300M的文件上传
if
(
fileSize
>
1024
*
1024
*
300
)
{
throw
Exception
(
'上传的文件过大'
);
}
}
/// 2
/// 2
...
@@ -135,14 +142,14 @@ class UploadFileHandler extends MessageHandler {
...
@@ -135,14 +142,14 @@ class UploadFileHandler extends MessageHandler {
///
///
final
chunkSize
=
Constant
.
obsUploadChunkSize
;
final
chunkSize
=
Constant
.
obsUploadChunkSize
;
final
totalChunks
=
(
fileSize
/
chunkSize
).
ceil
();
final
totalChunks
=
(
fileSize
/
chunkSize
).
ceil
();
p
rint
(
'上传文件大小:
$fileSize
字节'
);
debugP
rint
(
'上传文件大小:
$fileSize
字节'
);
p
rint
(
'分片数量:
$totalChunks
'
);
debugP
rint
(
'分片数量:
$totalChunks
'
);
///
///
/// 5 sig
/// 5 sig
///
///
var
startTime1
=
DateTime
.
now
();
var
startTime1
=
DateTime
.
now
();
p
rint
(
'====================>签名开始
$startTime1
'
);
debugP
rint
(
'====================>签名开始
$startTime1
'
);
final
bxeApiService
=
ApiService
(
baseUrl:
Constant
.
iotAppBaseUrl
);
final
bxeApiService
=
ApiService
(
baseUrl:
Constant
.
iotAppBaseUrl
);
late
String
uploadId
;
late
String
uploadId
;
var
signUrls
=
[];
var
signUrls
=
[];
...
@@ -159,7 +166,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -159,7 +166,7 @@ class UploadFileHandler extends MessageHandler {
}
}
}
}
var
endTime1
=
DateTime
.
now
();
var
endTime1
=
DateTime
.
now
();
p
rint
(
'====================>签名耗时:
${endTime1.millisecondsSinceEpoch - startTime1.millisecondsSinceEpoch}
毫秒'
);
debugP
rint
(
'====================>签名耗时:
${endTime1.millisecondsSinceEpoch - startTime1.millisecondsSinceEpoch}
毫秒'
);
///
///
/// 6 上传
/// 6 上传
...
@@ -204,7 +211,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -204,7 +211,7 @@ class UploadFileHandler extends MessageHandler {
var
startTime2
=
DateTime
.
now
();
var
startTime2
=
DateTime
.
now
();
String
location
=
await
_merge
(
bxeApiService
,
objectKey
,
bucket
,
uploadId
,
tagsMap
);
String
location
=
await
_merge
(
bxeApiService
,
objectKey
,
bucket
,
uploadId
,
tagsMap
);
var
endTime2
=
DateTime
.
now
();
var
endTime2
=
DateTime
.
now
();
p
rint
(
'====================>合并签名耗时:
${endTime2.millisecondsSinceEpoch - startTime2.millisecondsSinceEpoch}
毫秒'
);
debugP
rint
(
'====================>合并签名耗时:
${endTime2.millisecondsSinceEpoch - startTime2.millisecondsSinceEpoch}
毫秒'
);
///
///
/// 8 针对视频生成封面
/// 8 针对视频生成封面
...
@@ -258,7 +265,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -258,7 +265,7 @@ class UploadFileHandler extends MessageHandler {
final
resp
=
await
_uploadChunk
(
dio
,
signUrl
,
chunk
,
chunkIndex
);
final
resp
=
await
_uploadChunk
(
dio
,
signUrl
,
chunk
,
chunkIndex
);
var
endTime
=
DateTime
.
now
();
var
endTime
=
DateTime
.
now
();
if
(
resp
.
statusCode
==
200
)
{
if
(
resp
.
statusCode
==
200
)
{
p
rint
(
debugP
rint
(
'====================> 分片
$chunkIndex
第
${attempt + 1}
次,
$endTime
上传耗时:
${endTime.millisecondsSinceEpoch - starTime.millisecondsSinceEpoch}
毫秒'
);
'====================> 分片
$chunkIndex
第
${attempt + 1}
次,
$endTime
上传耗时:
${endTime.millisecondsSinceEpoch - starTime.millisecondsSinceEpoch}
毫秒'
);
final
etags
=
resp
.
headers
[
'etag'
]
as
List
<
String
>;
final
etags
=
resp
.
headers
[
'etag'
]
as
List
<
String
>;
return
Future
.
value
({
'idx'
:
chunkIndex
+
1
,
'etag'
:
etags
[
0
]});
// 上传成功
return
Future
.
value
({
'idx'
:
chunkIndex
+
1
,
'etag'
:
etags
[
0
]});
// 上传成功
...
@@ -266,7 +273,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -266,7 +273,7 @@ class UploadFileHandler extends MessageHandler {
throw
Exception
(
'Chunk
$chunkIndex
upload failed:
${resp.statusCode}
'
);
throw
Exception
(
'Chunk
$chunkIndex
upload failed:
${resp.statusCode}
'
);
}
}
}
catch
(
e
)
{
}
catch
(
e
)
{
p
rint
(
'====================> 分片
$chunkIndex
第
${attempt + 1}
次, 上传失败:
${e.toString()}
'
);
debugP
rint
(
'====================> 分片
$chunkIndex
第
${attempt + 1}
次, 上传失败:
${e.toString()}
'
);
if
(
attempt
==
maxRetries
)
{
if
(
attempt
==
maxRetries
)
{
throw
Exception
(
'Chunk
$chunkIndex
upload failed after
$maxRetries
attempts:
$e
'
);
throw
Exception
(
'Chunk
$chunkIndex
upload failed after
$maxRetries
attempts:
$e
'
);
}
}
...
@@ -282,14 +289,14 @@ class UploadFileHandler extends MessageHandler {
...
@@ -282,14 +289,14 @@ class UploadFileHandler extends MessageHandler {
var
url
=
signUrl
.
replaceFirst
(
'AWSAccessKeyId='
,
'AccessKeyId='
).
replaceFirst
(
':443'
,
''
);
var
url
=
signUrl
.
replaceFirst
(
'AWSAccessKeyId='
,
'AccessKeyId='
).
replaceFirst
(
':443'
,
''
);
try
{
try
{
// Response response = await _put(url, chunk);
// Response response = await _put(url, chunk);
p
rint
(
'====================> 分片
$chunkIndex
, 开始上传
${DateTime.now()}
'
);
debugP
rint
(
'====================> 分片
$chunkIndex
, 开始上传
${DateTime.now()}
'
);
final
response
=
await
dio
.
put
(
final
response
=
await
dio
.
put
(
url
,
url
,
// data: Stream.fromIterable(chunk.map((e) => [e])),
// data: Stream.fromIterable(chunk.map((e) => [e])),
// data: Stream.fromIterable([chunk]),
// data: Stream.fromIterable([chunk]),
data:
chunk
,
data:
chunk
,
);
);
p
rint
(
'====================> 分片
$chunkIndex
, 上传成功
${DateTime.now()}
'
);
debugP
rint
(
'====================> 分片
$chunkIndex
, 上传成功
${DateTime.now()}
'
);
return
response
;
return
response
;
}
catch
(
e
)
{
}
catch
(
e
)
{
...
@@ -382,7 +389,7 @@ class UploadFileHandler extends MessageHandler {
...
@@ -382,7 +389,7 @@ class UploadFileHandler extends MessageHandler {
),
),
);
);
}
catch
(
e
)
{
}
catch
(
e
)
{
print
(
e
);
debugPrint
(
e
.
toString
()
);
}
}
}
}
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment