diff --git a/AudioCuesheetEditor/Model/IO/Audio/Audiofile.cs b/AudioCuesheetEditor/Model/IO/Audio/Audiofile.cs index f42de7dc..a390feda 100644 --- a/AudioCuesheetEditor/Model/IO/Audio/Audiofile.cs +++ b/AudioCuesheetEditor/Model/IO/Audio/Audiofile.cs @@ -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)) { diff --git a/AudioCuesheetEditor/Model/Utility/IOUtility.cs b/AudioCuesheetEditor/Model/Utility/IOUtility.cs index 51105b97..450d14de 100644 --- a/AudioCuesheetEditor/Model/Utility/IOUtility.cs +++ b/AudioCuesheetEditor/Model/Utility/IOUtility.cs @@ -39,16 +39,28 @@ public static Boolean CheckFileMimeType(IFileEntry file, String mimeType, String return fileMimeTypeMatches; } - public static Boolean CheckFileMimeType(IFileEntry file, IReadOnlyCollection 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; } } } diff --git a/AudioCuesheetEditor/Pages/ViewModeImport.razor b/AudioCuesheetEditor/Pages/ViewModeImport.razor index 74352bd2..7ed3ac7d 100644 --- a/AudioCuesheetEditor/Pages/ViewModeImport.razor +++ b/AudioCuesheetEditor/Pages/ViewModeImport.razor @@ -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); } @@ -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("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; diff --git a/AudioCuesheetEditor/Shared/CuesheetData.razor b/AudioCuesheetEditor/Shared/CuesheetData.razor index 8923b230..c6533636 100644 --- a/AudioCuesheetEditor/Shared/CuesheetData.razor +++ b/AudioCuesheetEditor/Shared/CuesheetData.razor @@ -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); } @@ -262,7 +262,7 @@ along with Foobar. If not, see if (file != null) { var audioFileObjectURL = await _jsRuntime.InvokeAsync("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;