Skip to content

Commit

Permalink
Fix for Dio.download not cleaning the file on data handling error (#1915
Browse files Browse the repository at this point in the history
)

<!-- Write down your pull request descriptions. -->

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

<!-- Provide more context and info about the PR. -->

---------

Signed-off-by: hgraceb <[email protected]>
Co-authored-by: Flop <[email protected]>
  • Loading branch information
hgraceb and hgraceb authored Aug 2, 2023
1 parent 0b87202 commit 074d428
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
## Unreleased

- Revert removed `download` for `DioMixin`.
- Fix for `Dio.download` not cleaning the file on data handling error.

## 5.3.1

Expand Down
11 changes: 8 additions & 3 deletions dio/lib/src/dio/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ class DioForNative with DioMixin implements Dio {
if (!closed) {
closed = true;
await asyncWrite;
await raf.close();
await raf.close().catchError((_) => raf);
if (deleteOnError && file.existsSync()) {
await file.delete();
await file.delete().catchError((_) => file);
}
}
}
Expand All @@ -140,6 +140,11 @@ class DioForNative with DioMixin implements Dio {
}).catchError((Object e) async {
try {
await subscription.cancel();
closed = true;
await raf.close().catchError((_) => raf);
if (deleteOnError && file.existsSync()) {
await file.delete().catchError((_) => file);
}
} finally {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
Expand All @@ -151,7 +156,7 @@ class DioForNative with DioMixin implements Dio {
try {
await asyncWrite;
closed = true;
await raf.close();
await raf.close().catchError((_) => raf);
completer.complete(response);
} catch (e) {
completer.completeError(
Expand Down
43 changes: 43 additions & 0 deletions dio/test/download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,49 @@ void main() {
);
});

test('delete on error', () async {
const savePath = 'test/_download_test.md';
final f = File(savePath)..createSync(recursive: true);
expect(f.existsSync(), isTrue);

final dio = Dio()..options.baseUrl = serverUrl.toString();
await expectLater(
dio
.download(
'/download',
savePath,
deleteOnError: true,
onReceiveProgress: (count, total) => throw AssertionError(),
)
.catchError((e) => throw (e as DioException).error!),
throwsA(isA<AssertionError>()),
);
expect(f.existsSync(), isFalse);
});

test('delete on cancel', () async {
const savePath = 'test/_download_test.md';
final f = File(savePath)..createSync(recursive: true);
expect(f.existsSync(), isTrue);

final cancelToken = CancelToken();
final dio = Dio()..options.baseUrl = serverUrl.toString();
await expectLater(
dio
.download(
'/download',
savePath,
deleteOnError: true,
cancelToken: cancelToken,
onReceiveProgress: (count, total) => cancelToken.cancel(),
)
.catchError((e) => throw (e as DioException).type),
throwsA(DioExceptionType.cancel),
);
await Future.delayed(const Duration(milliseconds: 100));
expect(f.existsSync(), isFalse);
});

test('`savePath` types', () async {
final testPath = p.join(Directory.systemTemp.path, 'dio', 'testPath');

Expand Down

0 comments on commit 074d428

Please sign in to comment.