Skip to content

Commit 49081dc

Browse files
authored
chore(aws_common): move AWSFile to aws_common package (#2365)
Moves AWSFile into aws_common and expose AWSFilePlatform from aws_common/web.dart and aws_common/vm.dart libraries.
1 parent 8b1c2ef commit 49081dc

File tree

14 files changed

+114
-80
lines changed

14 files changed

+114
-80
lines changed

packages/amplify_core/lib/amplify_core.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export 'src/hub/amplify_hub.dart';
4646
export 'src/hub/hub_channel.dart';
4747
export 'src/hub/hub_event.dart';
4848

49-
// io
50-
export 'src/io/aws_file.dart';
51-
5249
// Logger
5350
export 'src/logger/amplify_logger.dart';
5451

packages/aws_common/lib/aws_common.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export 'src/credentials/credentials_provider.dart';
3434
// Exception
3535
export 'src/exception/aws_http_exception.dart';
3636
export 'src/exception/cancellation_exception.dart';
37+
export 'src/exception/invalid_file_exception.dart';
3738

3839
// HTTP
3940
export 'src/http/alpn_protocol.dart';
@@ -46,6 +47,9 @@ export 'src/http/aws_http_response.dart';
4647
export 'src/http/http_payload.dart';
4748
export 'src/http/x509_certificate.dart';
4849

50+
// IO
51+
export 'src/io/aws_file.dart';
52+
4953
// Logging
5054
export 'src/logging/aws_logger.dart';
5155
export 'src/logging/log_entry.dart';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/// {@template aws_common.invalid_file_exception}
16+
/// The file created from AWSFile is not readable.
17+
/// {@endtemplate}
18+
class InvalidFileException implements Exception {
19+
/// {@macro aws_common.invalid_file_exception}
20+
const InvalidFileException({
21+
this.message = 'Invalid file.',
22+
this.recoverySuggestion = 'Make sure to initialize AWSFile correctly.',
23+
});
24+
25+
/// The error message.
26+
final String message;
27+
28+
/// The recover suggestion for the [InvalidFileException].
29+
final String recoverySuggestion;
30+
}

packages/amplify_core/lib/src/io/aws_file.dart renamed to packages/aws_common/lib/src/io/aws_file.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
// limitations under the License.
1414

1515
import 'package:async/async.dart';
16-
import 'aws_file_platform.dart'
16+
import 'package:aws_common/aws_common.dart';
17+
import 'package:aws_common/src/io/aws_file_platform.dart'
1718
if (dart.library.html) 'aws_file_platform_html.dart'
1819
if (dart.library.io) 'aws_file_platform_io.dart';
20+
import 'package:meta/meta.dart';
1921

2022
/// {@template amplify_core.io.aws_file}
2123
/// A cross-platform abstraction over a read-only file.
@@ -108,24 +110,35 @@ abstract class AWSFile {
108110
String? contentType,
109111
}) = AWSFilePlatform.fromData;
110112

113+
/// Protected constructor of [AWSFile].
114+
@protected
111115
AWSFile.protected({
112-
this.stream,
113116
this.path,
114117
this.bytes,
115118
this.name,
116119
this.contentType,
117120
});
118121

119-
final Stream<List<int>>? stream;
122+
/// Stream of the file content.
123+
Stream<List<int>> get stream;
124+
125+
/// The cached bytes content of the file.
120126
final List<int>? bytes;
127+
128+
/// The name of the file if provided or read from OS.
121129
final String? name;
130+
131+
/// The path of the file if provided.
122132
final String? path;
133+
134+
/// The content type of the file if provided.
123135
final String? contentType;
124136

125137
/// {@template amplify_core.io.aws_file.chunked_reader}
126138
/// Returns a [ChunkedStreamReader] over the stream of bytes of the file.
127139
/// {@endtemplate}
128140
ChunkedStreamReader<int> getChunkedStreamReader();
129141

142+
/// Size of the file.
130143
Future<int> get size;
131144
}

packages/amplify_core/lib/src/io/aws_file_platform.dart renamed to packages/aws_common/lib/src/io/aws_file_platform.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,30 @@
1616

1717
import 'dart:async';
1818

19-
import 'package:amplify_core/src/io/aws_file.dart';
2019
import 'package:async/async.dart';
20+
import 'package:aws_common/aws_common.dart';
2121

