Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing FileAccessMode for Dio.download #2281

Merged
merged 21 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fe609eb
Add ability to change [FileMode] of the download by introducing [Dio…
shehabmohamed0 Aug 14, 2024
3266e0f
Add append download test with the new [DioFileMode]
shehabmohamed0 Sep 1, 2024
53a762d
Merge branch 'main' into main
shehabmohamed0 Sep 3, 2024
cf5f6bf
Apply dart formatting
shehabmohamed0 Sep 5, 2024
87ad6d2
Merge branch 'main' into main
shehabmohamed0 Oct 15, 2024
66de32f
Merge branch 'main' into main
shehabmohamed0 Oct 21, 2024
8eee312
change dio_file_mode.dart to file_access_mode.dart
shehabmohamed0 Oct 23, 2024
0c4614c
Merge branch 'main' into main
shehabmohamed0 Oct 29, 2024
6f2d1ab
Merge branch 'main' into main
shehabmohamed0 Nov 18, 2024
9a9ee71
Merge branch 'main' into main
shehabmohamed0 Nov 30, 2024
27bb332
Apply suggestions from code review
shehabmohamed0 Dec 16, 2024
4276742
Moving FileAccessMode from "file_access_mode.dart" to "options.dart"
shehabmohamed0 Dec 16, 2024
238e185
Merge branch 'main' into main
shehabmohamed0 Dec 16, 2024
6c45ae5
Fix [FileAccessMode] comments in dio/lib/src/dio.dart
shehabmohamed0 Dec 17, 2024
c2a23e8
format dio_test/lib/src/test/download_tests.dart
shehabmohamed0 Dec 17, 2024
ef26620
Rename "mode" in `Dio.downloadUri` to "fileAccessMode"
shehabmohamed0 Dec 26, 2024
ff334f7
Update dio CHANGELOG
shehabmohamed0 Dec 26, 2024
f54c19c
Update web_adapter CHANGELOG
shehabmohamed0 Dec 26, 2024
b1bf656
Merge branch 'main' into main
shehabmohamed0 Jan 5, 2025
57e9d7e
Merge branch 'main' into main
shehabmohamed0 Jan 12, 2025
01fc6a7
Merge branch 'main' into main
shehabmohamed0 Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dio/lib/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'src/adapter.dart';
export 'src/cancel_token.dart';
export 'src/dio.dart';
export 'src/dio_exception.dart';
export 'src/dio_file_mode.dart';
export 'src/dio_mixin.dart' hide InterceptorState, InterceptorResultType;
export 'src/form_data.dart';
export 'src/headers.dart';
Expand Down
7 changes: 7 additions & 0 deletions dio/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'cancel_token.dart';
import 'dio/dio_for_native.dart'
if (dart.library.js_interop) 'dio/dio_for_browser.dart'
if (dart.library.html) 'dio/dio_for_browser.dart';
import 'dio_file_mode.dart';
import 'dio_mixin.dart';
import 'headers.dart';
import 'options.dart';
Expand Down Expand Up @@ -209,6 +210,9 @@ abstract class Dio {
/// [deleteOnError] whether delete the file when error occurs.
/// The default value is [true].
///
/// [fileMode]
/// {@macro dio.DioFileMode}
///
/// [lengthHeader] : The real size of original file (not compressed).
/// When file is compressed:
/// 1. If this value is 'content-length', the `total` argument of
Expand Down Expand Up @@ -242,6 +246,7 @@ abstract class Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode fileMode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -254,6 +259,7 @@ abstract class Dio {
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode mode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -266,6 +272,7 @@ abstract class Dio {
deleteOnError: deleteOnError,
cancelToken: cancelToken,
data: data,
fileMode: mode,
options: options,
);
}
Expand Down
9 changes: 8 additions & 1 deletion dio/lib/src/dio/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../adapters/io_adapter.dart';
import '../cancel_token.dart';
import '../dio.dart';
import '../dio_exception.dart';
import '../dio_file_mode.dart';
import '../dio_mixin.dart';
import '../headers.dart';
import '../options.dart';
Expand All @@ -32,6 +33,7 @@ class DioForNative with DioMixin implements Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode fileMode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand Down Expand Up @@ -95,7 +97,12 @@ class DioForNative with DioMixin implements Dio {
// Shouldn't call file.writeAsBytesSync(list, flush: flush),
// because it can write all bytes by once. Consider that the file is
// a very big size (up to 1 Gigabytes), it will be expensive in memory.
RandomAccessFile raf = file.openSync(mode: FileMode.write);
RandomAccessFile raf = file.openSync(
mode: fileMode.map(
write: () => FileMode.write,
append: () => FileMode.append,
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
),
);

// Create a Completer to notify the success/error state.
final completer = Completer<Response>();
Expand Down
24 changes: 24 additions & 0 deletions dio/lib/src/dio_file_mode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// {@template dio.DioFileMode}
/// The file access mode when downloading the file.
/// - [DioFileMode.write]: Mode for opening a file for reading and writing.
/// The file is overwritten if it already exists. The file is created if it
/// does not already exist
/// - [DioFileMode.append]: Mode for opening a file for reading and writing
/// to the end of it. The file is created if it does not already exist.
/// {@endtemplate}
enum DioFileMode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is a bit overwhelming. The type is only used for the download process.

Also the enhanced enum is not working on our minimum SDK support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will convert it to extension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any suggestion for naming

write,
append;

T map<T>({
required T Function() write,
required T Function() append,
}) {
switch (this) {
case DioFileMode.write:
return write();
case DioFileMode.append:
return append();
}
}
}
3 changes: 3 additions & 0 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'adapter.dart';
import 'cancel_token.dart';
import 'dio.dart';
import 'dio_exception.dart';
import 'dio_file_mode.dart';
import 'form_data.dart';
import 'headers.dart';
import 'interceptors/imply_content_type.dart';
Expand Down Expand Up @@ -286,6 +287,7 @@ abstract class DioMixin implements Dio {
ProgressCallback? onReceiveProgress,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode mode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand All @@ -310,6 +312,7 @@ abstract class DioMixin implements Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode fileMode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand Down
48 changes: 48 additions & 0 deletions dio_test/lib/src/test/download_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,54 @@ void downloadTests(
completes,
);
});
test('append bytes previous download', () async {
final cancelToken = CancelToken();
final path = p.join(tmp.path, 'download_3.txt');
final requestedBytes = 1024 * 1024 * 10;
var recievedBytes1 = 0;
await expectLater(
dio.download(
'/bytes/$requestedBytes',
path,
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
if (c > 5000) {
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
recievedBytes1 = c;
cancelToken.cancel();
}
},
deleteOnError: false,
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/download_tests.dart',
),
);

final cancelToken2 = CancelToken();
var recievedBytes2 = 0;
shehabmohamed0 marked this conversation as resolved.
Show resolved Hide resolved
expectLater(
dio.download(
'/bytes/$requestedBytes',
path,
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
recievedBytes2 = c;
if (c > 5000) {
cancelToken2.cancel();
}
},
deleteOnError: false,
fileMode: DioFileMode.append,
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/download_tests.dart',
),
);
await Future.delayed(const Duration(milliseconds: 100), () {});
expect(File(path).lengthSync(), recievedBytes1 + recievedBytes2);
});
},
testOn: 'vm',
);
Expand Down
1 change: 1 addition & 0 deletions plugins/web_adapter/lib/src/dio_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DioForBrowser with DioMixin implements Dio {
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
bool deleteOnError = true,
DioFileMode fileMode = DioFileMode.write,
String lengthHeader = Headers.contentLengthHeader,
Object? data,
Options? options,
Expand Down
Loading