Skip to content

Commit

Permalink
Bugfix for finding audio codec from mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoCoderMatrix86 committed Mar 26, 2024
1 parent ec30866 commit c7a129d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Model/IO/Audio/Audiofile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Audiofile(String name, Boolean isRecorded = false)
IsRecorded = isRecorded;
}

public Audiofile(String name, String objectURL, AudioCodec audioCodec, HttpClient httpClient, Boolean isRecorded = false) : this(name, isRecorded)
public Audiofile(String name, String objectURL, AudioCodec? audioCodec, HttpClient httpClient, Boolean isRecorded = false) : this(name, isRecorded)
{
if (String.IsNullOrEmpty(objectURL))
{
Expand Down
26 changes: 19 additions & 7 deletions AudioCuesheetEditor/Model/Utility/IOUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,28 @@ public static Boolean CheckFileMimeType(IFileEntry file, String mimeType, String
return fileMimeTypeMatches;
}

public static Boolean CheckFileMimeType(IFileEntry file, IReadOnlyCollection<AudioCodec> audioCodecs)
public static Boolean CheckFileMimeTypeForAudioCodec(IFileEntry file)
{
Boolean fileMimeTypeMatches = false;
if ((file != null) && (audioCodecs != null))
return GetAudioCodec(file) != null;
}

public static AudioCodec? GetAudioCodec(IFileEntry fileEntry)
{
AudioCodec? foundAudioCodec = null;
var extension = Path.GetExtension(fileEntry.Name).ToLower();
// First search with mime type and file extension
var audioCodecsFound = Audiofile.AudioCodecs.Where(x => x.MimeType.Equals(fileEntry.Type, StringComparison.OrdinalIgnoreCase) && x.FileExtension.Equals(extension, StringComparison.OrdinalIgnoreCase));
if (audioCodecsFound.Count() <= 1)
{
var extension = Path.GetExtension(file.Name).ToLower();
var audioCodecsFound = audioCodecs.Where(x => x.MimeType.Equals(file.Type, StringComparison.OrdinalIgnoreCase) || x.FileExtension.Equals(extension, StringComparison.OrdinalIgnoreCase));
fileMimeTypeMatches = (audioCodecsFound != null) && (audioCodecsFound.Any());
foundAudioCodec = audioCodecsFound.FirstOrDefault();
}
return fileMimeTypeMatches;
else
{
// Second search with mime type or file extension
audioCodecsFound = Audiofile.AudioCodecs.Where(x => x.MimeType.Equals(fileEntry.Type, StringComparison.OrdinalIgnoreCase) || x.FileExtension.Equals(extension, StringComparison.OrdinalIgnoreCase));
foundAudioCodec = audioCodecsFound.FirstOrDefault();
}
return foundAudioCodec;
}
}
}
6 changes: 3 additions & 3 deletions AudioCuesheetEditor/Pages/ViewModeImport.razor
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ along with Foobar. If not, see
if ((IOUtility.CheckFileMimeType(file, Projectfile.MimeType, Projectfile.FileExtension) == false)
&& (IOUtility.CheckFileMimeType(file, Cuesheet.MimeType, Cuesheet.FileExtension) == false)
&& (IOUtility.CheckFileMimeType(file, TextImportfile.MimeType, TextImportfile.FileExtension) == false)
&& (IOUtility.CheckFileMimeType(file, Audiofile.AudioCodecs) == false))
&& (IOUtility.CheckFileMimeTypeForAudioCodec(file) == false))
{
invalidDropFileNames.Add(file.Name);
}
Expand Down Expand Up @@ -528,10 +528,10 @@ along with Foobar. If not, see
_sessionStateContainer.TextImportFile.TimeSpanFormat.ValidateablePropertyChanged += Timespanformat_ValidateablePropertyChanged;
_sessionStateContainer.TextImportFile.TextImportScheme.ValidateablePropertyChanged += TextImportScheme_ValidateablePropertyChanged;
}
if (IOUtility.CheckFileMimeType(file, Audiofile.AudioCodecs) == true)
if (IOUtility.CheckFileMimeTypeForAudioCodec(file) == true)
{
var audioFileObjectURL = await _jsRuntime.InvokeAsync<String>("getObjectURL", "dropFileInput");
var codec = Audiofile.AudioCodecs.Single(x => x.MimeType.Equals(file.Type, StringComparison.OrdinalIgnoreCase));
var codec = IOUtility.GetAudioCodec(file);
var audiofile = new Audiofile(file.Name, audioFileObjectURL, codec, _httpClient);
_ = audiofile.LoadContentStream();
_sessionStateContainer.ImportAudiofile = audiofile;
Expand Down
4 changes: 2 additions & 2 deletions AudioCuesheetEditor/Shared/CuesheetData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ along with Foobar. If not, see
if (Cuesheet != null)
{
var file = e.Files.First();
if (IOUtility.CheckFileMimeType(file, Audiofile.AudioCodecs) == true)
if (IOUtility.CheckFileMimeTypeForAudioCodec(file) == true)
{
await SetAudioFile(file);
}
Expand Down Expand Up @@ -262,7 +262,7 @@ along with Foobar. If not, see
if (file != null)
{
var audioFileObjectURL = await _jsRuntime.InvokeAsync<String>("getObjectURL", fileEditAudiofileId);
var codec = Audiofile.AudioCodecs.Single(x => x.MimeType.Equals(file.Type, StringComparison.OrdinalIgnoreCase));
var codec = IOUtility.GetAudioCodec(file);
var audiofile = new Audiofile(file.Name, audioFileObjectURL, codec, _httpClient);
_ = audiofile.LoadContentStream();
Cuesheet.Audiofile = audiofile;
Expand Down

0 comments on commit c7a129d

Please sign in to comment.