adding packages
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class FileUploadEvent {
|
||||
final String url;
|
||||
final String path;
|
||||
final String owner;
|
||||
final UploadType type;
|
||||
final bool loading;
|
||||
|
||||
FileUploadEvent({
|
||||
@required this.url,
|
||||
@required this.path,
|
||||
@required this.owner,
|
||||
this.type = UploadType.file,
|
||||
@required this.loading,
|
||||
});
|
||||
}
|
||||
|
||||
enum UploadType {
|
||||
file,
|
||||
image,
|
||||
video,
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export 'unsupported.dart'
|
||||
if (dart.library.html) 'web.dart'
|
||||
if (dart.library.io) 'mobile.dart';
|
||||
|
||||
export 'data/classes/event.dart';
|
||||
@@ -0,0 +1,34 @@
|
||||
import 'data/classes/event.dart';
|
||||
|
||||
abstract class FbStorageImpl {
|
||||
final String imagesPath, filesPath, videosPath;
|
||||
final String owner;
|
||||
|
||||
FbStorageImpl({
|
||||
this.owner,
|
||||
this.imagesPath,
|
||||
this.filesPath,
|
||||
this.videosPath,
|
||||
});
|
||||
|
||||
Future<String> uploadString(
|
||||
String data,
|
||||
String fileName, {
|
||||
String folder = 'files',
|
||||
String contentType = 'text/plain',
|
||||
});
|
||||
|
||||
void captureUploadPhoto();
|
||||
|
||||
void captureUploadVideo();
|
||||
|
||||
void pickUploadPhoto();
|
||||
|
||||
void pickUploadVideo();
|
||||
|
||||
void pickAndUploadFile();
|
||||
|
||||
Stream<FileUploadEvent> get fileUploadedStream;
|
||||
|
||||
void dispose();
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import 'data/classes/event.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FbStorage implements FbStorageImpl {
|
||||
FbStorage({
|
||||
this.owner = 'Guest',
|
||||
this.filesPath = 'files',
|
||||
this.imagesPath = 'images',
|
||||
this.videosPath = 'videos',
|
||||
});
|
||||
|
||||
@override
|
||||
void captureUploadPhoto() async {
|
||||
final _file = await ImagePicker.pickImage(source: ImageSource.camera);
|
||||
if (_file != null) {
|
||||
await _uploadFile(_file, imagesPath, owner, UploadType.image);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void captureUploadVideo() async {
|
||||
final _file = await ImagePicker.pickVideo(source: ImageSource.camera);
|
||||
if (_file != null) {
|
||||
await _uploadFile(_file, videosPath, owner, UploadType.video);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickAndUploadFile() async {
|
||||
final _file = await FilePicker.getFile();
|
||||
if (_file != null) {
|
||||
await _uploadFile(_file, filesPath, owner, UploadType.file);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadPhoto() async {
|
||||
final _file = await ImagePicker.pickImage(source: ImageSource.gallery);
|
||||
if (_file != null) {
|
||||
await _uploadFile(_file, imagesPath, owner, UploadType.image);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadVideo() async {
|
||||
final _file = await ImagePicker.pickVideo(source: ImageSource.gallery);
|
||||
if (_file != null) {
|
||||
await _uploadFile(_file, videosPath, owner, UploadType.video);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadString(
|
||||
String data,
|
||||
String fileName, {
|
||||
String folder = 'files',
|
||||
String contentType = 'text/plain',
|
||||
}) async {
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final file = File('${directory.path}/$fileName');
|
||||
await file.writeAsString(data);
|
||||
final _storage = FirebaseStorage.instance;
|
||||
final StorageReference ref = _storage.ref();
|
||||
var customMetadata = {"owner": owner};
|
||||
var uploadTask = ref.child('$folder').putFile(
|
||||
file,
|
||||
StorageMetadata(
|
||||
contentType: contentType,
|
||||
customMetadata: customMetadata,
|
||||
),
|
||||
);
|
||||
try {
|
||||
var snapshot = await uploadTask.onComplete;
|
||||
final _url = await snapshot.ref.getDownloadURL();
|
||||
return _url.toString();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future _uploadFile(
|
||||
File _file, String path, String owner, UploadType type) async {
|
||||
_controller.add(FileUploadEvent(
|
||||
url: '',
|
||||
owner: owner,
|
||||
path: path,
|
||||
type: type,
|
||||
loading: true,
|
||||
));
|
||||
final _storage = FirebaseStorage.instance;
|
||||
final StorageReference ref = _storage.ref();
|
||||
var customMetadata = {"owner": owner};
|
||||
var uploadTask = ref
|
||||
.child('$path/' + DateTime.now().millisecondsSinceEpoch.toString())
|
||||
.putFile(
|
||||
_file,
|
||||
StorageMetadata(
|
||||
customMetadata: customMetadata,
|
||||
),
|
||||
);
|
||||
try {
|
||||
var snapshot = await uploadTask.onComplete;
|
||||
final _url = await snapshot.ref.getDownloadURL();
|
||||
_controller.add(FileUploadEvent(
|
||||
url: _url ?? '',
|
||||
owner: owner,
|
||||
path: path,
|
||||
type: type,
|
||||
loading: false,
|
||||
));
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
final String owner;
|
||||
|
||||
@override
|
||||
final String filesPath;
|
||||
|
||||
@override
|
||||
final String imagesPath;
|
||||
|
||||
@override
|
||||
final String videosPath;
|
||||
|
||||
@override
|
||||
Stream<FileUploadEvent> get fileUploadedStream => _controller.stream;
|
||||
|
||||
final _controller = StreamController<FileUploadEvent>();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import 'data/classes/event.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FbStorage implements FbStorageImpl {
|
||||
FbStorage({
|
||||
this.owner = 'Guest',
|
||||
this.filesPath = 'files',
|
||||
this.imagesPath = 'images',
|
||||
this.videosPath = 'videos',
|
||||
});
|
||||
|
||||
@override
|
||||
final String owner;
|
||||
|
||||
@override
|
||||
final String filesPath;
|
||||
|
||||
@override
|
||||
final String imagesPath;
|
||||
|
||||
@override
|
||||
final String videosPath;
|
||||
|
||||
@override
|
||||
void captureUploadPhoto() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
void captureUploadVideo() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
void pickAndUploadFile() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadPhoto() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadVideo() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadString(
|
||||
String data,
|
||||
String fileName, {
|
||||
String folder = 'files',
|
||||
String contentType = 'text/plain',
|
||||
}) {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<FileUploadEvent> get fileUploadedStream => null;
|
||||
|
||||
@override
|
||||
void dispose() {}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase/firebase.dart';
|
||||
import 'package:firebase/firebase.dart' as fb;
|
||||
import 'package:universal_html/prefer_universal/html.dart' as html;
|
||||
|
||||
import 'data/classes/event.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FbStorage implements FbStorageImpl {
|
||||
html.FileUploadInputElement _images, _videos, _files;
|
||||
FbStorage({
|
||||
this.owner = 'Guest',
|
||||
this.filesPath = 'files',
|
||||
this.imagesPath = 'images',
|
||||
this.videosPath = 'videos',
|
||||
}) {
|
||||
_images = html.FileUploadInputElement();
|
||||
_images.accept = "image/*";
|
||||
_images.onChange.listen((e) {
|
||||
html.File file = (e.target as dynamic).files[0];
|
||||
if (file != null) {
|
||||
_uploadFile(file, imagesPath, owner, UploadType.image);
|
||||
}
|
||||
});
|
||||
_videos = html.FileUploadInputElement();
|
||||
_videos.accept = "video/*";
|
||||
_videos.onChange.listen((e) {
|
||||
html.File file = (e.target as dynamic).files[0];
|
||||
if (file != null) {
|
||||
_uploadFile(file, videosPath, owner, UploadType.video);
|
||||
}
|
||||
});
|
||||
_files = html.FileUploadInputElement();
|
||||
_files.accept = "*";
|
||||
_files.onChange.listen((e) {
|
||||
html.File file = (e.target as dynamic).files[0];
|
||||
if (file != null) {
|
||||
_uploadFile(file, filesPath, owner, UploadType.file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadString(
|
||||
String data,
|
||||
String fileName, {
|
||||
String folder = 'files',
|
||||
String contentType = 'text/plain',
|
||||
}) async {
|
||||
try {
|
||||
final StorageReference ref = storage().ref('$folder');
|
||||
var customMetadata = {"owner": owner};
|
||||
var uploadTask = ref.child('$fileName').putString(
|
||||
data,
|
||||
StringFormat.RAW,
|
||||
UploadMetadata(
|
||||
contentType: contentType,
|
||||
customMetadata: customMetadata,
|
||||
),
|
||||
);
|
||||
var snapshot = await uploadTask.future;
|
||||
final _url = await snapshot.ref.getDownloadURL();
|
||||
return _url.toString();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void captureUploadPhoto() async {
|
||||
_images.click();
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void captureUploadVideo() async {
|
||||
_videos.click();
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickAndUploadFile() async {
|
||||
_files.click();
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadPhoto() async {
|
||||
_images.click();
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void pickUploadVideo() async {
|
||||
_videos.click();
|
||||
return null;
|
||||
}
|
||||
|
||||
void _uploadFile(
|
||||
html.File file, String path, String owner, UploadType type) async {
|
||||
print('Uploading File...');
|
||||
_controller.add(FileUploadEvent(
|
||||
url: '',
|
||||
owner: owner,
|
||||
path: path,
|
||||
type: type,
|
||||
loading: true,
|
||||
));
|
||||
UploadTask uploadTask;
|
||||
try {
|
||||
var ref = fb
|
||||
.storage()
|
||||
.ref('$path/' + DateTime.now().millisecondsSinceEpoch.toString());
|
||||
uploadTask = ref.put(file);
|
||||
} catch (e) {
|
||||
throw 'Error Creating File';
|
||||
}
|
||||
UploadTaskSnapshot snapshot;
|
||||
try {
|
||||
snapshot = await uploadTask.future;
|
||||
} catch (e) {
|
||||
throw 'Error Uploading File';
|
||||
}
|
||||
try {
|
||||
final _url = await snapshot.ref.getDownloadURL();
|
||||
_controller.add(FileUploadEvent(
|
||||
url: _url ?? '',
|
||||
owner: owner,
|
||||
path: path,
|
||||
type: type,
|
||||
loading: false,
|
||||
));
|
||||
} catch (e) {
|
||||
throw 'Error Getting Url';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
final String owner;
|
||||
|
||||
@override
|
||||
final String filesPath;
|
||||
|
||||
@override
|
||||
final String imagesPath;
|
||||
|
||||
@override
|
||||
final String videosPath;
|
||||
|
||||
@override
|
||||
Stream<FileUploadEvent> get fileUploadedStream => _controller.stream;
|
||||
|
||||
final _controller = StreamController<FileUploadEvent>();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user