2222
/// {@template amplify_core.io.aws_file_platform}
2323
/// A cross platform implementation of [AWSFile].
2424
/// {@endtemplate}
2525
class AWSFilePlatform extends AWSFile {
26+
/// {@macro amplify_core.io.aws_file_platform}
2627
AWSFilePlatform() : super.protected();
2728

2829
/// {@macro amplify_core.io.aws_file.from_stream}
2930
AWSFilePlatform.fromStream(
3031
Stream<List<int>> stream, {
31-
String? name,
32-
String? contentType,
32+
super.name,
33+
super.contentType,
3334
required int size,
34-
}) : super.protected(
35-
stream: stream,
36-
name: name,
37-
contentType: contentType,
38-
);
35+
}) : super.protected();
3936

4037
/// {@macro amplify_core.io.aws_file.from_path}
4138
AWSFilePlatform.fromPath(
4239
String path, {
43-
String? name,
40+
super.name,
4441
}) : super.protected(
4542
path: path,
46-
name: name,
4743
) {
4844
throw UnimplementedError(
4945
'AWSFile is not available in the current platform',
@@ -53,12 +49,10 @@ class AWSFilePlatform extends AWSFile {
5349
/// {@macro amplify_core.io.aws_file.from_path}
5450
AWSFilePlatform.fromData(
5551
List<int> data, {
56-
String? name,
57-
String? contentType,
52+
super.name,
53+
super.contentType,
5854
}) : super.protected(
5955
bytes: data,
60-
name: name,
61-
contentType: contentType,
6256
);
6357

6458
@override
@@ -68,6 +62,13 @@ class AWSFilePlatform extends AWSFile {
6862
);
6963
}
7064

65+
@override
66+
Stream<List<int>> get stream {
67+
throw UnimplementedError(
68+
'stream getter has not been implemented in the current platform.',
69+
);
70+
}
71+
7172
/// {@macro amplify_core.io.aws_file.chunked_reader}
7273
@override
7374
ChunkedStreamReader<int> getChunkedStreamReader() {

packages/amplify_core/lib/src/io/aws_file_platform_html.dart renamed to packages/aws_common/lib/src/io/aws_file_platform_html.dart

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
import 'dart:async';
1616
import 'dart:html';
1717

18-
import 'package:amplify_core/src/io/aws_file.dart';
19-
import 'package:amplify_core/src/io/exception/invalid_file.dart';
2018
import 'package:async/async.dart';
19+
import 'package:aws_common/aws_common.dart';
2120

2221
// Dart io.File openRead chunk size
2322
const _readStreamChunkSize = 64 * 1024;
@@ -26,62 +25,60 @@ const _readStreamChunkSize = 64 * 1024;
2625
class AWSFilePlatform extends AWSFile {
2726
/// Creates an [AWSFile] from html [File].
2827
AWSFilePlatform.fromFile(File file)
29-
: _inputFile = file,
28+
: _stream = null,
29+
_inputFile = file,
3030
_inputBlob = null,
3131
_size = file.size,
3232
super.protected();
3333

3434
/// Creates an [AWSFile] from html [Blob].
3535
AWSFilePlatform.fromBlob(Blob blob)
36-
: _inputBlob = blob,
36+
: _stream = null,
37+
_inputBlob = blob,
3738
_inputFile = null,
3839
_size = blob.size,
3940
super.protected();
4041

4142
/// {@macro amplify_core.io.aws_file.from_path}
4243
AWSFilePlatform.fromPath(
4344
String path, {
44-
String? name,
45-
}) : _inputFile = null,
45+
super.name,
46+
}) : _stream = null,
47+
_inputFile = null,
4648
_inputBlob = null,
4749
_size = null,
4850
super.protected(
49-
name: name,
5051
path: path,
5152
);
5253

5354
/// {@macro amplify_core.io.aws_file.from_stream}
5455
AWSFilePlatform.fromStream(
5556
Stream<List<int>> stream, {
56-
String? name,
57-
String? contentType,
57+
super.name,
58+
super.contentType,
5859
required int size,
59-
}) : _inputFile = null,
60+
}) : _stream = stream,
61+
_inputFile = null,
6062
_inputBlob = null,
6163
_size = size,
62-
super.protected(
63-
stream: stream,
64-
name: name,
65-
contentType: contentType,
66-
);
64+
super.protected();
6765

6866
/// {@macro amplify_core.io.aws_file.from_path}
6967
AWSFilePlatform.fromData(
7068
List<int> data, {
71-
String? name,
72-
String? contentType,
73-
}) : _inputBlob = Blob([data], contentType),
69+
super.name,
70+
super.contentType,
71+
}) : _stream = null,
72+
_inputBlob = Blob([data], contentType),
7473
_inputFile = null,
7574
_size = data.length,
7675
super.protected(
7776
bytes: data,
78-
name: name,
79-
contentType: contentType,
80-
stream: Stream.value(data),
8177
);
8278

