From 0ffe261e868d0501f106bdc3588a491f55a3766e Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Sun, 17 Dec 2023 16:14:39 +0100 Subject: [PATCH] Add audio, video, image, pdf and json files (online and local) --- lib/Components/audio_player.dart | 12 +- lib/Components/fetch_resource_icon.dart | 9 +- lib/Components/rounded_input_field.dart | 2 +- lib/Components/string_input_container.dart | 2 +- .../upload_online_resources_container.dart | 19 ++- .../Resources/get_element_for_resource.dart | 19 +-- lib/Screens/Resources/new_resource_popup.dart | 37 ++--- lib/Screens/Resources/resource_body_grid.dart | 6 +- lib/Screens/Resources/resources_screen.dart | 127 +++++++++++------- .../Resources/show_resource_popup.dart | 15 +++ web/index.html | 3 + 11 files changed, 153 insertions(+), 98 deletions(-) diff --git a/lib/Components/audio_player.dart b/lib/Components/audio_player.dart index 8411d4d..3eeb644 100644 --- a/lib/Components/audio_player.dart +++ b/lib/Components/audio_player.dart @@ -10,9 +10,10 @@ import 'package:just_audio/just_audio.dart'; class AudioPlayerFloatingContainer extends StatefulWidget { - const AudioPlayerFloatingContainer({Key? key, required this.audioBytes, required this.isAuto}) : super(key: key); + const AudioPlayerFloatingContainer({Key? key, required this.audioBytes, required this.resourceURl, required this.isAuto}) : super(key: key); - final Uint8List audioBytes; + final Uint8List? audioBytes; + final String resourceURl; final bool isAuto; @override @@ -32,8 +33,10 @@ class _AudioPlayerFloatingContainerState extends State snapshot) { Size size = MediaQuery.of(context).size; if (snapshot.connectionState == ConnectionState.done) { @@ -67,7 +68,7 @@ getElementForResource(dynamic resourceDTO, AppContext appContext) { audioBytes = snapshot.data; //this.player.playBytes(audiobytes); } - return AudioPlayerFloatingContainer(audioBytes: audioBytes, isAuto: true); + return AudioPlayerFloatingContainer(audioBytes: audioBytes, resourceURl: resourceDTO.url, isAuto: true); } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { @@ -80,19 +81,19 @@ getElementForResource(dynamic resourceDTO, AppContext appContext) { ); } } - ); + );*/ //return Text("Fichier audio - aucune visualisation possible"); break; case ResourceType.Video: return Text("Vidéo locale - aucune visualisation possible"); break; case ResourceType.VideoUrl: - return Text(resourceDTO.data); + return Text(resourceDTO.url); break; } } -Future getAudio(String resourceId, AppContext appContext) async { +/*Future getAudio(String resourceId, AppContext appContext) async { try { ManagerAppContext managerAppContext = appContext.getContext() as ManagerAppContext; var url = managerAppContext.host! + "/api/Resource/" + resourceId; // TO TEST TODO UPDATE ROUTE @@ -105,4 +106,4 @@ Future getAudio(String resourceId, AppContext appContext) async { print("IN CATCH"); return null; } -} +}*/ diff --git a/lib/Screens/Resources/new_resource_popup.dart b/lib/Screens/Resources/new_resource_popup.dart index 1264c79..0f44257 100644 --- a/lib/Screens/Resources/new_resource_popup.dart +++ b/lib/Screens/Resources/new_resource_popup.dart @@ -29,7 +29,7 @@ dynamic showNewResource(AppContext appContext, BuildContext context) async { child: Column( children: [ Text("Nouvelle ressource", style: new TextStyle(fontSize: 25, fontWeight: FontWeight.w400)), - Column( + /*Column( children: [ StringInputContainer( label: "Nom :", @@ -40,19 +40,22 @@ dynamic showNewResource(AppContext appContext, BuildContext context) async { ), if (fileName != null) new Text(fileName), ], - ), - Container( - width: size.width *0.85, - height: size.height *0.5, - child: ResourceTab( - resourceDTO: resourceDetailDTO, - onFileUpload: (List files) { - filesToSend = files; - }, - onFileUploadWeb: (List files) { - filesToSendWeb = files; - }, - ) + ),*/ + Padding( + padding: const EdgeInsets.only(top: 10.0), + child: Container( + width: size.width *0.85, + height: size.height *0.5, + child: ResourceTab( + resourceDTO: resourceDetailDTO, + onFileUpload: (List files) { + filesToSend = files; + }, + onFileUploadWeb: (List files) { + filesToSendWeb = files; + }, + ) + ), ), ], ), @@ -89,7 +92,7 @@ dynamic showNewResource(AppContext appContext, BuildContext context) async { color: kPrimaryColor, textColor: kWhite, press: () { - if (resourceDetailDTO.label != null && resourceDetailDTO.label!.trim() != '') { + //if (resourceDetailDTO.label != null && resourceDetailDTO.label!.trim() != '') { if(kIsWeb) { if(resourceDetailDTO.url != null || filesToSendWeb != null) { // TODO clarify resourceDetailDTO.data != null Navigator.pop(context, [resourceDetailDTO, filesToSend, filesToSendWeb]); @@ -103,9 +106,9 @@ dynamic showNewResource(AppContext appContext, BuildContext context) async { showNotification(Colors.orange, kWhite, 'Aucun fichier n\'a été chargé', context, null); } } - } else { + /*} else { showNotification(Colors.orange, kWhite, 'Veuillez donner un nom à la ressource', context, null); - } + }*/ //Navigator.of(context).pop(); //create(resourceDetailDTO, fileToSend, appContext, context); }, diff --git a/lib/Screens/Resources/resource_body_grid.dart b/lib/Screens/Resources/resource_body_grid.dart index 96a53e8..5f3a002 100644 --- a/lib/Screens/Resources/resource_body_grid.dart +++ b/lib/Screens/Resources/resource_body_grid.dart @@ -190,14 +190,12 @@ class _ResourceBodyGridState extends State { } } -boxDecoration(ResourceDTO resourceDetailDTO, appContext) { - print("boxDecorationboxDecorationboxDecorationboxDecoration"); - print(resourceDetailDTO); +boxDecoration(dynamic resourceDetailDTO, appContext) { return BoxDecoration( color: resourceDetailDTO.id == null ? kSecond : kBackgroundColor, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(30.0), - image: resourceDetailDTO.id != null && (resourceDetailDTO.type == ResourceType.Image || resourceDetailDTO.url != null && resourceDetailDTO.type == ResourceType.ImageUrl)? new DecorationImage( + image: resourceDetailDTO.id != null && (resourceDetailDTO.type == ResourceType.Image || resourceDetailDTO.type == ResourceType.ImageUrl) && resourceDetailDTO.url != null ? new DecorationImage( fit: BoxFit.cover, colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.3), BlendMode.dstATop), image: new NetworkImage( diff --git a/lib/Screens/Resources/resources_screen.dart b/lib/Screens/Resources/resources_screen.dart index 358147a..10ae748 100644 --- a/lib/Screens/Resources/resources_screen.dart +++ b/lib/Screens/Resources/resources_screen.dart @@ -128,62 +128,85 @@ Future?> create(ResourceDTO resourceDTO, List? files, L ManagerAppContext managerAppContext = appContext.getContext(); int index = 0; - // TODO handle online resource - for (PlatformFile platformFile in filesWeb!) { - print("Coucou platformFile"); - print(platformFile); - print(platformFile.name); - print(platformFile.extension); - ResourceDTO resourceDTO = new ResourceDTO(label: platformFile.name); - switch(platformFile.extension) { - case 'jpg': - case 'jpeg': - case 'png': - resourceDTO.type = ResourceType.Image; - break; - case 'mp3': - resourceDTO.type = ResourceType.Audio; - break; - case 'mp4': - resourceDTO.type = ResourceType.Video; - break; - case 'pdf': - resourceDTO.type = ResourceType.Pdf; - break; - case 'json': - resourceDTO.type = ResourceType.Json; - break; - } - resourceDTO.instanceId = managerAppContext.instanceId; - try { - print("Trying to create resource"); - resourceDTO.dateCreation = DateTime.now(); - ResourceDTO? newResource = await managerAppContext.clientAPI!.resourceApi!.resourceCreate(resourceDTO); - print("created resource"); - print(newResource); - if(newResource != null && resourceDTO.type != null) { - FirebaseStorage storage = FirebaseStorage.instance; - Reference ref = storage.ref().child('pictures/${appContext.getContext().instanceId}/${Path.basename(newResource.id!.toString())}.${platformFile.extension}'); - UploadTask uploadTask = ref.putData(platformFile.bytes!); - uploadTask.then((res) { - res.ref.getDownloadURL().then((urlImage) { - showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context, null); - newResource.url = urlImage; - (appContext.getContext() as ManagerAppContext).clientAPI!.resourceApi!.resourceUpdate(newResource); - createdResources.add(newResource); - index++; - if(index == filesWeb.length) { - return createdResources; - } - }); - }); + if(filesWeb != null) { + for (PlatformFile platformFile in filesWeb) { + var mimeType = ""; + print("Coucou platformFile"); + print(platformFile); + print(platformFile.name); + print(platformFile.extension); + ResourceDTO resourceDTO = new ResourceDTO(label: platformFile.name); + switch(platformFile.extension) { + case 'jpg': + case 'jpeg': + case 'png': + resourceDTO.type = ResourceType.Image; + mimeType = 'image/'+platformFile.extension!; + break; + case 'mp3': + resourceDTO.type = ResourceType.Audio; + mimeType = 'audio/'+platformFile.extension!; + break; + case 'mp4': + resourceDTO.type = ResourceType.Video; + mimeType = 'video/'+platformFile.extension!; + break; + case 'pdf': + resourceDTO.type = ResourceType.Pdf; + mimeType = 'application/'+platformFile.extension!; + break; + case 'json': + resourceDTO.type = ResourceType.Json; + mimeType = 'application/'+platformFile.extension!; + break; + } + resourceDTO.instanceId = managerAppContext.instanceId; + try { + print("Trying to create resource"); + resourceDTO.dateCreation = DateTime.now(); + ResourceDTO? newResource = await managerAppContext.clientAPI!.resourceApi!.resourceCreate(resourceDTO); + print("created resource"); + print(newResource); + if(newResource != null && resourceDTO.type != null) { + FirebaseStorage storage = FirebaseStorage.instance; + Reference ref = storage.ref().child('pictures/${appContext.getContext().instanceId}/${Path.basename(newResource.id!.toString())}'); + + var metadata = SettableMetadata( + contentType: mimeType, // ou le type MIME approprié pour votre fichier + ); + + UploadTask uploadTask = ref.putData(platformFile.bytes!, metadata); + uploadTask.then((res) { + res.ref.getDownloadURL().then((urlImage) { + showNotification(Colors.green, kWhite, 'La ressource a été créée avec succès', context, null); + newResource.url = urlImage; + (appContext.getContext() as ManagerAppContext).clientAPI!.resourceApi!.resourceUpdate(newResource); + createdResources.add(newResource); + index++; + if(index == filesWeb.length) { + return createdResources; + } + }); + }); + } + } catch(e) { + print("ERROR creating resource"); + print(e); } - } catch(e) { - print("ERROR creating resource"); - print(e); } + } else { + // online resource + print("Trying to create resource"); + resourceDTO.dateCreation = DateTime.now(); + resourceDTO.label = "Resource en ligne"; + resourceDTO.instanceId = managerAppContext.instanceId; + ResourceDTO? newResource = await managerAppContext.clientAPI!.resourceApi!.resourceCreate(resourceDTO); + print("created resource"); + createdResources.add(newResource); + return createdResources; } + return null; /*switch(resourceDTO.type) { diff --git a/lib/Screens/Resources/show_resource_popup.dart b/lib/Screens/Resources/show_resource_popup.dart index 29d3548..d64f44e 100644 --- a/lib/Screens/Resources/show_resource_popup.dart +++ b/lib/Screens/Resources/show_resource_popup.dart @@ -1,3 +1,4 @@ +import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; import 'package:manager_app/Components/confirmation_dialog.dart'; import 'package:manager_app/Components/message_notification.dart'; @@ -135,6 +136,20 @@ Future delete(ResourceDTO resourceDTO, AppContext appContext, context) asy () {}, () async { await (appContext.getContext() as ManagerAppContext).clientAPI!.resourceApi!.resourceDelete(resourceDTO.id!); + // TODO Remove from firebase storage via service or ? + + FirebaseStorage storage = FirebaseStorage.instance; + Reference storageReference = storage.ref().child('pictures/${appContext.getContext().instanceId}/${resourceDTO.id}'); + // Supprimer le fichier + try { + await storageReference.delete(); + print('Fichier supprimé avec succès'); + } catch (e) { + print('Erreur lors de la suppression du fichier : $e'); + } + + try {} catch (e) {} + // just to refresh ManagerAppContext managerAppContext = appContext.getContext(); appContext.setContext(managerAppContext); diff --git a/web/index.html b/web/index.html index 9774a6d..783dbeb 100644 --- a/web/index.html +++ b/web/index.html @@ -111,6 +111,9 @@ loadMainDartJs(); } +