Skip to content

Commit

Permalink
chore(aws_common): move AWSFile to aws_common package (#2365)
Browse files Browse the repository at this point in the history
Moves AWSFile into aws_common and expose AWSFilePlatform from aws_common/web.dart and aws_common/vm.dart libraries.
  • Loading branch information
HuiSF authored Nov 15, 2022
1 parent 8b1c2ef commit 49081dc
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 80 deletions.
3 changes: 0 additions & 3 deletions packages/amplify_core/lib/amplify_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ export 'src/hub/amplify_hub.dart';
export 'src/hub/hub_channel.dart';
export 'src/hub/hub_event.dart';

// io
export 'src/io/aws_file.dart';

// Logger
export 'src/logger/amplify_logger.dart';

Expand Down
4 changes: 4 additions & 0 deletions packages/aws_common/lib/aws_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export 'src/credentials/credentials_provider.dart';
// Exception
export 'src/exception/aws_http_exception.dart';
export 'src/exception/cancellation_exception.dart';
export 'src/exception/invalid_file_exception.dart';

// HTTP
export 'src/http/alpn_protocol.dart';
Expand All @@ -46,6 +47,9 @@ export 'src/http/aws_http_response.dart';
export 'src/http/http_payload.dart';
export 'src/http/x509_certificate.dart';

// IO
export 'src/io/aws_file.dart';

// Logging
export 'src/logging/aws_logger.dart';
export 'src/logging/log_entry.dart';
Expand Down
30 changes: 30 additions & 0 deletions packages/aws_common/lib/src/exception/invalid_file_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// {@template aws_common.invalid_file_exception}
/// The file created from AWSFile is not readable.
/// {@endtemplate}
class InvalidFileException implements Exception {
/// {@macro aws_common.invalid_file_exception}
const InvalidFileException({
this.message = 'Invalid file.',
this.recoverySuggestion = 'Make sure to initialize AWSFile correctly.',
});

/// The error message.
final String message;

/// The recover suggestion for the [InvalidFileException].
final String recoverySuggestion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// limitations under the License.

import 'package:async/async.dart';
import 'aws_file_platform.dart'
import 'package:aws_common/aws_common.dart';
import 'package:aws_common/src/io/aws_file_platform.dart'
if (dart.library.html) 'aws_file_platform_html.dart'
if (dart.library.io) 'aws_file_platform_io.dart';
import 'package:meta/meta.dart';

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

/// Protected constructor of [AWSFile].
@protected
AWSFile.protected({
this.stream,
this.path,
this.bytes,
this.name,
this.contentType,
});

final Stream<List<int>>? stream;
/// Stream of the file content.
Stream<List<int>> get stream;

/// The cached bytes content of the file.
final List<int>? bytes;

/// The name of the file if provided or read from OS.
final String? name;

/// The path of the file if provided.
final String? path;

/// The content type of the file if provided.
final String? contentType;

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

/// Size of the file.
Future<int> get size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,30 @@

import 'dart:async';

import 'package:amplify_core/src/io/aws_file.dart';
import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';

/// {@template amplify_core.io.aws_file_platform}
/// A cross platform implementation of [AWSFile].
/// {@endtemplate}
class AWSFilePlatform extends AWSFile {
/// {@macro amplify_core.io.aws_file_platform}
AWSFilePlatform() : super.protected();

/// {@macro amplify_core.io.aws_file.from_stream}
AWSFilePlatform.fromStream(
Stream<List<int>> stream, {
String? name,
String? contentType,
super.name,
super.contentType,
required int size,
}) : super.protected(
stream: stream,
name: name,
contentType: contentType,
);
}) : super.protected();

/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromPath(
String path, {
String? name,
super.name,
}) : super.protected(
path: path,
name: name,
) {
throw UnimplementedError(
'AWSFile is not available in the current platform',
Expand All @@ -53,12 +49,10 @@ class AWSFilePlatform extends AWSFile {
/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromData(
List<int> data, {
String? name,
String? contentType,
super.name,
super.contentType,
}) : super.protected(
bytes: data,
name: name,
contentType: contentType,
);

@override
Expand All @@ -68,6 +62,13 @@ class AWSFilePlatform extends AWSFile {
);
}

@override
Stream<List<int>> get stream {
throw UnimplementedError(
'stream getter has not been implemented in the current platform.',
);
}

/// {@macro amplify_core.io.aws_file.chunked_reader}
@override
ChunkedStreamReader<int> getChunkedStreamReader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import 'dart:async';
import 'dart:html';

import 'package:amplify_core/src/io/aws_file.dart';
import 'package:amplify_core/src/io/exception/invalid_file.dart';
import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';

// Dart io.File openRead chunk size
const _readStreamChunkSize = 64 * 1024;
Expand All @@ -26,62 +25,60 @@ const _readStreamChunkSize = 64 * 1024;
class AWSFilePlatform extends AWSFile {
/// Creates an [AWSFile] from html [File].
AWSFilePlatform.fromFile(File file)
: _inputFile = file,
: _stream = null,
_inputFile = file,
_inputBlob = null,
_size = file.size,
super.protected();

/// Creates an [AWSFile] from html [Blob].
AWSFilePlatform.fromBlob(Blob blob)
: _inputBlob = blob,
: _stream = null,
_inputBlob = blob,
_inputFile = null,
_size = blob.size,
super.protected();

/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromPath(
String path, {
String? name,
}) : _inputFile = null,
super.name,
}) : _stream = null,
_inputFile = null,
_inputBlob = null,
_size = null,
super.protected(
name: name,
path: path,
);

/// {@macro amplify_core.io.aws_file.from_stream}
AWSFilePlatform.fromStream(
Stream<List<int>> stream, {
String? name,
String? contentType,
super.name,
super.contentType,
required int size,
}) : _inputFile = null,
}) : _stream = stream,
_inputFile = null,
_inputBlob = null,
_size = size,
super.protected(
stream: stream,
name: name,
contentType: contentType,
);
super.protected();

