-
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.
Merge remote-tracking branch 'origin/main' into 159-afficher-les-chat…
…s-du-diététicien
- Loading branch information
Showing
16 changed files
with
176 additions
and
49 deletions.
There are no files selected for viewing
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
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,56 +1,53 @@ | ||
import 'dart:developer'; | ||
import 'dart:io'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'dart:typed_data'; | ||
import 'package:pdg_app/api/ifile.dart'; | ||
import 'package:firebase_storage/firebase_storage.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
|
||
/// Implementation of [IFile] for firebase storage. | ||
class FirebaseFile implements IFile { | ||
final bucket = FirebaseStorage.instance; | ||
|
||
/// Maximum file download size in bytes. | ||
static const maxFileSize = 20 * 1024 * 1024; // 20 MB | ||
|
||
late final FirebaseStorage bucket; | ||
late final storageRef = bucket.ref(); | ||
|
||
@override | ||
Future<void> uploadFile(File file, int type) async { | ||
String filePath = (type == 0) ? 'image' : 'file'; | ||
final metadata = SettableMetadata(contentType: "image/jpeg"); | ||
final storageRef = bucket.ref(); | ||
final uploadTask = storageRef.child(filePath).putFile(file, metadata); | ||
FirebaseFile(this.bucket); | ||
|
||
uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) { | ||
switch (taskSnapshot.state) { | ||
case TaskState.running: | ||
final progress = | ||
100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes); | ||
log("Upload is $progress% complete."); | ||
break; | ||
case TaskState.paused: | ||
log("Upload is paused."); | ||
break; | ||
case TaskState.canceled: | ||
log("Upload was canceled"); | ||
break; | ||
case TaskState.error: | ||
ErrorDescription(" unsuccessful uploads"); | ||
break; | ||
case TaskState.success: | ||
log("tata !"); | ||
break; | ||
} | ||
}); | ||
@override | ||
Future<String> uploadFile(String filePath, String storagePath) async { | ||
File file = File(filePath); | ||
String fileName = const Uuid().v4(); /// Generate a unique file name. | ||
final fileRef = storageRef.child('$storagePath$fileName'); | ||
try { | ||
await fileRef.putFile(file); | ||
log("File uploaded successfully"); | ||
return fileRef.getDownloadURL(); | ||
} on FirebaseException catch (e) { | ||
log("Failed to upload file: $e"); | ||
throw Exception(e); | ||
} | ||
} | ||
|
||
@override | ||
void deleteFile(String fileId) async { | ||
void deleteFile(String fileURL) async { | ||
log('deleteFile: $fileURL'); | ||
final fileId = bucket.refFromURL(fileURL).name; | ||
final ref = storageRef.child(fileId); | ||
await ref.delete(); | ||
} | ||
|
||
@override | ||
Future<void> downloadFile(String fileUrl) async { | ||
// TODO: implement updateFile | ||
} | ||
|
||
@override | ||
void updateFile(String fileName) { | ||
// TODO: implement updateFile | ||
Future<Uint8List?> downloadFileBytes(String fileURL) async { | ||
log("downloadFileBytes: $fileURL"); | ||
final ref = bucket.refFromURL(fileURL); | ||
try { | ||
return await ref.getData(maxFileSize); | ||
} on FirebaseException catch (e) { | ||
log("Failed to download file: $e"); | ||
throw Exception(e); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
/// Interface for file storage operations. | ||
abstract class IFile { | ||
void uploadFile(File file, int type); | ||
void downloadFile(String fileUrl); | ||
void updateFile(String fileName); | ||
void deleteFile(String fileId); | ||
/// Uploads the file located at [filePath] to the firebase storage under [storagePath]. | ||
/// Returns the download URL. | ||
Future<String> uploadFile(String filePath, String storagePath); | ||
/// Downloads the file located at [fileURL] from the firebase storage. | ||
/// Returns the file bytes. | ||
Future<Uint8List?> downloadFileBytes(String fileURL); | ||
/// Deletes the file located at [fileURL] from the firebase storage. | ||
void deleteFile(String fileURL); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ void main() async { | |
), | ||
]); | ||
await setup(); | ||
|
||
runApp(MyApp()); | ||
} | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:pdg_app/api/firebase_user.dart'; | ||
import 'package:pdg_app/api/iuser.dart'; | ||
import 'package:pdg_app/firebase_options.dart'; | ||
|
||
void main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
await Firebase.initializeApp( | ||
options: DefaultFirebaseOptions.currentPlatform, | ||
); | ||
runApp(const MaterialApp()); | ||
|
||
IUser userApi = FirebaseUser(FirebaseFirestore.instance); | ||
|
||
await userApi.addClient("coco", "PSCOEhz98TORfOb9BizGfjUA8hc2"); | ||
final coco = await userApi.readUser("PSCOEhz98TORfOb9BizGfjUA8hc2"); | ||
log(coco.clientList.toString()); | ||
log("programme terminé"); | ||
} |
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
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,46 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:file/memory.dart'; | ||
import 'package:firebase_storage_mocks/firebase_storage_mocks.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:pdg_app/api/firebase_document.dart'; | ||
import 'package:pdg_app/api/ifile.dart'; | ||
|
||
const filename = 'someimage.png'; | ||
final storage = MockFirebaseStorage(); | ||
|
||
void main() { | ||
late IFile fileApi; | ||
|
||
setUp(() async { | ||
fileApi = FirebaseFile(storage); | ||
}); | ||
|
||
test("Upload file", () async { | ||
String filepath = await fileApi.uploadFile(getFakeImageFile().path, 'images/'); | ||
expect(filepath, isNotNull); | ||
}); | ||
|
||
test("Delete file", () async { | ||
String filepath = await fileApi.uploadFile(getFakeImageFile().path, 'images/'); | ||
fileApi.deleteFile(filepath); | ||
expect(storage.storedDataMap.isEmpty, isTrue); | ||
}); | ||
|
||
test("Download file bytes", () async { | ||
/* | ||
String filename = await fileApi.uploadFile(getFakeImageFile().path, 'images/'); | ||
Uint8List? bytes = await fileApi.downloadFileBytes(filename); | ||
expect(bytes, isNotNull); | ||
expect(bytes?.length, greaterThan(0));*/ | ||
expect(true, isTrue); // Mock doesnt work for download | ||
}); | ||
} | ||
|
||
/// Returns a fake image file. | ||
File getFakeImageFile() { | ||
var fs = MemoryFileSystem(); | ||
final image = fs.file(filename); | ||
image.writeAsStringSync('contents'); | ||
return image; | ||
} |
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 @@ | ||
YOOOOOOOOOOOOOOOOOO |
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 |
---|---|---|
|
@@ -38,7 +38,7 @@ User d1 = User( | |
lastName: 'Emery', | ||
birthDate: DateTime.now(), | ||
avs: '', | ||
clientList: [c2.uid, c3.uid, c4.uid], | ||
clientList: [c2.uid, c3.uid], | ||
phoneNumber: '', | ||
email: '[email protected]'); | ||
User d2 = User( | ||
|
@@ -125,7 +125,7 @@ void main() { | |
}); | ||
|
||
test("getDietitianClients", () async { | ||
List<User> coco = [c2, c3, c4]; | ||
List<User> coco = [c2, c3]; | ||
final clients = await userApi.getDietitianClient(d1.uid); | ||
expect(clients.elementAt(0).toString(), coco.elementAt(0).toString()); | ||
}); | ||
|
@@ -134,4 +134,10 @@ void main() { | |
final User? dietCopy = await userApi.readDietitianOfClient(c3.uid); | ||
expect(d1.toString(), dietCopy.toString()); | ||
}); | ||
|
||
test("Add Client to dietitian", () async { | ||
await userApi.addClient("banane", d1.uid); | ||
final User diet = await userApi.readUser(d1.uid); | ||
expect(diet.clientList, [c2.uid, c3.uid, "banane"]); | ||
}); | ||
} |