From 27f8725dd0d77a4d5c81a4453aa0c20fa74f5989 Mon Sep 17 00:00:00 2001 From: JRD-OneBeyond <150130293+JRD-OneBeyond@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:38:32 +0000 Subject: [PATCH] Update file validators to handle mp3 and rtf formats (#123) Handle case where very small files causes an exception to be thrown on validation --- .../FileSignatureValidator.cs | 5 ++++- .../FileValidatorBuilder.cs | 3 ++- .../OfficeValidators/WordValidator.cs | 9 +++++++-- .../OtherValidators/Mp3Validator.cs | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OtherValidators/Mp3Validator.cs diff --git a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileSignatureValidator.cs b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileSignatureValidator.cs index 893e982..befa66e 100644 --- a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileSignatureValidator.cs +++ b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileSignatureValidator.cs @@ -86,7 +86,10 @@ public virtual void ValidateFileContent(string fileName, string contentType, byt ValidateFileContent( fileName, contentType, - (sizeToRead) => new ReadOnlySpan(content, 0, sizeToRead).ToArray()); + (sizeToRead) => new ReadOnlySpan(content, 0, sizeToRead > content.Length + ? content.Length + : sizeToRead + ).ToArray()); } /// diff --git a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileValidatorBuilder.cs b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileValidatorBuilder.cs index 3cd0a05..5358c1d 100644 --- a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileValidatorBuilder.cs +++ b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/FileValidatorBuilder.cs @@ -74,7 +74,8 @@ public FileValidatorBuilder AllowVideo() .AddValidator(new MpegValidator()); public FileValidatorBuilder AllowAudio() - => AddValidator(new WavValidator()); + => AddValidator(new WavValidator()) + .AddValidator(new Mp3Validator()); public FileValidatorBuilder AllowVisio() => AddValidator(new VisioValidator()); diff --git a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OfficeValidators/WordValidator.cs b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OfficeValidators/WordValidator.cs index a0db283..c62c479 100644 --- a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OfficeValidators/WordValidator.cs +++ b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OfficeValidators/WordValidator.cs @@ -9,8 +9,13 @@ public sealed class WordValidator : FileSignatureValidator public WordValidator() : base( "application/msword", - new[] { ".doc" }, - "D0-CF-11-E0-A1-B1-1A-E1") + new[] { ".doc", ".rtf" }, + new[] { + "D0-CF-11-E0-A1-B1-1A-E1", + // rtf will have different signatures depending on which program created them + "50-4B-03-04-14", + "7B-5C-72-74-66" + }) { } } diff --git a/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OtherValidators/Mp3Validator.cs b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OtherValidators/Mp3Validator.cs new file mode 100644 index 0000000..f086008 --- /dev/null +++ b/src/OneBeyond.Studio.Crosscuts/Utilities/FileUploadValidators/OtherValidators/Mp3Validator.cs @@ -0,0 +1,17 @@ +namespace OneBeyond.Studio.Crosscuts.Utilities.FileUploadValidators.OtherValidators; + +public sealed class Mp3Validator : FileSignatureValidator +{ + public Mp3Validator() + : base( + "audio/mpeg", + new[] { ".mp3" }, + new[] + { + "FF-FB", + "49-44-33", + "FF-F2" + }) + { + } +} \ No newline at end of file