Skip to content

Commit a5a7dfa

Browse files
fjnoypdnys1
andauthored
Storage download progress (#928)
Co-authored-by: Dillon Nys <[email protected]>
1 parent fd12602 commit a5a7dfa

30 files changed

+535
-40
lines changed

packages/amplify_api_plugin_interface/lib/src/graphql/graphql_request.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
import '../uuid.dart';
16+
import 'package:amplify_core/types/uuid.dart';
1717

1818
class GraphQLRequest<T> {
1919
final String? apiName;

packages/amplify_api_plugin_interface/lib/src/types.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@ export 'rest/rest_exception.dart';
2929
export 'rest/rest_operation.dart';
3030
export 'rest/rest_options.dart';
3131
export 'rest/rest_response.dart';
32-
33-
export 'uuid.dart';

packages/amplify_core/lib/types/index.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ export 'hub/HubChannel.dart';
2525
export 'exception/AmplifyException.dart';
2626
export 'exception/AmplifyAlreadyConfiguredException.dart';
2727
export 'exception/AmplifyExceptionMessages.dart';
28+
29+
// UUID
30+
export 'uuid.dart';

packages/amplify_api_plugin_interface/lib/src/uuid.dart renamed to packages/amplify_core/lib/types/uuid.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
import 'package:uuid/uuid.dart';
1717

1818
class UUID {
19-
static const _internal = Uuid();
19+
static final _internal = Uuid();
2020

2121
static String getUUID() {
2222
return _internal.v4();

packages/amplify_core/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies:
1212
meta: ^1.3.0
1313
flutter:
1414
sdk: flutter
15+
uuid: ^3.0.1
1516

1617
dev_dependencies:
1718
flutter_test:

packages/amplify_flutter/lib/categories/amplify_storage_category.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ class StorageCategory {
3939
}
4040

4141
Future<UploadFileResult> uploadFile(
42-
{required File local, required String key, UploadFileOptions? options}) {
42+
{required File local,
43+
required String key,
44+
void Function(TransferProgress)? onProgress,
45+
UploadFileOptions? options}) {
4346
final UploadFileRequest request =
4447
UploadFileRequest(local: local, key: key, options: options);
45-
return plugins[0].uploadFile(request: request);
48+
return plugins[0].uploadFile(request: request, onProgress: onProgress);
4649
}
4750

4851
Future<GetUrlResult> getUrl({required String key, GetUrlOptions? options}) {
@@ -63,9 +66,10 @@ class StorageCategory {
6366
Future<DownloadFileResult> downloadFile(
6467
{required String key,
6568
required File local,
69+
void Function(TransferProgress)? onProgress,
6670
DownloadFileOptions? options}) {
6771
final DownloadFileRequest request =
6872
DownloadFileRequest(key: key, local: local, options: options);
69-
return plugins[0].downloadFile(request: request);
73+
return plugins[0].downloadFile(request: request, onProgress: onProgress);
7074
}
7175
}

packages/amplify_storage_plugin_interface/lib/amplify_storage_plugin_interface.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ abstract class StoragePluginInterface extends AmplifyPluginInterface {
2828
throw UnimplementedError('addPlugin() has not been implemented.');
2929
}
3030

31-
Future<UploadFileResult> uploadFile({required UploadFileRequest request}) {
31+
Future<UploadFileResult> uploadFile(
32+
{required UploadFileRequest request,
33+
void Function(TransferProgress)? onProgress}) {
3234
throw UnimplementedError('uploadFile() has not been implemented.');
3335
}
3436

@@ -45,7 +47,8 @@ abstract class StoragePluginInterface extends AmplifyPluginInterface {
4547
}
4648

4749
Future<DownloadFileResult> downloadFile(
48-
{required DownloadFileRequest request}) {
50+
{required DownloadFileRequest request,
51+
void Function(TransferProgress)? onProgress}) {
4952
throw UnimplementedError('downloadFile() has not been implemented.');
5053
}
5154
}

packages/amplify_storage_plugin_interface/lib/src/DownloadFile/DownloadFileRequest.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
import 'package:amplify_core/types/uuid.dart';
17+
1618
import './DownloadFileOptions.dart';
1719
import 'dart:io';
1820

1921
class DownloadFileRequest {
22+
late String uuid;
2023
String key;
2124
File local;
2225
DownloadFileOptions? options;
2326

24-
DownloadFileRequest({required this.key, required this.local, this.options});
27+
DownloadFileRequest({required this.key, required this.local, this.options}) {
28+
this.uuid = UUID.getUUID();
29+
}
2530

2631
Map<String, dynamic> serializeAsMap() {
2732
final Map<String, dynamic> result = {
28-
'path': local.absolute.path,
33+
'uuid': uuid,
2934
'key': key,
35+
'path': local.absolute.path,
3036
'options': options?.serializeAsMap()
3137
};
3238
result.removeWhere((_, v) => v == null);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
/// {@template amplify_storage_s3.transfer_progress}
17+
/// The progress of a storage transfer operation.
18+
/// {@endtemplate}
19+
class TransferProgress {
20+
/// The current progress, in bytes, for the storage transfer operation.
21+
final int currentBytes;
22+
23+
/// The total number of bytes for the storage transfer operation.
24+
final int totalBytes;
25+
26+
/// {@macro amplify_storage_s3.transfer_progress}
27+
const TransferProgress(this.currentBytes, this.totalBytes);
28+
29+
/// The fractional progress of the storage transfer operation.
30+
///
31+
/// 0 <= `fractionCompleted` <= 1
32+
double getFractionCompleted() {
33+
return currentBytes / totalBytes;
34+
}
35+
}

packages/amplify_storage_plugin_interface/lib/src/UploadFile/UploadFileRequest.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
import 'package:amplify_core/types/uuid.dart';
17+
1618
import './UploadFileOptions.dart';
1719
import 'dart:io';
1820

1921
class UploadFileRequest {
22+
late String uuid;
2023
File local;
2124
String key;
2225
UploadFileOptions? options;
2326

24-
UploadFileRequest({required this.local, required this.key, this.options});
27+
UploadFileRequest({required this.local, required this.key, this.options}) {
28+
this.uuid = UUID.getUUID();
29+
}
2530

2631
Map<String, dynamic> serializeAsMap() {
2732
final Map<String, dynamic> result = {
33+
'uuid': uuid,
2834
'path': local.absolute.path,
2935
'key': key,
3036
'options': options?.serializeAsMap()

0 commit comments

Comments
 (0)