From 43b3c7a3199ece24a728bed8c4e46d1ac3c1122d Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 7 Oct 2024 10:13:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=92=A1=20Update=20comments=20and=20st?= =?UTF-8?q?rings=20with=20`MultipartFile`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/multipart_file.dart | 74 +++++++++++++++++---------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/dio/lib/src/multipart_file.dart b/dio/lib/src/multipart_file.dart index 8d451a4d3..6e3686377 100644 --- a/dio/lib/src/multipart_file.dart +++ b/dio/lib/src/multipart_file.dart @@ -11,20 +11,18 @@ import 'utils.dart'; /// The type (alias) for specifying the content-type of the `MultipartFile`. typedef DioMediaType = MediaType; -/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to -/// correspond to a physical file. -/// -/// MultipartFile is based on stream, and a stream can be read only once, -/// so the same MultipartFile can't be read multiple times. +/// An upload content that is a part of `MultipartRequest`. +/// This doesn't need to correspond to a physical file. class MultipartFile { /// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length /// of the file in bytes must be known in advance. If it's not, read the data /// from the stream and use [MultipartFile.fromBytes] instead. /// - /// [contentType] currently defaults to `application/octet-stream`, but in the - /// future may be inferred from [filename]. + /// [contentType] currently defaults to `application/octet-stream`, + /// but may be inferred from [filename] in the future. @Deprecated( - 'MultipartFile.clone() will not work when the stream is provided, use the MultipartFile.fromStream instead.' + 'MultipartFile() is not cloneable when the stream is consumed, ' + 'use MultipartFile.fromStream() instead.' 'This will be removed in 6.0.0', ) MultipartFile( @@ -37,12 +35,13 @@ class MultipartFile { headers = caseInsensitiveKeyMap(headers), contentType = contentType ?? MediaType('application', 'octet-stream'); - /// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length - /// of the file in bytes must be known in advance. If it's not, read the data - /// from the stream and use [MultipartFile.fromBytes] instead. + /// Creates a new [MultipartFile] from a creation method that creates + /// chunked [Stream] of bytes. The length of the file in bytes must be known + /// in advance. If it's not, read the data from the stream and use + /// [MultipartFile.fromBytes] instead. /// - /// [contentType] currently defaults to `application/octet-stream`, but in the - /// future may be inferred from [filename]. + /// [contentType] currently defaults to `application/octet-stream`, + /// but may be inferred from [filename] in the future. MultipartFile.fromStream( Stream> Function() data, this.length, { @@ -55,8 +54,8 @@ class MultipartFile { /// Creates a new [MultipartFile] from a byte array. /// - /// [contentType] currently defaults to `application/octet-stream`, but in the - /// future may be inferred from [filename]. + /// [contentType] currently defaults to `application/octet-stream`, + /// but may be inferred from [filename] in the future. factory MultipartFile.fromBytes( List value, { String? filename, @@ -76,8 +75,9 @@ class MultipartFile { /// /// The encoding to use when translating [value] into bytes is taken from /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. - /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in - /// the future may be inferred from [filename]. + /// + /// [contentType] currently defaults to `text/plain; charset=utf-8`, + /// but may be inferred from [filename] in the future. factory MultipartFile.fromString( String value, { String? filename, @@ -90,7 +90,6 @@ class MultipartFile { utf8, ); contentType = contentType.change(parameters: {'charset': encoding.name}); - return MultipartFile.fromBytes( encoding.encode(value), filename: filename, @@ -131,26 +130,28 @@ class MultipartFile { String? filename, DioMediaType? contentType, final Map>? headers, - }) => - multipartFileFromPath( - filePath, - filename: filename, - contentType: contentType, - headers: headers, - ); + }) { + return multipartFileFromPath( + filePath, + filename: filename, + contentType: contentType, + headers: headers, + ); + } static MultipartFile fromFileSync( String filePath, { String? filename, DioMediaType? contentType, final Map>? headers, - }) => - multipartFileFromPathSync( - filePath, - filename: filename, - contentType: contentType, - headers: headers, - ); + }) { + return multipartFileFromPathSync( + filePath, + filename: filename, + contentType: contentType, + headers: headers, + ); + } bool _isFinalized = false; @@ -159,12 +160,15 @@ class MultipartFile { throw StateError( 'The MultipartFile has already been finalized. ' 'This typically means you are using ' - 'the same MultipartFile in repeated requests.', + 'the same MultipartFile in repeated requests.\n' + 'Use MultipartFile.clone() or creates a new MultipartFile ' + 'for further usages.', ); } _isFinalized = true; - return _dataBuilder() - .map((e) => e is Uint8List ? e : Uint8List.fromList(e)); + return _dataBuilder().map( + (e) => e is Uint8List ? e : Uint8List.fromList(e), + ); } /// Clone MultipartFile, returning a new instance of the same object. From 9786df79d4022e1e0520618a1759827bde8fe6a2 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 7 Oct 2024 13:37:58 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=85=20Fix=20string=20matching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/test/multipart_file_test.dart | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dio/test/multipart_file_test.dart b/dio/test/multipart_file_test.dart index b858736ee..273da52b1 100644 --- a/dio/test/multipart_file_test.dart +++ b/dio/test/multipart_file_test.dart @@ -63,8 +63,11 @@ void main() async { expect(e, isA()); expect( (e as StateError).message, - 'The MultipartFile has already been finalized. This typically ' - 'means you are using the same MultipartFile in repeated requests.', + 'The MultipartFile has already been finalized. ' + 'This typically means you are using the same MultipartFile ' + 'in repeated requests.\n' + 'Use MultipartFile.clone() or creates a new MultipartFile ' + 'for further usages.', ); } From 617741907ed64893b104ac8ee50fa24f259d341a Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 7 Oct 2024 14:23:53 +0800 Subject: [PATCH 3/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonas Uekötter Signed-off-by: Alex Li --- dio/lib/src/multipart_file.dart | 10 +++++----- dio/test/multipart_file_test.dart | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dio/lib/src/multipart_file.dart b/dio/lib/src/multipart_file.dart index 6e3686377..414bb050d 100644 --- a/dio/lib/src/multipart_file.dart +++ b/dio/lib/src/multipart_file.dart @@ -19,7 +19,7 @@ class MultipartFile { /// from the stream and use [MultipartFile.fromBytes] instead. /// /// [contentType] currently defaults to `application/octet-stream`, - /// but may be inferred from [filename] in the future. + /// but it may be inferred from [filename] in the future. @Deprecated( 'MultipartFile() is not cloneable when the stream is consumed, ' 'use MultipartFile.fromStream() instead.' @@ -41,7 +41,7 @@ class MultipartFile { /// [MultipartFile.fromBytes] instead. /// /// [contentType] currently defaults to `application/octet-stream`, - /// but may be inferred from [filename] in the future. + /// but it may be inferred from [filename] in the future. MultipartFile.fromStream( Stream> Function() data, this.length, { @@ -55,7 +55,7 @@ class MultipartFile { /// Creates a new [MultipartFile] from a byte array. /// /// [contentType] currently defaults to `application/octet-stream`, - /// but may be inferred from [filename] in the future. + /// but it may be inferred from [filename] in the future. factory MultipartFile.fromBytes( List value, { String? filename, @@ -77,7 +77,7 @@ class MultipartFile { /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. /// /// [contentType] currently defaults to `text/plain; charset=utf-8`, - /// but may be inferred from [filename] in the future. + /// but it may be inferred from [filename] in the future. factory MultipartFile.fromString( String value, { String? filename, @@ -161,7 +161,7 @@ class MultipartFile { 'The MultipartFile has already been finalized. ' 'This typically means you are using ' 'the same MultipartFile in repeated requests.\n' - 'Use MultipartFile.clone() or creates a new MultipartFile ' + 'Use MultipartFile.clone() or create a new MultipartFile ' 'for further usages.', ); } diff --git a/dio/test/multipart_file_test.dart b/dio/test/multipart_file_test.dart index 273da52b1..9684c9910 100644 --- a/dio/test/multipart_file_test.dart +++ b/dio/test/multipart_file_test.dart @@ -66,7 +66,7 @@ void main() async { 'The MultipartFile has already been finalized. ' 'This typically means you are using the same MultipartFile ' 'in repeated requests.\n' - 'Use MultipartFile.clone() or creates a new MultipartFile ' + 'Use MultipartFile.clone() or create a new MultipartFile ' 'for further usages.', ); } From 9d00d9725055bdc37658995f90adc8c8890e0d83 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 7 Oct 2024 14:24:51 +0800 Subject: [PATCH 4/4] Update CHANGELOG.md Signed-off-by: Alex Li --- dio/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index e319e0ed4..74142e260 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.** ## Unreleased -*None.* +- Update comments and strings with `MultipartFile`. ## 5.7.0