forked from firebase/flutterfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
247221b
commit 34e07c7
Showing
17 changed files
with
726 additions
and
416 deletions.
There are no files selected for viewing
13 changes: 9 additions & 4 deletions
13
packages/firebase_storage/firebase_storage/lib/firebase_storage.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 |
---|---|---|
@@ -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'; |
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
165 changes: 165 additions & 0 deletions
165
packages/firebase_storage/firebase_storage/lib/src/storage_reference.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,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; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/firebase_storage/firebase_storage/lib/src/upload_task.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,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
16
packages/firebase_storage/firebase_storage/lib/src/utils.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,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', | ||
); |
Oops, something went wrong.