/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromData(
List<int> data, {
String? name,
String? contentType,
}) : _inputBlob = Blob([data], contentType),
super.name,
super.contentType,
}) : _stream = null,
_inputBlob = Blob([data], contentType),
_inputFile = null,
_size = data.length,
super.protected(
bytes: data,
name: name,
contentType: contentType,
stream: Stream.value(data),
);

final File? _inputFile;
final Blob? _inputBlob;
final Stream<List<int>>? _stream;
int? _size;
Blob? _resolvedBlobFromPath;
String? _contentType;
Expand All @@ -96,7 +93,7 @@ class AWSFilePlatform extends AWSFile {
return _getReadStream(file);
}

final inputStream = super.stream;
final inputStream = _stream;
if (inputStream != null) {
return inputStream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,55 @@

import 'dart:io';

import 'package:amplify_core/src/io/aws_file.dart';
import 'package:amplify_core/src/io/exception/invalid_file.dart';
import 'package:async/async.dart';
import 'package:aws_common/aws_common.dart';

/// The io implementation of [AWSFile].
class AWSFilePlatform extends AWSFile {
/// Creates an [AWSFile] from io [File].
AWSFilePlatform.fromFile(File file)
: _inputFile = file,
: _stream = null,
_inputFile = file,
_size = null,
super.protected();

/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromPath(
String path, {
String? name,
}) : _inputFile = File(path),
super.name,
}) : _stream = null,
_inputFile = File(path),
_size = null,
super.protected(
name: name,
path: path,
);

/// {@macro amplify_core.io.aws_file.from_stream}
AWSFilePlatform.fromStream(
Stream<List<int>> inputStream, {
String? name,
String? contentType,
super.name,
super.contentType,
required int size,
}) : _size = size,
}) : _stream = inputStream,
_size = size,
_inputFile = null,
super.protected(
name: name,
stream: inputStream,
contentType: contentType,
);
super.protected();

/// {@macro amplify_core.io.aws_file.from_path}
AWSFilePlatform.fromData(
List<int> data, {
String? name,
String? contentType,
}) : _inputFile = null,
super.name,
super.contentType,
}) : _stream = null,
_inputFile = null,
_size = data.length,
super.protected(
bytes: data,
name: name,
contentType: contentType,
);

final File? _inputFile;
final int? _size;
final Stream<List<int>>? _stream;

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

final stream = super.stream;
final stream = _stream;
if (stream != null) {
return stream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

class InvalidFileException extends AmplifyException {
const InvalidFileException({
String? message,
String? recoverySuggestion,
}) : super(
message ?? 'Invalid file.',
recoverySuggestion: recoverySuggestion ??
'Make sure to initialize AWSFile correctly.',
);
}
export 'src/io/aws_file_platform_io.dart';
1 change: 1 addition & 0 deletions packages/aws_common/lib/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
/// Web-specific types and utilities used across AWS and Amplify packages.
library aws_common.web;

export 'src/io/aws_file_platform_html.dart';
export 'src/util/get_base_element_href_from_dom.dart';
Loading

0 comments on commit 49081dc

Please sign in to comment.