This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[image_picker_platform_interface] Introduce new PickedFile APIs. (#2791)
* Make API Async, so web can use objectUrls internally, instead of bytes. * Introduce the PickedFile class to have a more platform agnostic return. * Modify the platform interface to return PickedFiles. Run tests with flutter test / flutter test --platform chrome Co-authored-by: Rody Davis <rody.davis.jr@gmail.com>
- Loading branch information
Showing
17 changed files
with
798 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/image_picker/image_picker_platform_interface/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/image_picker/image_picker_platform_interface/lib/src/types/picked_file/base.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:meta/meta.dart'; | ||
|
||
/// The interface for a PickedFile. | ||
/// | ||
/// A PickedFile is a container that wraps the path of a selected | ||
/// file by the user and (in some platforms, like web) the bytes | ||
/// with the contents of the file. | ||
/// | ||
/// This class is a very limited subset of dart:io [File], so all | ||
/// the methods should seem familiar. | ||
@immutable | ||
abstract class PickedFileBase { | ||
/// Construct a PickedFile | ||
PickedFileBase(String path); | ||
|
||
/// Get the path of the picked file. | ||
/// | ||
/// This should only be used as a backwards-compatibility clutch | ||
/// for mobile apps, or cosmetic reasons only (to show the user | ||
/// the path they've picked). | ||
/// | ||
/// Accessing the data contained in the picked file by its path | ||
/// is platform-dependant (and won't work on web), so use the | ||
/// byte getters in the PickedFile instance instead. | ||
String get path { | ||
throw UnimplementedError('.path has not been implemented.'); | ||
} | ||
|
||
/// Synchronously read the entire file contents as a string using the given [Encoding]. | ||
/// | ||
/// By default, `encoding` is [utf8]. | ||
/// | ||
/// Throws Exception if the operation fails. | ||
Future<String> readAsString({Encoding encoding = utf8}) { | ||
throw UnimplementedError('readAsString() has not been implemented.'); | ||
} | ||
|
||
/// Synchronously read the entire file contents as a list of bytes. | ||
/// | ||
/// Throws Exception if the operation fails. | ||
Future<Uint8List> readAsBytes() { | ||
throw UnimplementedError('readAsBytes() has not been implemented.'); | ||
} | ||
|
||
/// Create a new independent [Stream] for the contents of this file. | ||
/// | ||
/// If `start` is present, the file will be read from byte-offset `start`. Otherwise from the beginning (index 0). | ||
/// | ||
/// If `end` is present, only up to byte-index `end` will be read. Otherwise, until end of file. | ||
/// | ||
/// In order to make sure that system resources are freed, the stream must be read to completion or the subscription on the stream must be cancelled. | ||
Stream<Uint8List> openRead([int start, int end]) { | ||
throw UnimplementedError('openRead() has not been implemented.'); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/image_picker/image_picker_platform_interface/lib/src/types/picked_file/html.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:http/http.dart' as http show readBytes; | ||
|
||
import './base.dart'; | ||
|
||
/// A PickedFile that works on web. | ||
/// | ||
/// It wraps the bytes of a selected file. | ||
class PickedFile extends PickedFileBase { | ||
final String path; | ||
final Uint8List _initBytes; | ||
|
||
/// Construct a PickedFile object from its ObjectUrl. | ||
/// | ||
/// Optionally, this can be initialized with `bytes` | ||
/// so no http requests are performed to retrieve files later. | ||
PickedFile(this.path, {Uint8List bytes}) | ||
: _initBytes = bytes, | ||
super(path); | ||
|
||
Future<Uint8List> get _bytes async { | ||
if (_initBytes != null) { | ||
return Future.value(UnmodifiableUint8ListView(_initBytes)); | ||
} | ||
return http.readBytes(path); | ||
} | ||
|
||
@override | ||
Future<String> readAsString({Encoding encoding = utf8}) async { | ||
return encoding.decode(await _bytes); | ||
} | ||
|
||
@override | ||
Future<Uint8List> readAsBytes() async { | ||
return Future.value(await _bytes); | ||
} | ||
|
||
@override | ||
Stream<Uint8List> openRead([int start, int end]) async* { | ||
final bytes = await _bytes; | ||
yield bytes.sublist(start ?? 0, end ?? bytes.length); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/image_picker/image_picker_platform_interface/lib/src/types/picked_file/io.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
import './base.dart'; | ||
|
||
/// A PickedFile backed by a dart:io File. | ||
class PickedFile extends PickedFileBase { | ||
final File _file; | ||
|
||
/// Construct a PickedFile object backed by a dart:io File. | ||
PickedFile(String path) | ||
: _file = File(path), | ||
super(path); | ||
|
||
@override | ||
String get path { | ||
return _file.path; | ||
} | ||
|
||
@override | ||
Future<String> readAsString({Encoding encoding = utf8}) { | ||
return _file.readAsString(encoding: encoding); | ||
} | ||
|
||
@override | ||
Future<Uint8List> readAsBytes() { | ||
return _file.readAsBytes(); | ||
} | ||
|
||
@override | ||
Stream<Uint8List> openRead([int start, int end]) { | ||
return _file | ||
.openRead(start ?? 0, end) | ||
.map((chunk) => Uint8List.fromList(chunk)); | ||
} | ||
} |
Oops, something went wrong.