|
1 | 1 | # ByteGuard.FileValidator.Extensions.DependencyInjection  |
2 | 2 |
|
| 3 | +`ByteGuard.FileValidator.Extensions.DependencyInjection` provides first-class integration of `ByteGuard.FileValidator` with `Microsoft.Extensions.DependencyInjection`. |
| 4 | + |
| 5 | +It gives you: |
| 6 | +- Extension methods to register the file validator in the DI container |
| 7 | +- Easy configuration via appsettings.json or fluent configuration in code |
| 8 | + |
| 9 | +> This package is the `Microsoft.Extensions.DependencyInjection` integration layer. |
| 10 | +> The core validation logic lives in [`ByteGuard.FileValidator`](https://github.com/ByteGuard-HQ/byteguard-file-validator-net). |
| 11 | +
|
| 12 | +## Getting Started |
| 13 | + |
| 14 | +### Installation |
| 15 | +This package is published and installed via NuGet. |
| 16 | + |
| 17 | +Reference the package in your project: |
| 18 | +```bash |
| 19 | +dotnet add package ByteGuard.FileValidator.Extensions.DependencyInjection |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Add to DI container |
| 25 | +In your `Program.cs` (or `Startup.cs` in older projects), register the validator: |
| 26 | + |
| 27 | +```csharp |
| 28 | +using ByteGuard.FileValidator; |
| 29 | +using ByteGuard.FileValidator.Extensions.DependencyInjection; |
| 30 | + |
| 31 | +// Using inline configuration |
| 32 | +builder.Services.AddFileValidator(options => |
| 33 | +{ |
| 34 | + options.AllowFileTypes(FileExtensions.Pdf, FileExtensions.Jpg, FileExtensions.Png); |
| 35 | + options.FileSizeLimit = ByteSize.MegaBytes(25); |
| 36 | + options.ThrowOnInvalidFiles(false); |
| 37 | +}); |
| 38 | + |
| 39 | +// Using configuration from appsettings.json |
| 40 | +builder.Services.AddFileValidator(options => configuration.GetSection("FileValidatorConfiguration").Bind(options)); |
| 41 | +``` |
| 42 | + |
| 43 | +### Injection & Usage |
| 44 | +You can then inject `FileValidator` into your services and other classes. |
| 45 | + |
| 46 | +```csharp |
| 47 | +public class MyService |
| 48 | +{ |
| 49 | + private readonly FileValidator _fileValidator; |
| 50 | + |
| 51 | + public MyService(FileValidator fileValidator) |
| 52 | + { |
| 53 | + _fileValidator = fileValidator; |
| 54 | + } |
| 55 | + |
| 56 | + public bool SaveFile(Stream fileStream, string fileName) |
| 57 | + { |
| 58 | + var isValid = _fileValidator.IsValidFile(fileName, fileStream); |
| 59 | + |
| 60 | + // ... |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Configuration via appsettings |
| 66 | +It's possible to configure the `FileValidator` through `appsettings.json`. |
| 67 | + |
| 68 | +> _ℹ️ As you'll notice below, you can either define the `FileSizeLimit` in raw byte size, or use the `UnitFileSizeLimit` to define |
| 69 | +> the file size in a more human readable format. When both are defined, `FileSizeLimit` always wins over `UnitFileSizeLimit`._ |
| 70 | +
|
| 71 | +```json |
| 72 | +{ |
| 73 | + "FileValidatorConfiguration": { |
| 74 | + "SupportedFileTypes": [ ".pdf", ".jpg", ".png" ], |
| 75 | + "FileSizeLimit": 26214400, |
| 76 | + "UnitFileSizeLimit": "25MB", |
| 77 | + "ThrowExceptionOnInvalidFile": true |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## License |
| 83 | +_ByteGuard.FileValidator.Extensions.DpendencyInjection is Copyright © ByteGuard Contributors - Provided under the MIT license._ |
0 commit comments