Skip to content

Commit

Permalink
pulling master
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMusleh committed Jan 10, 2020
1 parent 247221b commit 34e07c7
Show file tree
Hide file tree
Showing 17 changed files with 726 additions and 416 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
library firebase_storage;

import 'dart:async';
import 'dart:typed_data';

import 'package:firebase_storage_platform_interface/firebase_storage_platform_interface.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';
import 'dart:io';

export 'package:firebase_storage_platform_interface/firebase_storage_platform_interface.dart'
show
FileDownloadTaskSnapshot,
StorageFileDownloadTask,
StorageReference,
StorageTaskSnapshot,
StorageError,
StorageTaskEvent,
StorageTaskEventType,
StorageMetadata,
StorageUploadTask;
StorageMetadata;

part 'src/firebase_storage.dart';
part 'src/storage_reference.dart';
part 'src/upload_task.dart';
part 'src/download_task.dart';
part 'src/utils.dart';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of firebase_storage_platform_interface;
part of firebase_storage;

class StorageFileDownloadTask {
StorageFileDownloadTask._(this._firebaseStorage, this._path, this._file);
Expand All @@ -7,10 +7,14 @@ class StorageFileDownloadTask {
final String _path;
final File _file;

static const MethodChannel channel = MethodChannel(
'plugins.flutter.io/firebase_storage',
);

Future<void> _start() async {
try {
final int totalByteCount =
await MethodChannelFirebaseStorage.channel.invokeMethod<int>(
await channel.invokeMethod<int>(
"StorageReference#writeToFile",
<String, dynamic>{
'app': _firebaseStorage.app?.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class FirebaseStorage {
/// Creates a new [StorageReference] initialized at the root
/// Firebase Storage location.
StorageReference ref() {
return FirebaseStoragePlatform.instance.ref();
return StorageReference._fromPlatform(
FirebaseStoragePlatform.instance?.ref());
}

// @visibleForTesting
Expand Down Expand Up @@ -71,8 +72,6 @@ class FirebaseStorage {
await FirebaseStoragePlatform.instance.setMaxOperationRetryTimeMillis(time);
}

/// Creates a [StorageReference] given a gs:// or // URL pointing to a Firebase
/// Storage location.
Future<StorageReference> getReferenceFromUrl(String fullUrl) async {
return await FirebaseStoragePlatform.instance.getReferenceFromUrl(fullUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of firebase_storage;

class StorageReference extends PlatformStorageRef {
StorageReference(
List<String> pathComponents,
FirebaseStoragePlatform firebaseStorage,
) : super(pathComponents, firebaseStorage);

factory StorageReference._fromPlatform(
PlatformStorageRef platformStorageRef) {
return platformStorageRef == null
? null
: StorageReference(platformStorageRef.pathComponents,
platformStorageRef.firebaseStorage);
}

/// Returns the Google Cloud Storage bucket that holds this object.
@override
Future<String> getBucket() async {
return await _channel
.invokeMethod<String>("StorageReference#getBucket", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
});
}

/// Returns the short name of this object.
@override
Future<String> getName() async {
return await _channel
.invokeMethod<String>("StorageReference#getName", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
});
}

/// Returns the full path to this object, not including the Google Cloud
/// Storage bucket.
@override
Future<String> getPath() async {
final String _path = await _channel
.invokeMethod<String>("StorageReference#getPath", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
});

if (_path.startsWith('/')) {
return _path.substring(1);
} else {
return _path;
}
}

/// Asynchronously retrieves a long lived download URL with a revokable token.
/// This can be used to share the file with others, but can be revoked by a
/// developer in the Firebase Console if desired.
@override
Future<dynamic> getDownloadURL() async {
return await _channel.invokeMethod<dynamic>(
"StorageReference#getDownloadUrl", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
});
}

@override
Future<void> delete() {
return _channel
.invokeMethod<void>("StorageReference#delete", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path
});
}

/// Retrieves metadata associated with an object at this [StorageReference].
@override
Future<StorageMetadata> getMetadata() async {
return StorageMetadata.fromMap(await _channel
.invokeMapMethod<String, dynamic>(
"StorageReference#getMetadata", <String, String>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
}));
}

/// Updates the metadata associated with this [StorageReference].
///
/// Returns a [Future] that will complete to the updated [StorageMetadata].
///
/// This method ignores fields of [metadata] that cannot be set by the public
/// [StorageMetadata] constructor. Writable metadata properties can be deleted
/// by passing the empty string.
@override
Future<StorageMetadata> updateMetadata(StorageMetadata metadata) async {
return StorageMetadata.fromMap(await _channel
.invokeMapMethod<String, dynamic>(
"StorageReference#updateMetadata", <String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'path': path,
'metadata': metadata == null ? null : _buildMetadataUploadMap(metadata),
}));
}

/// Asynchronously downloads the object at the StorageReference to a list in memory.
/// A list of the provided max size will be allocated.
Future<Uint8List> getData(int maxSize) async {
return await _channel.invokeMethod<Uint8List>(
"StorageReference#getData",
<String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'maxSize': maxSize,
'path': path,
},
);
}

/// Asynchronously downloads the object at this [StorageReference] to a
/// specified system file.
StorageFileDownloadTask writeToFile(File file) {
final StorageFileDownloadTask task =
StorageFileDownloadTask._(firebaseStorage, path, file);
task._start();
return task;
}

/// This method is deprecated. Please use [putFile] instead.
///
/// Asynchronously uploads a file to the currently specified
/// [StorageReference], with an optional [metadata].
@deprecated
StorageUploadTask put(File file, [StorageMetadata metadata]) {
return putFile(file, metadata);
}

/// Asynchronously uploads a file to the currently specified
/// [StorageReference], with an optional [metadata].
StorageUploadTask putFile(File file, [StorageMetadata metadata]) {
assert(file.existsSync());
final StorageUploadTask task = StorageUploadTask._(
file, firebaseStorage, this, metadata, UploadDataType.FILE);
task.start();
return task;
}

/// Asynchronously uploads byte data to the currently specified
/// [StorageReference], with an optional [metadata].
StorageUploadTask putData(Uint8List data, [StorageMetadata metadata]) {
final StorageUploadTask task = StorageUploadTask._(
data, firebaseStorage, this, metadata, UploadDataType.DATA);
task.start();
return task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of firebase_storage;

class StorageUploadTask extends PlatformUploadTask {
StorageUploadTask._(
this._data,
MethodChannelFirebaseStorage firebaseStorage,
PlatformStorageRef ref,
StorageMetadata metadata,
UploadDataType dataType,
) : super(_data, firebaseStorage, ref, metadata, dataType);

final dynamic _data;

/// Pause the upload
void pause() => _channel.invokeMethod<void>(
'UploadTask#pause',
<String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'handle': handle,
},
);

/// Resume the upload
void resume() => _channel.invokeMethod<void>(
'UploadTask#resume',
<String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'handle': handle,
},
);

/// Cancel the upload
void cancel() => _channel.invokeMethod<void>(
'UploadTask#cancel',
<String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
'handle': handle,
},
);

@override
Future<dynamic> platformStart() {
String key;
dynamic value;
String method;
switch (dataType) {
case UploadDataType.FILE:
key = 'filename';
value = _data.absolute.path; //File.absolute.path
method = 'StorageReference#putFile';
break;
case UploadDataType.DATA:
key = 'data';
method = 'StorageReference#putData';
value = _data; //Uint8List
break;
default:
break;
}
return _channel.invokeMethod<dynamic>(
method,
<String, dynamic>{
'app': firebaseStorage.app?.name,
'bucket': firebaseStorage.storageBucket,
key: value,
'path': ref.path,
'metadata': metadata == null ? null : _buildMetadataUploadMap(metadata),
},
);
}
}
16 changes: 16 additions & 0 deletions packages/firebase_storage/firebase_storage/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
part of firebase_storage;

Map<String, dynamic> _buildMetadataUploadMap(StorageMetadata metadata) {
return <String, dynamic>{
'cacheControl': metadata.cacheControl,
'contentDisposition': metadata.contentDisposition,
'contentLanguage': metadata.contentLanguage,
'contentType': metadata.contentType,
'contentEncoding': metadata.contentEncoding,
'customMetadata': metadata.customMetadata,
};
}

const MethodChannel _channel = MethodChannel(
'plugins.flutter.io/firebase_storage',
);
Loading

0 comments on commit 34e07c7

Please sign in to comment.