Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Consent Export] Include file-type extensions; Convert webm to wav #2900

Merged
merged 11 commits into from
Feb 13, 2024
27 changes: 22 additions & 5 deletions Backend/Controllers/SpeakerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public async Task<IActionResult> 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));
}
Expand Down Expand Up @@ -145,8 +152,8 @@ public async Task<IActionResult> 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);
}
Expand Down Expand Up @@ -232,9 +239,12 @@ public async Task<IActionResult> 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"))
{
Expand All @@ -245,8 +255,15 @@ public async Task<IActionResult> 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);
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 14 additions & 2 deletions Backend/Helper/FileStorage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;

namespace BackendFramework.Helper
Expand Down Expand Up @@ -96,9 +97,20 @@ public static string GenerateAvatarFilePath(string userId)
/// Generate the path to where Consent audio/images are stored.
/// </summary>
/// <exception cref="InvalidIdException"> Throws when id invalid. </exception>
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);
}

/// <summary>
/// Get the path of a Consent audio/images, or null if it doesn't exist.
/// </summary>
/// <exception cref="InvalidIdException"> Throws when id invalid. </exception>
public static string? GetConsentFilePath(string speakerId)
{
var searchPattern = $"*{Sanitization.SanitizeId(speakerId)}*";
return Directory.GetFiles(GenerateDirPath(ConsentDir, true), searchPattern).FirstOrDefault();
}

/// <summary>
Expand Down
17 changes: 12 additions & 5 deletions Backend/Services/LiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,19 @@ public async Task<string> 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);
}
}
}
}
Expand Down
Loading