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 6b31c590
authored
2025-11-06 15:27:00 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
测试
1 parent
86fb6411
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
16 deletions
lib/config/locator.dart
lib/data/repositories/message/upload_file.dart
lib/data/repositories/message/upload_file2.dart
lib/main.dart
lib/config/locator.dart
View file @
6b31c59
...
...
@@ -23,6 +23,7 @@ import 'package:appframe/data/repositories/message/scan_code_handler.dart';
import
'package:appframe/data/repositories/message/set_title_handler.dart'
;
import
'package:appframe/data/repositories/message/storage_handler.dart'
;
import
'package:appframe/data/repositories/message/upload_file.dart'
;
import
'package:appframe/data/repositories/message/upload_file2.dart'
;
import
'package:appframe/data/repositories/message/vibrate_short_handler.dart'
;
import
'package:appframe/data/repositories/message/video_info_handler.dart'
;
import
'package:appframe/data/repositories/message/wifi_info_handler.dart'
;
...
...
@@ -155,7 +156,8 @@ Future<void> setupLocator() async {
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
ScanCodeHandler
(),
instanceName:
'scanCode'
);
/// 上传文件
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
UploadFileHandler
(),
instanceName:
'uploadFile'
);
// getIt.registerLazySingleton<MessageHandler>(() => UploadFileHandler(), instanceName: 'uploadFile');
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
UploadFile2Handler
(),
instanceName:
'uploadFile'
);
/// 下载文件
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
DownloadFileHandler
(),
instanceName:
'downloadFile'
);
...
...
lib/data/repositories/message/upload_file.dart
View file @
6b31c59
...
...
@@ -6,10 +6,13 @@ import 'package:appframe/services/dispatcher.dart';
import
'package:appframe/utils/file_type_util.dart'
;
import
'package:dio/dio.dart'
;
import
'package:flutter/foundation.dart'
;
import
'package:logger/logger.dart'
;
import
'package:path/path.dart'
as
path
;
import
'package:uuid/uuid.dart'
;
class
UploadFileHandler
extends
MessageHandler
{
static
final
upLogger
=
Logger
(
level:
Level
.
debug
);
@override
Future
handleMessage
(
params
)
async
{
if
(
params
is
!
Map
<
String
,
dynamic
>)
{
...
...
@@ -30,9 +33,18 @@ class UploadFileHandler extends MessageHandler {
throw
Exception
(
'参数错误'
);
}
final
startTimestamp
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'开始上传文件'
);
final
result
=
await
compute
(
_handleUpload
,
{
'filePath'
:
tempFilePath
,
'busi'
:
busi
,
'subBusi'
:
subBusi
});
// final result = await _handleUpload({'filePath': tempFilePath});
final
endTimestamp
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'上传完成,耗时:
${endTimestamp - startTimestamp}
毫秒'
);
result
[
'startTimestamp'
]
=
startTimestamp
;
result
[
'sendTimestamp'
]
=
endTimestamp
;
return
result
;
}
...
...
@@ -47,12 +59,6 @@ class UploadFileHandler extends MessageHandler {
String
busi
=
fileParams
[
'busi'
]
as
String
;
String
subBusi
=
fileParams
[
'subBusi'
]
as
String
;
print
(
'参数-------'
);
print
(
'filePath:
$filePath
'
);
print
(
'busi:
$busi
'
);
print
(
'subBusi:
$subBusi
'
);
print
(
'参数-------'
);
if
(
filePath
.
startsWith
(
Constant
.
localServerUrl
))
{
filePath
=
filePath
.
replaceFirst
(
Constant
.
localServerUrl
,
''
);
}
...
...
@@ -67,18 +73,23 @@ class UploadFileHandler extends MessageHandler {
final
obsApiService
=
ApiService
(
defaultHeaders:
{
'Content-Type'
:
''
,
'Accept'
:
''
});
String
logicPrefix
=
_getLoginPrefix
(
busi
,
subBusi
);
print
(
'logicPrefix:
$logicPrefix
'
);
//并行上传分段
final
uploadResult
=
await
_uploadInParallel
(
bxeApiService
,
obsApiService
,
logicPrefix
,
filePath
);
upLogger
.
d
(
"开始处理并行上传"
);
final
uploadResult
=
await
_uploadInParallel
(
bxeApiService
,
obsApiService
,
logicPrefix
,
filePath
,
maxConcurrency:
30
);
upLogger
.
d
(
"并行上传完成"
);
// 上传结果
String
objectKey
=
uploadResult
[
'objectKey'
]
as
String
;
String
bucket
=
uploadResult
[
'bucket'
]
as
String
;
String
uploadId
=
uploadResult
[
'uploadId'
]
as
String
;
Map
<
int
,
String
>
tagsMap
=
uploadResult
[
'tagsMap'
]
as
Map
<
int
,
String
>;
//请求合并文件
upLogger
.
d
(
"开始处理合并文件"
);
String
location
=
await
_merge
(
bxeApiService
,
objectKey
,
bucket
,
uploadId
,
tagsMap
);
print
(
'location:
$location
'
);
upLogger
.
d
(
"合并文件完成"
);
//关闭Dio
bxeApiService
.
close
();
...
...
@@ -106,10 +117,15 @@ class UploadFileHandler extends MessageHandler {
throw
Exception
(
'上传的文件过大'
);
}
upLogger
.
d
(
'文件大小:
$fileSize
'
);
//分段大小2M
final
chunkSize
=
Constant
.
obsUploadChunkSize
;
upLogger
.
d
(
'分段大小:
$chunkSize
'
);
//分段总数
final
totalChunks
=
(
fileSize
/
chunkSize
).
ceil
();
upLogger
.
d
(
'分段总数:
$totalChunks
'
);
final
randomAccessFile
=
file
.
openSync
();
...
...
@@ -127,14 +143,16 @@ class UploadFileHandler extends MessageHandler {
// String objectKey = 'd2/test/file.csv';
var
uuid
=
Uuid
();
String
objectKey
=
'
$logicPrefix
/
${uuid.v4()}${path.extension(file.path)}
'
;
print
(
'objectKey:
$objectKey
'
);
String
uploadId
=
''
;
Map
<
int
,
String
>
tagsMap
=
{};
final
futures
=
<
Future
>[];
for
(
int
i
=
0
;
i
<
totalChunks
;
i
++)
{
upLogger
.
d
(
'开始处理分段:
$i
'
);
// 控制并发数量
if
(
futures
.
length
>=
maxConcurrency
)
{
upLogger
.
d
(
'超过最大并发数量,等待'
);
await
Future
.
wait
(
futures
);
futures
.
clear
();
}
...
...
@@ -146,6 +164,7 @@ class UploadFileHandler extends MessageHandler {
randomAccessFile
.
setPositionSync
(
start
);
await
randomAccessFile
.
readInto
(
chunk
,
0
,
actualChunkSize
);
final
startTime
=
DateTime
.
now
().
millisecondsSinceEpoch
;
String
chunkSignUrl
;
if
(
i
==
0
)
{
final
initResult
=
await
_init
(
bxeApiService
,
objectKey
,
bucket
);
...
...
@@ -155,7 +174,8 @@ class UploadFileHandler extends MessageHandler {
final
nextResult
=
await
_next
(
bxeApiService
,
objectKey
,
bucket
,
uploadId
,
i
+
1
);
chunkSignUrl
=
nextResult
[
'signed_url'
]
as
String
;
}
print
(
'chunkSignUrl:
$chunkSignUrl
'
);
final
endTime
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'分段
$i
,签名耗时:
${endTime - startTime}
毫秒'
);
// await _uploadChunkWithRetry(obsApiService, chunkSignUrl, i, chunk, tagsMap);
final
future
=
_uploadChunkWithRetry
(
obsApiService
,
chunkSignUrl
,
i
,
chunk
,
tagsMap
);
...
...
@@ -165,6 +185,7 @@ class UploadFileHandler extends MessageHandler {
// 等待剩余的上传完成
if
(
futures
.
isNotEmpty
)
{
await
Future
.
wait
(
futures
);
futures
.
clear
();
}
randomAccessFile
.
closeSync
();
...
...
@@ -200,12 +221,17 @@ class UploadFileHandler extends MessageHandler {
Map
<
int
,
String
>
tagsMap
,
{
int
maxRetries
=
3
,
})
async
{
final
start
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'分段
$chunkIndex
,开始时间:
$start
毫秒'
);
for
(
int
attempt
=
0
;
attempt
<=
maxRetries
;
attempt
++)
{
try
{
final
resp
=
await
_uploadChunk
(
obsApiService
,
signUrl
,
chunk
);
final
resp
=
await
_uploadChunk
(
obsApiService
,
signUrl
,
chunk
,
chunkIndex
);
if
(
resp
.
statusCode
==
200
)
{
final
etags
=
resp
.
headers
[
'etag'
]
as
List
<
String
>;
tagsMap
[
chunkIndex
+
1
]
=
etags
[
0
];
final
end
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'分段
$chunkIndex
,上传段耗时:
${end - start}
毫秒'
);
return
;
// 上传成功
}
else
{
throw
Exception
(
'Chunk
$chunkIndex
upload failed:
${resp.statusCode}
'
);
...
...
@@ -221,14 +247,18 @@ class UploadFileHandler extends MessageHandler {
}
/// 上传段
static
Future
<
Response
>
_uploadChunk
(
ApiService
obsApiService
,
String
signUrl
,
Uint8List
chunk
)
async
{
static
Future
<
Response
>
_uploadChunk
(
ApiService
obsApiService
,
String
signUrl
,
Uint8List
chunk
,
int
chunkIndex
)
async
{
final
start
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'分段
$chunkIndex
,处理开始时间
$start
'
);
var
url
=
signUrl
.
replaceFirst
(
'AWSAccessKeyId='
,
'AccessKeyId='
).
replaceFirst
(
':443'
,
''
);
try
{
Response
response
=
await
obsApiService
.
put
(
url
,
chunk
);
final
end
=
DateTime
.
now
().
millisecondsSinceEpoch
;
upLogger
.
d
(
'分段
$chunkIndex
,处理完成时间:
${end - start}
毫秒'
);
return
response
;
}
catch
(
e
)
{
print
(
'Chunk upload failed:
$e
'
);
throw
Exception
(
'Chunk upload failed:
$e
'
);
}
}
...
...
lib/data/repositories/message/upload_file2.dart
0 → 100644
View file @
6b31c59
This diff is collapsed.
Click to expand it.
lib/main.dart
View file @
6b31c59
...
...
@@ -8,7 +8,7 @@ import 'app.dart';
void
main
(
)
async
{
WidgetsFlutterBinding
.
ensureInitialized
();
await
setupLocator
();
await
registerMqtt
();
//
await registerMqtt();
await
initDatabase
();
runApp
(
const
App
());
...
...
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