Skip to content

Commit

Permalink
Implement Future on SftpFileWriter for backward compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
xtyxtyx committed Mar 7, 2023
1 parent 412156b commit ae54479
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [2.8.2] - 2023-03-07
- Make `SftpFileWriter` implement `Future<void>` for backward compatibility.

## [2.8.1] - 2023-03-07
- Export `SftpFileWriter`

## [2.8.0] - 2023-03-06
- `SftpFile.write` now returns a `SftpFileWriter` that can be used to control
the writing process.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ await file.writeBytes(data, offset: 6);
```dart
final sftp = await client.sftp();
final file = await sftp.open('file.txt', mode: SftpFileOpenMode.create | SftpFileOpenMode.write);
await file.write(File('local_file.txt').openRead().cast()).done;
await file.write(File('local_file.txt').openRead().cast());
```

#### Pause and resume file upload
Expand Down
38 changes: 37 additions & 1 deletion lib/src/sftp/sftp_stream_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const chunkSize = 16 * 1024;
const maxBytesOnTheWire = chunkSize * 64;

/// Holds the state of a streaming write operation from [stream] to [file].
class SftpFileWriter {
class SftpFileWriter with DoneFuture {
/// The remote file to write to.
final SftpFile file;

Expand Down Expand Up @@ -61,8 +61,12 @@ class SftpFileWriter {
///
/// - All data from [stream] has been written to [file]
/// - Or the write operation has been aborted by calling [abort].
@override
Future<void> get done => _doneCompleter.future;

/// The number of bytes that have been successfully written to [file].
int get progress => _bytesAcked;

/// Stops [stream] from emitting more data. Returns a [Future] that completes
/// when the underlying data source of [stream] has been successfully closed.
///
Expand Down Expand Up @@ -113,3 +117,35 @@ class SftpFileWriter {
_streamDone = true;
}
}

/// Implements [Future] interface for [SftpFileWriter].
///
/// This is for compatibility with earlier versions of dartssh2.
mixin DoneFuture implements Future {
Future<void> get done;

@override
Stream<void> asStream() => done.asStream();

@override
Future<void> catchError(
Function onError, {
bool Function(Object error)? test,
}) =>
done.catchError(onError, test: test);

@override
Future<S> then<S>(FutureOr<S> Function(void) onValue, {Function? onError}) =>
done.then(onValue, onError: onError);

@override
Future<void> whenComplete(FutureOr Function() action) =>
done.whenComplete(action);

@override
Future<void> timeout(
Duration timeLimit, {
FutureOr<void> Function()? onTimeout,
}) =>
done.timeout(timeLimit, onTimeout: onTimeout);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dartssh2
version: 2.8.1
version: 2.8.2
description: SSH and SFTP client written in pure Dart, aiming to be feature-rich as well as easy to use.
# author is a deprecated pubspec field.
# author: Green Appers, Inc. <[email protected]>
Expand Down
12 changes: 12 additions & 0 deletions test/src/sftp/sftp_stream_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,17 @@ void main() {
await dataController.close();
await writer.done;
});

test('can be awaited', () async {
final sftp = await client.sftp();
final file = await sftp.open(
'a.out',
mode: SftpFileOpenMode.create | SftpFileOpenMode.write,
);

final uploader = file.write(Stream.value(Uint8List(100)));
await uploader;
expect(uploader.progress, 100);
});
});
}

0 comments on commit ae54479

Please sign in to comment.