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

Adding optionnal headers to saveImage and saveVideo #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions lib/gallery_saver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GallerySaver {
static const MethodChannel _channel = const MethodChannel(channelName);

///saves video from provided temp path and optional album name in gallery
static Future<bool> saveVideo(String path, {String albumName}) async {
static Future<bool> saveVideo(String path, {String albumName, Map<String, String> headers}) async {
File tempFile;
if (path == null || path.isEmpty) {
throw ArgumentError(pleaseProvidePath);
Expand All @@ -27,7 +27,7 @@ class GallerySaver {
throw ArgumentError(fileIsNotVideo);
}
if (!isLocalFilePath(path)) {
tempFile = await _downloadFile(path);
tempFile = await _downloadFile(path, headers: headers);
path = tempFile.path;
}
bool result = await _channel.invokeMethod(
Expand All @@ -41,7 +41,7 @@ class GallerySaver {
}

///saves image from provided temp path and optional album name in gallery
static Future<bool> saveImage(String path, {String albumName}) async {
static Future<bool> saveImage(String path, {String albumName, Map<String, String> headers}) async {
File tempFile;
if (path == null || path.isEmpty) {
throw ArgumentError(pleaseProvidePath);
Expand All @@ -50,7 +50,7 @@ class GallerySaver {
throw ArgumentError(fileIsNotImage);
}
if (!isLocalFilePath(path)) {
tempFile = await _downloadFile(path);
tempFile = await _downloadFile(path, headers: headers);
path = tempFile.path;
}

Expand All @@ -65,10 +65,10 @@ class GallerySaver {
return result;
}

static Future<File> _downloadFile(String url) async {
static Future<File> _downloadFile(String url, {Map<String, String> headers}) async {
print(url);
http.Client _client = new http.Client();
var req = await _client.get(Uri.parse(url));
var req = headers != null ? await _client.get(Uri.parse(url), headers: headers) : await _client.get(Uri.parse(url));

Choose a reason for hiding this comment

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

Does this break without the explicit null check? Seems from the implementation that it's not necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah i overreacted and it doesn't break out without the null check i should correct that thax for highlighting it !

var bytes = req.bodyBytes;
String dir = (await getTemporaryDirectory()).path;
File file = new File('$dir/${basename(url)}');
Expand Down