From aedd5f33fa6ff0323eb2839c714b554f0bf652e2 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Fri, 16 Aug 2024 16:59:31 +0200 Subject: [PATCH] Added methods for mdlf migration in MyInfomate + firebase service (linked) --- .../Controllers/ResourceController.cs | 64 ++- .../Controllers/SectionController.cs | 432 +++++++++++++++++- ManagerService/ManagerService.csproj | 2 + ManagerService/Services/FirebaseService.cs | 18 + .../Services/FirebaseStorageService.cs | 64 +++ ManagerService/Startup.cs | 4 + 6 files changed, 582 insertions(+), 2 deletions(-) create mode 100644 ManagerService/Services/FirebaseService.cs create mode 100644 ManagerService/Services/FirebaseStorageService.cs diff --git a/ManagerService/Controllers/ResourceController.cs b/ManagerService/Controllers/ResourceController.cs index d0f386f..ce31528 100644 --- a/ManagerService/Controllers/ResourceController.cs +++ b/ManagerService/Controllers/ResourceController.cs @@ -13,11 +13,13 @@ using Manager.Interfaces.Models; using Manager.Services; using ManagerService.Helpers; using ManagerService.Service.Services; +using ManagerService.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Server.IIS.Core; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NSwag.Annotations; using static System.Net.Mime.MediaTypeNames; @@ -31,17 +33,21 @@ namespace ManagerService.Controllers private ResourceDatabaseService _resourceService; private SectionDatabaseService _sectionService; private ConfigurationDatabaseService _configurationService; + private FirebaseService _firebaseService; + private FirebaseStorageService _firebaseStorageService; private readonly ILogger _logger; private static int MaxWidth = 1024; private static int MaxHeight = 1024; - public ResourceController(ILogger logger, ResourceDatabaseService resourceService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService) + public ResourceController(ILogger logger, ResourceDatabaseService resourceService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService, FirebaseService firebaseService, FirebaseStorageService firebaseStorageService) { _logger = logger; _resourceService = resourceService; _sectionService = sectionService; _configurationService = configurationService; + _firebaseService = firebaseService; + _firebaseStorageService = firebaseStorageService; } /// @@ -560,5 +566,61 @@ namespace ManagerService.Controllers return new ObjectResult(ex.Message) { StatusCode = 500 }; } } + + /// + /// Create new ressources from json + /// + /// New section info + [ProducesResponseType(typeof(SectionDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 409)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost("json")] + public async Task CreateFromJSON() + { + try + { + + string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/Aout 2024/TabletDb.Resources.json"); + JArray resourceAll = JArray.Parse(json); + + foreach (var resource in resourceAll) + { + Resource newResource = new Resource(); + newResource.Id = (string)resource["_id"]["$oid"]; + + var test = _resourceService.GetById(newResource.Id); + if (test == null) + { + newResource.Type = ResourceType.Image; + newResource.DateCreation = (DateTime)resource["DateCreation"]["$date"]; + newResource.Label = (string)resource["Label"]; + newResource.InstanceId = "65ccc67265373befd15be511"; + + var dataTest = (string)resource["Data"]; + var downloadUrl = await _firebaseStorageService.UploadBase64Async(dataTest, newResource.Id, newResource.InstanceId); + newResource.Url = downloadUrl; + + // TO Uncomment if needed + //Resource createdResource = _resourceService.Create(newResource); + } + } + + return new OkObjectResult(true); + } + catch (ArgumentNullException ex) + { + return new BadRequestObjectResult(ex.Message) { }; + } + catch (InvalidOperationException ex) + { + return new ConflictObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + } } diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs index 4ab7baa..37ef8f0 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -2,16 +2,20 @@ using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.Services; +using ManagerService.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Mqtt.Client.AspNetCore.Services; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NSwag.Annotations; using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; +using static System.Net.Mime.MediaTypeNames; namespace ManagerService.Controllers { @@ -20,15 +24,17 @@ namespace ManagerService.Controllers [OpenApiTag("Section", Description = "Section management")] public class SectionController : ControllerBase { + private ResourceDatabaseService _resourceService; private SectionDatabaseService _sectionService; private ConfigurationDatabaseService _configurationService; private readonly ILogger _logger; private readonly IConfiguration _configuration; - public SectionController(IConfiguration configuration, ILogger logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService) + public SectionController(IConfiguration configuration, ILogger logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService, ResourceDatabaseService resourceService) { _logger = logger; _configuration = configuration; + _resourceService = resourceService; _sectionService = sectionService; _configurationService = configurationService; } @@ -659,6 +665,430 @@ namespace ManagerService.Controllers } } + /// + /// Update section image url order + /// + [ProducesResponseType(typeof(string), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut("imageURL")] + public ObjectResult UpdateImageURL() + { + try + { + List
sections = _sectionService.GetAll("65ccc67265373befd15be511"); // get by instance ID (hardcoded MDLF) + foreach (var section in sections) + { + section.ImageSource = CheckAndUpdateURL(section.ImageSource, _resourceService); + switch (section.Type) + { + case SectionType.Map: + var mapSection = JsonConvert.DeserializeObject(section.Data); + mapSection.iconSource = CheckAndUpdateURL(mapSection.iconSource, _resourceService); + foreach (var point in mapSection.points) + { + point.imageUrl = CheckAndUpdateURL(point.imageUrl, _resourceService); + + foreach (var content in point.contents) + { + content.resourceUrl = CheckAndUpdateURL(content.resourceUrl, _resourceService); + } + } + section.Data = JsonConvert.SerializeObject(mapSection); // Include all info from specific section as JSON + + break; + case SectionType.Slider: + var sliderData = JsonConvert.DeserializeObject(section.Data); + foreach (var content in sliderData.contents) + { + content.resourceUrl = CheckAndUpdateURL(content.resourceUrl, _resourceService); + } + section.Data = JsonConvert.SerializeObject(sliderData); // Include all info from specific section as JSON + + break; + case SectionType.Video: + break; + case SectionType.Web: + break; + case SectionType.Quizz: + var quizData = JsonConvert.DeserializeObject(section.Data); + foreach (var badLevelLabel in quizData.bad_level.label) + { + badLevelLabel.resourceUrl = CheckAndUpdateURL(badLevelLabel.resourceUrl, _resourceService); + } + + foreach (var mediumLevelLabel in quizData.medium_level.label) + { + mediumLevelLabel.resourceUrl = CheckAndUpdateURL(mediumLevelLabel.resourceUrl, _resourceService); + } + + foreach (var goodLevelLabel in quizData.good_level.label) + { + goodLevelLabel.resourceUrl = CheckAndUpdateURL(goodLevelLabel.resourceUrl, _resourceService); + } + + foreach (var greatLevelLabel in quizData.great_level.label) + { + greatLevelLabel.resourceUrl = CheckAndUpdateURL(greatLevelLabel.resourceUrl, _resourceService); + } + + foreach (var question in quizData.questions) + { + question.imageBackgroundResourceUrl = CheckAndUpdateURL(question.imageBackgroundResourceUrl, _resourceService); + foreach (var labelQuestion in question.label) + { + labelQuestion.resourceUrl = CheckAndUpdateURL(labelQuestion.resourceUrl, _resourceService); + } + foreach (var labelResponse in question.responses) + { + foreach (var LabelResponseLabel in labelResponse.label) + { + LabelResponseLabel.resourceUrl = CheckAndUpdateURL(LabelResponseLabel.resourceUrl, _resourceService); + } + } + } + section.Data = JsonConvert.SerializeObject(quizData); // Include all info from specific section as JSON + + break; + case SectionType.Menu: + break; + } + // Update DB + //_sectionService.Update(section.Id, section); + } + + return new ObjectResult("OK, done") { StatusCode = 200 }; + } + catch (ArgumentNullException ex) + { + return new BadRequestObjectResult(ex.Message) { }; + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + private string CheckAndUpdateURL(string sourceURL, ResourceDatabaseService resourceDatabaseService) + { + if (sourceURL != null && (sourceURL.Contains("192.168.1.19") || sourceURL.Contains("localhost"))) + { + if (sourceURL.Contains("localhost")) + { + } + string[] segments = sourceURL.Split('/'); + string sourceIDFromURL = segments[segments.Length - 1]; + Resource resource = resourceDatabaseService.GetById(sourceIDFromURL); + + return resource != null ? resource.Url : sourceURL; + } else return sourceURL; + } + + /// + /// Create a new sections from json + /// + /// New section info + [ProducesResponseType(typeof(SectionDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 409)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost("json")] + public ObjectResult CreateFromJSON() + { + try + { + + string json = System.IO.File.ReadAllText("C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/Aout 2024/TabletDb.Sections.json"); + var sections = JsonConvert.DeserializeObject(json); + + foreach (var sectionJSON in sections) + { + // Todo add some verification ? + Section section = new Section(); + section.Id = sectionJSON.id; + section.InstanceId = "65ccc67265373befd15be511"; // MDLF hardcoded + section.Label = sectionJSON.label; + section.ImageId = sectionJSON.imageId; + section.ImageSource = sectionJSON.imageSource; + section.ConfigurationId = sectionJSON.configurationId; + section.DateCreation = sectionJSON.dateCreation; + section.IsSubSection = sectionJSON.isSubSection; + section.ParentId = sectionJSON.parentId; + section.Type = sectionJSON.type; + section.Title = sectionJSON.title; + section.Description = sectionJSON.description; + section.Order = _sectionService.GetAllFromConfiguration(section.ConfigurationId).Count; + section.IsBeacon = false; + + // Preparation + //List languages = _configuration.GetSection("SupportedLanguages").Get>(); + + var contentArticle = new List(); + + /*section.Title = LanguageInit.Init("Title", languages); + section.Description = LanguageInit.Init("Description", languages); + contentArticle = LanguageInit.Init("Content", languages); + + section.Title = section.Title.OrderBy(t => t.language).ToList(); + section.Description = section.Description.OrderBy(d => d.language).ToList();*/ + + switch (section.Type) + { + case SectionType.Map: + var sectionDataMap = JsonConvert.DeserializeObject(sectionJSON.data); + var mapDTO = new MapDTO(); + + mapDTO.mapType = sectionDataMap.mapType; + mapDTO.mapProvider = MapProvider.Google; + mapDTO.zoom = sectionDataMap.zoom; + mapDTO.iconSource = sectionDataMap.iconSource; + mapDTO.iconResourceId = sectionDataMap.iconResourceId; + + //mapDTO.points = sectionDataMap.points ?? new List(); + + List geoPoints = new List(); + + JObject sectionMapp = JObject.Parse(sectionJSON.data); + foreach (var point in sectionMapp["points"]) + { + GeoPointDTO pointDTO = new GeoPointDTO(); + pointDTO.id = (int)point["id"]; + pointDTO.latitude = (string)point["latitude"]; + pointDTO.longitude = (string)point["longitude"]; + pointDTO.title = JsonConvert.DeserializeObject>(point["title"].ToString()); + pointDTO.description = JsonConvert.DeserializeObject>(point["description"].ToString()); + List contents = new List(); + + foreach (var image in point["images"]) + { + ContentGeoPoint contentDTO = new ContentGeoPoint(); + contentDTO.resourceType = ResourceType.Image; // ce n'est que des images.. + contentDTO.resourceId = (string)image["imageResourceId"]; + contentDTO.resourceUrl = (string)image["imageSource"]; + + contents.Add(contentDTO); + } + pointDTO.contents = contents; + + geoPoints.Add(pointDTO); + } + + mapDTO.points = geoPoints; + mapDTO.categories = new List(); + section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON + break; + case SectionType.Slider: + //var sectionSlider = JsonConvert.DeserializeObject(sectionJSON.data); + var sliderDTO = new SliderDTO(); + JObject sectionSlider = JObject.Parse(sectionJSON.data); + + List newContents = new List(); + + if (sectionSlider["images"].Count() > 0) + { + } + + foreach (var image in sectionSlider["images"]) + { + ContentDTO newContent = new ContentDTO(); + int order = (int)image["order"]; + string resourceId = (string)image["resourceId"]; + string source = (string)image["source"]; + + newContent.order = order; + newContent.resourceId = resourceId; + newContent.resourceUrl = source; + newContent.resourceType = ResourceType.Image; // ONLY IMAGE WAS POSSIBLE + + newContent.title = JsonConvert.DeserializeObject>(image["title"].ToString()); + newContent.description = JsonConvert.DeserializeObject>(image["description"].ToString()); + newContents.Add(newContent); + } + sliderDTO.contents = newContents; + section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON + break; + case SectionType.Video: + var sectionVideo = JsonConvert.DeserializeObject(sectionJSON.data); + VideoDTO videoDTO = new VideoDTO(); + videoDTO.source = sectionVideo.source; + section.Data = JsonConvert.SerializeObject(videoDTO); // Include all info from specific section as JSON + break; + case SectionType.Web: + var sectionWeb = JsonConvert.DeserializeObject(sectionJSON.data); + WebDTO webDTO = new WebDTO(); + webDTO.source = sectionWeb.source; + section.Data = JsonConvert.SerializeObject(webDTO); // Include all info from specific section as JSON + break; + case SectionType.Menu: + var sectionMenu = JsonConvert.DeserializeObject(sectionJSON.data); + MenuDTO menuDTO = new MenuDTO(); + menuDTO.sections = sectionMenu.sections; + foreach (var sectionMenuu in menuDTO.sections) + { + sectionMenuu.instanceId = section.InstanceId; + switch (sectionMenuu.type) + { + case SectionType.Map: + var sectionMAPSub = JsonConvert.DeserializeObject(sectionMenuu.data); + var mapDTOSUB = new MapDTO(); + + mapDTOSUB.mapType = sectionMAPSub.mapType; + mapDTOSUB.mapProvider = MapProvider.Google; + mapDTOSUB.zoom = sectionMAPSub.zoom; + mapDTOSUB.iconSource = sectionMAPSub.iconSource; + mapDTOSUB.iconResourceId = sectionMAPSub.iconResourceId; + + List geoPointsSUB = new List(); + + JObject sectionMappSUB = JObject.Parse(sectionMenuu.data); + foreach (var pointSUB in sectionMappSUB["points"]) + { + GeoPointDTO pointDTOSUB = new GeoPointDTO(); + pointDTOSUB.id = (int)pointSUB["id"]; + pointDTOSUB.latitude = (string)pointSUB["latitude"]; + pointDTOSUB.longitude = (string)pointSUB["longitude"]; + pointDTOSUB.title = JsonConvert.DeserializeObject>(pointSUB["title"].ToString()); + pointDTOSUB.description = JsonConvert.DeserializeObject>(pointSUB["description"].ToString()); + List contentsSUB = new List(); + + foreach (var imageSUB in pointSUB["images"]) + { + ContentGeoPoint contentDTOSUB = new ContentGeoPoint(); + contentDTOSUB.resourceType = ResourceType.Image; // ce n'est que des images.. + contentDTOSUB.resourceId = (string)imageSUB["imageResourceId"]; + contentDTOSUB.resourceUrl = (string)imageSUB["imageSource"]; + + contentsSUB.Add(contentDTOSUB); + } + pointDTOSUB.contents = contentsSUB; + + geoPointsSUB.Add(pointDTOSUB); + } + + mapDTOSUB.points = geoPointsSUB; + mapDTOSUB.categories = new List(); + section.Data = JsonConvert.SerializeObject(mapDTOSUB); // Include all info from specific section as JSON + + break; + case SectionType.Slider: + + var sliderDTOSUB = new SliderDTO(); + JObject sliderSUB = JObject.Parse(sectionMenuu.data); + + List newContentsSUB = new List(); + + foreach (var image in sliderSUB["images"]) + { + ContentDTO newContentSUB = new ContentDTO(); + int order = (int)image["order"]; + string resourceId = (string)image["resourceId"]; + string source = (string)image["source"]; // TODO REPLACE SOURCE.. UPLOAD TO FIREBASE WHEN UPLOAD RESSOURCE FROM JSON.. + + newContentSUB.order = order; + newContentSUB.resourceId = resourceId; + newContentSUB.resourceUrl = source; + newContentSUB.resourceType = ResourceType.Image; // ONLY IMAGE WAS POSSIBLE + + newContentSUB.title = JsonConvert.DeserializeObject>(image["title"].ToString()); + newContentSUB.description = JsonConvert.DeserializeObject>(image["description"].ToString()); + newContentsSUB.Add(newContentSUB); + } + + sliderDTOSUB.contents = newContentsSUB; + sectionMenuu.data = JsonConvert.SerializeObject(sliderDTOSUB); + break; + case SectionType.Video: + var sectionVideoSUB = JsonConvert.DeserializeObject(sectionMenuu.data); + VideoDTO videoDTOSUB = new VideoDTO(); + videoDTOSUB.source = sectionVideoSUB.source; + section.Data = JsonConvert.SerializeObject(videoDTOSUB); // Include all info from specific section as JSON + break; + case SectionType.Web: + var sectionWebSUB = JsonConvert.DeserializeObject(sectionMenuu.data); + WebDTO webDTOSUB = new WebDTO(); + webDTOSUB.source = sectionWebSUB.source; + section.Data = JsonConvert.SerializeObject(webDTOSUB); // Include all info from specific section as JSON + break; + case SectionType.Quizz: + var sectionSUBQuizz = JsonConvert.DeserializeObject(sectionMenuu.data); + var quizzSUBDTO = new QuizzDTO(); + quizzSUBDTO.questions = sectionSUBQuizz.questions; + quizzSUBDTO.bad_level = sectionSUBQuizz.bad_level; + quizzSUBDTO.medium_level = sectionSUBQuizz.medium_level; + quizzSUBDTO.good_level = sectionSUBQuizz.medium_level; + quizzSUBDTO.great_level = sectionSUBQuizz.great_level; + + sectionMenuu.data = JsonConvert.SerializeObject(quizzSUBDTO); // Include all info from specific section as JSON + break; + } + } + section.Data = JsonConvert.SerializeObject(menuDTO); // Include all info from specific section as JSON + break; + case SectionType.Quizz: + var sectionQuizz = JsonConvert.DeserializeObject(sectionJSON.data); + QuizzDTO quizzDTO = new QuizzDTO(); + quizzDTO.questions = sectionQuizz.questions; + quizzDTO.bad_level = sectionQuizz.bad_level; + quizzDTO.medium_level = sectionQuizz.medium_level; + quizzDTO.good_level = sectionQuizz.medium_level; + quizzDTO.great_level = sectionQuizz.great_level; + + section.Data = JsonConvert.SerializeObject(quizzDTO); // Include all info from specific section as JSON + break; + + + + // NEW CONTENTS AFTER MDLF + case SectionType.Article: + ArticleDTO articleDTO = new ArticleDTO(); + articleDTO.contents = new List(); + articleDTO.content = contentArticle; + //articleDTO.audioIds = LanguageInit.Init("Audio", languages, true); + + section.Data = JsonConvert.SerializeObject(articleDTO); // Include all info from specific section as JSON + break; + case SectionType.PDF: + PdfDTO pdfDTO = new PdfDTO(); + section.Data = JsonConvert.SerializeObject(pdfDTO); // Include all info from specific section as JSON + break; + case SectionType.Puzzle: + PuzzleDTO puzzleDTO = new PuzzleDTO(); + section.Data = JsonConvert.SerializeObject(puzzleDTO); // Include all info from specific section as JSON + break; + case SectionType.Agenda: + AgendaDTO agendaDTO = new AgendaDTO(); + section.Data = JsonConvert.SerializeObject(agendaDTO); // Include all info from specific section as JSON + break; + case SectionType.Weather: + WeatherDTO weatherDTO = new WeatherDTO(); + section.Data = JsonConvert.SerializeObject(weatherDTO); // Include all info from specific section as JSON + break; + } + + //Section sectionCreated = _sectionService.Create(section); + } + + return new OkObjectResult(true); + } + catch (ArgumentNullException ex) + { + return new BadRequestObjectResult(ex.Message) { }; + } + catch (InvalidOperationException ex) + { + return new ConflictObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + /// /// Useless, just to generate dto code /// diff --git a/ManagerService/ManagerService.csproj b/ManagerService/ManagerService.csproj index e110133..b555d02 100644 --- a/ManagerService/ManagerService.csproj +++ b/ManagerService/ManagerService.csproj @@ -7,6 +7,8 @@ + + diff --git a/ManagerService/Services/FirebaseService.cs b/ManagerService/Services/FirebaseService.cs new file mode 100644 index 0000000..26c16fb --- /dev/null +++ b/ManagerService/Services/FirebaseService.cs @@ -0,0 +1,18 @@ +using FirebaseAdmin; +using Google.Apis.Auth.OAuth2; + +namespace ManagerService.Services +{ + public class FirebaseService + { + public FirebaseService() + { + // Remplace le chemin par le chemin vers ton fichier JSON + var pathToKey = "C:/Users/ThomasFransolet/Documents/Documents/Perso/MuseeDeLaFraise/mymuseum-3b97f-firebase-adminsdk-sdbbn-7ec3e24a91.json"; + FirebaseApp.Create(new AppOptions() + { + Credential = GoogleCredential.FromFile(pathToKey), + }); + } + } +} diff --git a/ManagerService/Services/FirebaseStorageService.cs b/ManagerService/Services/FirebaseStorageService.cs new file mode 100644 index 0000000..c6e6bb6 --- /dev/null +++ b/ManagerService/Services/FirebaseStorageService.cs @@ -0,0 +1,64 @@ +using Firebase.Storage; +using System; +using System.IO; +using System.Threading.Tasks; + +namespace ManagerService.Services +{ + + + public class FirebaseStorageService + { + private readonly FirebaseStorage _firebaseStorage; + + public FirebaseStorageService() + { + // Remplace "your-project-id.appspot.com" par l'identifiant de ton bucket Firebase Storage + _firebaseStorage = new FirebaseStorage("mymuseum-3b97f.appspot.com"); + } + + public async Task UploadFileAsync(Stream fileStream, string fileName, string instanceId) + { + // Téléversement du fichier dans le dossier "uploads" + var task = _firebaseStorage + .Child("pictures") + .Child(instanceId) + .Child(fileName) + .PutAsync(fileStream); + + // Optionnel: Suivre la progression du téléversement + task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %"); + + // Attendre la fin du téléversement + var downloadUrl = await task; + + return downloadUrl; + } + + public async Task UploadBase64Async(string base64String, string fileName, string instanceId) + { + // Convertir la chaîne base64 en un tableau de bytes + byte[] bytes = Convert.FromBase64String(base64String); + + // Créer un flux de mémoire à partir des bytes + using (var stream = new MemoryStream(bytes)) + { + // Téléversement du fichier dans le dossier "uploads" + var task = _firebaseStorage + .Child("pictures") + .Child(instanceId) + .Child(fileName) + .PutAsync(stream); + + // Optionnel: Suivre la progression du téléversement + task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %"); + + // Attendre la fin du téléversement + var downloadUrl = await task; + + return downloadUrl; + } + } + } + +} diff --git a/ManagerService/Startup.cs b/ManagerService/Startup.cs index 373c057..4b6f702 100644 --- a/ManagerService/Startup.cs +++ b/ManagerService/Startup.cs @@ -1,3 +1,4 @@ +using Firebase.Storage; using Manager.Framework.Business; using Manager.Framework.Models; using Manager.Helpers; @@ -6,6 +7,7 @@ using Manager.Services; using ManagerService.Extensions; using ManagerService.Service; using ManagerService.Service.Services; +using ManagerService.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; @@ -145,6 +147,8 @@ namespace ManagerService services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.