Implemented the [uploadFile] method

This commit is contained in:
Mariano Uvalle 2019-02-27 17:05:38 -06:00
parent 0a6d7842ab
commit 8313793184

View file

@ -5,18 +5,26 @@ import 'package:firebase_storage/firebase_storage.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
class FirebaseStorageProvider { class FirebaseStorageProvider {
final FirebaseStorage _storage; final StorageReference _storage;
final Uuid _uuid; final Uuid _uuid;
// [FirebaseStorage] and [Uuid] instances can be injected for testing purposes. // [FirebaseStorage] and [Uuid] instances can be injected for testing purposes.
// Don't remove. // Don't remove.
FirebaseStorageProvider([FirebaseStorage storage, Uuid uuid]) FirebaseStorageProvider([StorageReference storage, Uuid uuid])
: _storage = storage ?? FirebaseStorage.instance, : _storage = storage ?? FirebaseStorage.instance.ref(),
_uuid = uuid ?? Uuid(); _uuid = uuid ?? Uuid();
/// Uploads a given file to the firebase storage bucket. /// Uploads a given file to the firebase storage bucket.
/// ///
/// It returns a [StorageUploadTask] which contains the status of the upload. /// It returns a [StorageUploadTask] which contains the status of the upload.
/// The [folder] parameters allows the file to be stored at any path in the /// The [folder] parameter should not start with "/" , it allows the file to
/// bucket. /// be stored at any path in the bucket.
StorageUploadTask uploadFile(File file, {String folder}) {} /// The [type] parameter allows you to specify the extension of the file bein
/// uploaded, it defaults to png.
StorageUploadTask uploadFile(File file,
{String folder, String type = 'png'}) {
final String fileId = _uuid.v1();
final StorageReference fileReference =
_storage.child('folder/$fileId.$type');
return fileReference.putFile(file);
}
} }