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
[image_picker_platform_interface] Introduce new PickedFile APIs. #2791
Merged
ditman
merged 7 commits into
flutter:master
from
ditman:image-picker-platform-interface
May 26, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fcb1fe4
Introduce the PickedFile class to have a more platform agnostic return.
ditman f25265f
Modify the platform interface to return PickedFiles.
ditman fa8cf32
Make API Async, so web can use objectUrls internally, instead of bytes.
ditman a36c6ac
[image_picker_platform_interface] Ignore deprecated use analyzer warn…
ditman c31b6bf
[image_picker_platform_interface] Add unit tests
ditman b1df591
[image_picker_platform_interface] dartfmt -w .
ditman 1ccfb51
Document default utf8 encoding in the base PickedFile.
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also mention the default encoding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got it. I'll add the comment that it defaults to UTF8 and merge after this all goes green again.