Skip to content

Commit a1a950d

Browse files
authored
KS-66 修复上传进度相关问题 (#102)
* 修复表单上传进度粒度 * 修复分片上传重试导致的进度异常 * 更新Dio * 添加example * 更新example的environment * fix ci * review fix
1 parent 85767f0 commit a1a950d

File tree

12 files changed

+122
-10
lines changed

12 files changed

+122
-10
lines changed

base/example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/

base/example/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

base/example/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A sample command-line application with an entrypoint in `bin/`, library code
2+
in `lib/`, and example unit test in `test/`.

base/example/analysis_options.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'dart:io';
2+
import 'package:qiniu_sdk_base/qiniu_sdk_base.dart';
3+
import 'package:logger/logger.dart';
4+
5+
final logger = Logger(
6+
printer: PrettyPrinter(
7+
methodCount: 1,
8+
dateTimeFormat: DateTimeFormat.dateAndTime,
9+
),
10+
level: Level.trace,
11+
filter: ProductionFilter(),
12+
);
13+
14+
void main(List<String> arguments) async {
15+
final token = "<uptoken>";
16+
final file = File("<file path>");
17+
assert(file.existsSync());
18+
19+
final storage = Storage();
20+
final controller = PutController();
21+
controller.addProgressListener((progress) {
22+
logger.d("Progress: $progress");
23+
});
24+
controller.addStatusListener((status) {
25+
logger.d("Status: $status");
26+
});
27+
controller.addSendProgressListener((sendProgress) {
28+
logger.d("Send Progress: $sendProgress");
29+
});
30+
31+
final now = DateTime.now();
32+
await storage.putFile(
33+
file,
34+
token,
35+
options: PutOptions(
36+
forceBySingle: true, // 强制表单上传
37+
controller: controller,
38+
partSize: 4,
39+
),
40+
);
41+
logger.d(
42+
"Upload completed in ${DateTime.now().difference(now).inSeconds} seconds",
43+
);
44+
}

base/example/pubspec.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: qiniu_dart_sdk_example
2+
description: A sample command-line application.
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ">=3.0.0 <4.0.0"
8+
9+
dependencies:
10+
qiniu_sdk_base:
11+
path: ../
12+
dio: ^5.8.0+1
13+
logger: ^2.6.0
14+
15+
dev_dependencies:
16+
test: any
17+
lints: any

base/lib/src/storage/methods/put/by_part/upload_part_task.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class UploadPartTask extends RequestTask<UploadPart> {
6666

6767
final response = await client.put<Map<String, dynamic>>(
6868
paramUrl,
69-
data: Stream.fromIterable([bytes.cast<int>()]),
69+
data: Stream.value(bytes),
7070
// 在 data 是 stream 的场景下, interceptor 传入 cancelToken 这里不传会有 bug
7171
cancelToken: controller?.cancelToken,
7272
options: Options(

base/lib/src/storage/methods/put/by_part/upload_parts_task.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class UploadPartsTask extends RequestTask<List<Part>> with CacheMixin {
144144
// partNumber 按照后端要求必须从 1 开始
145145
final partNumber = ++_uploadingPartIndex;
146146

147-
await for (var bytes in resource.stream) {
147+
await for (final bytes in resource.stream) {
148148
// 跳过上传过的分片
149149
final uploadedPart = _uploadedPartMap[partNumber];
150150
if (uploadedPart != null) {
@@ -189,7 +189,9 @@ class UploadPartsTask extends RequestTask<List<Part>> with CacheMixin {
189189
controller
190190
// UploadPartTask 一次上传一个 chunk,通知一次进度
191191
..addSendProgressListener((percent) {
192-
_sentPartCount++;
192+
if (!task.isRetrying) {
193+
_sentPartCount++;
194+
}
193195
notifySendProgress();
194196
})
195197
// UploadPartTask 上传完成后触发

base/lib/src/storage/methods/put/by_single/put_by_single_task.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import 'package:dio/dio.dart';
22

3+
import '../../../../util/chunk_list.dart' show chunkList;
34
import '../../../../../qiniu_sdk_base.dart';
45
import '../../../resource/resource.dart';
56

67
// 直传任务
78
class PutBySingleTask extends RequestTask<PutResponse> {
8-
late Resource resource;
9-
9+
final Resource resource;
1010
final PutOptions options;
1111

1212
/// 上传凭证
@@ -56,7 +56,11 @@ class PutBySingleTask extends RequestTask<PutResponse> {
5656
await resource.open();
5757

5858
final multipartFile = MultipartFile.fromStream(
59-
resource.getStream,
59+
() {
60+
return resource
61+
.getStream()
62+
.expand((data) => chunkList(data, 64 * 1024));
63+
},
6064
resource.length,
6165
// 与其他 sdk 保持一致,没有 filename 就是问号
6266
filename: filename ?? '?',

base/lib/src/storage/storage.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ export 'config/config.dart';
2020

2121
/// 客户端
2222
class Storage {
23-
late final Config config;
23+
final Config config;
2424
late final RequestTaskManager taskManager;
2525

26-
Storage({Config? config}) {
27-
this.config = config ?? Config();
26+
Storage({Config? config}) : config = config ?? Config() {
2827
taskManager = RequestTaskManager(config: this.config);
2928
}
3029

0 commit comments

Comments
 (0)