8379
final File? _inputFile;
8480
final Blob? _inputBlob;
81+
final Stream<List<int>>? _stream;
8582
int? _size;
8683
Blob? _resolvedBlobFromPath;
8784
String? _contentType;
@@ -96,7 +93,7 @@ class AWSFilePlatform extends AWSFile {
9693
return _getReadStream(file);
9794
}
9895

99-
final inputStream = super.stream;
96+
final inputStream = _stream;
10097
if (inputStream != null) {
10198
return inputStream;
10299
}

packages/amplify_core/lib/src/io/aws_file_platform_io.dart renamed to packages/aws_common/lib/src/io/aws_file_platform_io.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,55 @@
1414

1515
import 'dart:io';
1616

17-
import 'package:amplify_core/src/io/aws_file.dart';
18-
import 'package:amplify_core/src/io/exception/invalid_file.dart';
1917
import 'package:async/async.dart';
18+
import 'package:aws_common/aws_common.dart';
2019

2120
/// The io implementation of [AWSFile].
2221
class AWSFilePlatform extends AWSFile {
2322
/// Creates an [AWSFile] from io [File].
2423
AWSFilePlatform.fromFile(File file)
25-
: _inputFile = file,
24+
: _stream = null,
25+
_inputFile = file,
2626
_size = null,
2727
super.protected();
2828

2929
/// {@macro amplify_core.io.aws_file.from_path}
3030
AWSFilePlatform.fromPath(
3131
String path, {
32-
String? name,
33-
}) : _inputFile = File(path),
32+
super.name,
33+
}) : _stream = null,
34+
_inputFile = File(path),
3435
_size = null,
3536
super.protected(
36-
name: name,
3737
path: path,
3838
);
3939

4040
/// {@macro amplify_core.io.aws_file.from_stream}
4141
AWSFilePlatform.fromStream(
4242
Stream<List<int>> inputStream, {
43-
String? name,
44-
String? contentType,
43+
super.name,
44+
super.contentType,
4545
required int size,
46-
}) : _size = size,
46+
}) : _stream = inputStream,
47+
_size = size,
4748
_inputFile = null,
48-
super.protected(
49-
name: name,
50-
stream: inputStream,
51-
contentType: contentType,
52-
);
49+
super.protected();
5350

5451
/// {@macro amplify_core.io.aws_file.from_path}
5552
AWSFilePlatform.fromData(
5653
List<int> data, {
57-
String? name,
58-
String? contentType,
59-
}) : _inputFile = null,
54+
super.name,
55+
super.contentType,
56+
}) : _stream = null,
57+
_inputFile = null,
6058
_size = data.length,
6159
super.protected(
6260
bytes: data,
63-
name: name,
64-
contentType: contentType,
6561
);
6662

6763
final File? _inputFile;
6864
final int? _size;
65+
final Stream<List<int>>? _stream;
6966

7067
@override
7168
Stream<List<int>> get stream {
@@ -74,7 +71,7 @@ class AWSFilePlatform extends AWSFile {
7471
return file.openRead();
7572
}
7673

77-
final stream = super.stream;
74+
final stream = _stream;
7875
if (stream != null) {
7976
return stream;
8077
}

packages/amplify_core/lib/src/io/exception/invalid_file.dart renamed to packages/aws_common/lib/vm.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import 'package:amplify_core/src/types/exception/amplify_exception.dart';
15+
/// VM-specific types and utilities used across AWS and Amplify packages.
16+
library aws_common.vm;
1617

17-
class InvalidFileException extends AmplifyException {
18-
const InvalidFileException({
19-
String? message,
20-
String? recoverySuggestion,
21-
}) : super(
22-
message ?? 'Invalid file.',
23-
recoverySuggestion: recoverySuggestion ??
24-
'Make sure to initialize AWSFile correctly.',
25-
);
26-
}
18+
export 'src/io/aws_file_platform_io.dart';

packages/aws_common/lib/web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
/// Web-specific types and utilities used across AWS and Amplify packages.
1616
library aws_common.web;
1717

18+
export 'src/io/aws_file_platform_html.dart';
1819
export 'src/util/get_base_element_href_from_dom.dart';

0 commit comments

Comments
 (0)