diff --git a/Backend/Controllers/SpeakerController.cs b/Backend/Controllers/SpeakerController.cs index 5b99f812a3..54a0f57e8d 100644 --- a/Backend/Controllers/SpeakerController.cs +++ b/Backend/Controllers/SpeakerController.cs @@ -116,6 +116,13 @@ public async Task DeleteSpeaker(string projectId, string speakerI return NotFound(speakerId); } + // Delete consent file + var path = FileStorage.GetConsentFilePath(speakerId); + if (path is not null) + { + IO.File.Delete(path); + } + // Delete speaker and return success return Ok(await _speakerRepo.Delete(projectId, speakerId)); } @@ -145,8 +152,8 @@ public async Task RemoveConsent(string projectId, string speakerI { return StatusCode(StatusCodes.Status304NotModified, speakerId); } - var path = FileStorage.GenerateConsentFilePath(speaker.Id); - if (IO.File.Exists(path)) + var path = FileStorage.GetConsentFilePath(speaker.Id); + if (path is not null) { IO.File.Delete(path); } @@ -232,9 +239,12 @@ public async Task UploadConsent( { return BadRequest("Empty File"); } + + var extension = IO.Path.GetExtension(file.FileName) ?? ""; if (file.ContentType.Contains("audio")) { speaker.Consent = ConsentType.Audio; + extension = ".webm"; } else if (file.ContentType.Contains("image")) { @@ -245,8 +255,15 @@ public async Task UploadConsent( return BadRequest("File should be audio or image"); } + // Delete old consent file + var old = FileStorage.GetConsentFilePath(speaker.Id); + if (old is not null) + { + IO.File.Delete(old); + } + // Copy file data to a new local file - var path = FileStorage.GenerateConsentFilePath(speakerId); + var path = FileStorage.GenerateConsentFilePath(speakerId, extension); await using (var fs = new IO.FileStream(path, IO.FileMode.OpenOrCreate)) { await file.CopyToAsync(fs); @@ -284,8 +301,8 @@ public IActionResult DownloadConsent(string speakerId) } // Ensure file exists - var path = FileStorage.GenerateConsentFilePath(speakerId); - if (!IO.File.Exists(path)) + var path = FileStorage.GetConsentFilePath(speakerId); + if (path is null) { return NotFound(speakerId); } diff --git a/Backend/Helper/FileStorage.cs b/Backend/Helper/FileStorage.cs index 60b02ffa4b..124c658978 100644 --- a/Backend/Helper/FileStorage.cs +++ b/Backend/Helper/FileStorage.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using System.Runtime.Serialization; namespace BackendFramework.Helper @@ -96,9 +97,20 @@ public static string GenerateAvatarFilePath(string userId) /// Generate the path to where Consent audio/images are stored. /// /// Throws when id invalid. - public static string GenerateConsentFilePath(string speakerId) + public static string GenerateConsentFilePath(string speakerId, string? extension = null) { - return GenerateFilePath(ConsentDir, Sanitization.SanitizeId(speakerId)); + var fileName = Path.ChangeExtension(Sanitization.SanitizeId(speakerId), extension); + return GenerateFilePath(ConsentDir, fileName); + } + + /// + /// Get the path of a Consent audio/images, or null if it doesn't exist. + /// + /// Throws when id invalid. + public static string? GetConsentFilePath(string speakerId) + { + var searchPattern = $"*{Sanitization.SanitizeId(speakerId)}*"; + return Directory.GetFiles(GenerateDirPath(ConsentDir, true), searchPattern).FirstOrDefault(); } /// diff --git a/Backend/Services/LiftService.cs b/Backend/Services/LiftService.cs index 9acb4a412d..d9f082b2d0 100644 --- a/Backend/Services/LiftService.cs +++ b/Backend/Services/LiftService.cs @@ -347,12 +347,19 @@ public async Task LiftExport( { if (speaker.Consent != ConsentType.None) { - var src = FileStorage.GenerateConsentFilePath(speaker.Id); - if (File.Exists(src)) + var src = FileStorage.GetConsentFilePath(speaker.Id); + if (src is not null) { - var dest = Path.Combine(consentDir, speaker.Id); - File.Copy(src, dest, true); - + var dest = Path.Combine(consentDir, Path.GetFileName(src)); + if (Path.GetExtension(dest).Equals(".webm", StringComparison.OrdinalIgnoreCase)) + { + dest = Path.ChangeExtension(dest, ".wav"); + await FFmpeg.Conversions.New().Start($"-y -i \"{src}\" \"{dest}\""); + } + else + { + File.Copy(src, dest); + } } } }