Skip to content

Commit

Permalink
Merge branch 'master' into standalone-install
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrady committed Feb 28, 2024
2 parents 8be4441 + 4979ed8 commit 9565fa6
Show file tree
Hide file tree
Showing 42 changed files with 765 additions and 632 deletions.
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
11 changes: 3 additions & 8 deletions Backend/Models/MergeWordSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ public MergeWordSet()
Id = "";
ProjectId = "";
UserId = "";
WordIds = new List<string>();
WordIds = new();
}

public MergeWordSet Clone()
{
var clone = new MergeWordSet
return new()
{
Id = Id,
ProjectId = ProjectId,
UserId = UserId,
WordIds = new List<string>()
WordIds = WordIds.Select(id => id).ToList()
};
foreach (var id in WordIds)
{
clone.WordIds.Add(id);
}
return clone;
}

public bool ContentEquals(MergeWordSet other)
Expand Down
26 changes: 7 additions & 19 deletions Backend/Models/MergeWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class MergeWords

public MergeWords()
{
Parent = new Word();
Children = new List<MergeSourceWord>();
Parent = new();
Children = new();
DeleteOnly = false;
}
}
Expand Down Expand Up @@ -59,8 +59,8 @@ public class MergeUndoIds

public MergeUndoIds()
{
ParentIds = new List<string>();
ChildIds = new List<string>();
ParentIds = new();
ChildIds = new();
}

public MergeUndoIds(List<string> parentIds, List<string> childIds)
Expand All @@ -71,23 +71,11 @@ public MergeUndoIds(List<string> parentIds, List<string> childIds)

public MergeUndoIds Clone()
{
var clone = new MergeUndoIds
return new()
{
ParentIds = new List<string>(),
ChildIds = new List<string>()
ParentIds = ParentIds.Select(id => id).ToList(),
ChildIds = ChildIds.Select(id => id).ToList()
};

foreach (var id in ParentIds)
{
clone.ParentIds.Add(id);
}

foreach (var id in ChildIds)
{
clone.ChildIds.Add(id);
}

return clone;
}

public bool ContentEquals(MergeUndoIds other)
Expand Down
85 changes: 23 additions & 62 deletions Backend/Models/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ public Project()
DefinitionsEnabled = false;
GrammaticalInfoEnabled = false;
AutocompleteSetting = AutocompleteSetting.On;
SemDomWritingSystem = new WritingSystem();
VernacularWritingSystem = new WritingSystem();
AnalysisWritingSystems = new List<WritingSystem>();
SemanticDomains = new List<SemanticDomain>();
ValidCharacters = new List<string>();
RejectedCharacters = new List<string>();
CustomFields = new List<CustomField>();
WordFields = new List<string>();
PartsOfSpeech = new List<string>();
InviteTokens = new List<EmailInvite>();
WorkshopSchedule = new List<DateTime>();
SemDomWritingSystem = new();
VernacularWritingSystem = new();
AnalysisWritingSystems = new();
SemanticDomains = new();
ValidCharacters = new();
RejectedCharacters = new();
CustomFields = new();
WordFields = new();
PartsOfSpeech = new();
InviteTokens = new();
WorkshopSchedule = new();
}

public Project Clone()
{
var clone = new Project
return new()
{
Id = Id,
Name = Name,
Expand All @@ -118,55 +118,16 @@ public Project Clone()
AutocompleteSetting = AutocompleteSetting,
SemDomWritingSystem = SemDomWritingSystem.Clone(),
VernacularWritingSystem = VernacularWritingSystem.Clone(),
AnalysisWritingSystems = new List<WritingSystem>(),
SemanticDomains = new List<SemanticDomain>(),
ValidCharacters = new List<string>(),
RejectedCharacters = new List<string>(),
CustomFields = new List<CustomField>(),
WordFields = new List<string>(),
PartsOfSpeech = new List<string>(),
InviteTokens = new List<EmailInvite>(),
WorkshopSchedule = new List<DateTime>(),
AnalysisWritingSystems = AnalysisWritingSystems.Select(ws => ws.Clone()).ToList(),
SemanticDomains = SemanticDomains.Select(sd => sd.Clone()).ToList(),
ValidCharacters = ValidCharacters.Select(vc => vc).ToList(),
RejectedCharacters = RejectedCharacters.Select(rc => rc).ToList(),
CustomFields = CustomFields.Select(cf => cf.Clone()).ToList(),
WordFields = WordFields.Select(wf => wf).ToList(),
PartsOfSpeech = PartsOfSpeech.Select(ps => ps).ToList(),
InviteTokens = InviteTokens.Select(it => it.Clone()).ToList(),
WorkshopSchedule = WorkshopSchedule.Select(dt => dt).ToList(),
};

foreach (var aw in AnalysisWritingSystems)
{
clone.AnalysisWritingSystems.Add(aw.Clone());
}
foreach (var sd in SemanticDomains)
{
clone.SemanticDomains.Add(sd.Clone());
}
foreach (var cs in ValidCharacters)
{
clone.ValidCharacters.Add(cs);
}
foreach (var cs in RejectedCharacters)
{
clone.RejectedCharacters.Add(cs);
}
foreach (var cf in CustomFields)
{
clone.CustomFields.Add(cf.Clone());
}
foreach (var wf in WordFields)
{
clone.WordFields.Add(wf);
}
foreach (var pos in PartsOfSpeech)
{
clone.PartsOfSpeech.Add(pos);
}
foreach (var it in InviteTokens)
{
clone.InviteTokens.Add(it.Clone());
}
foreach (var dt in WorkshopSchedule)
{
clone.WorkshopSchedule.Add(dt);
}

return clone;
}

public bool ContentEquals(Project other)
Expand Down Expand Up @@ -362,8 +323,8 @@ public class UserCreatedProject

public UserCreatedProject()
{
Project = new Project();
User = new User();
Project = new();
User = new();
}
}

Expand Down
12 changes: 3 additions & 9 deletions Backend/Models/SemanticDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,15 @@ public SemanticDomainFull()
Name = "";
Id = "";
Description = "";
Questions = new List<string>();
Questions = new();
Lang = "";
}

public new SemanticDomainFull Clone()
{
var clone = (SemanticDomainFull)base.Clone();
clone.Description = Description;
clone.Questions = new List<string>();

foreach (var question in Questions)
{
clone.Questions.Add(question);
}

clone.Questions = Questions.Select(q => q).ToList();
return clone;
}

Expand Down Expand Up @@ -173,7 +167,7 @@ public SemanticDomainTreeNode(SemanticDomain sd)
Lang = sd.Lang;
Name = sd.Name;
Id = sd.Id;
Children = new List<SemanticDomain>();
Children = new();
}
}
}
39 changes: 10 additions & 29 deletions Backend/Models/Sense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,25 @@ public Sense()
// By default generate a new, unique Guid for each new Sense.
Guid = Guid.NewGuid();
Accessibility = Status.Active;
GrammaticalInfo = new GrammaticalInfo();
Definitions = new List<Definition>();
Glosses = new List<Gloss>();
ProtectReasons = new List<ProtectReason>();
SemanticDomains = new List<SemanticDomain>();
GrammaticalInfo = new();
Definitions = new();
Glosses = new();
ProtectReasons = new();
SemanticDomains = new();
}

public Sense Clone()
{
var clone = new Sense
return new()
{
Guid = Guid,
Accessibility = Accessibility,
GrammaticalInfo = GrammaticalInfo.Clone(),
Definitions = new List<Definition>(),
Glosses = new List<Gloss>(),
ProtectReasons = new List<ProtectReason>(),
SemanticDomains = new List<SemanticDomain>(),
Definitions = Definitions.Select(d => d.Clone()).ToList(),
Glosses = Glosses.Select(g => g.Clone()).ToList(),
ProtectReasons = ProtectReasons.Select(pr => pr.Clone()).ToList(),
SemanticDomains = SemanticDomains.Select(sd => sd.Clone()).ToList(),
};

foreach (var definition in Definitions)
{
clone.Definitions.Add(definition.Clone());
}
foreach (var gloss in Glosses)
{
clone.Glosses.Add(gloss.Clone());
}
foreach (var reason in ProtectReasons)
{
clone.ProtectReasons.Add(reason.Clone());
}
foreach (var sd in SemanticDomains)
{
clone.SemanticDomains.Add(sd.Clone());
}

return clone;
}

public override bool Equals(object? obj)
Expand Down
Loading

0 comments on commit 9565fa6

Please sign in to comment.