diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-04-14 16:17:24 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-14 16:17:24 -0500 |
| commit | 220189e375930731add0147bc7f74a60c68877e0 (patch) | |
| tree | 30ea22537f5d95ff84f12b571ef699e7607e8703 /src | |
| parent | 12e7e2cb42c95d1d3d673af9d19ba8bc0248c7d8 (diff) | |
| parent | cf7cf09745e21ba82f341f4f5386c792dd6cf184 (diff) | |
| download | slskd-220189e375930731add0147bc7f74a60c68877e0.tar.gz | |
Merge pull request #1114 from slskd/file-exists
Add FileAccess to FileExists attribute, test files for access
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/FileExistsAttribute.cs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/slskd/Common/Validation/FileExistsAttribute.cs b/src/slskd/Common/Validation/FileExistsAttribute.cs index c1b3fd15..762cd6b3 100644 --- a/src/slskd/Common/Validation/FileExistsAttribute.cs +++ b/src/slskd/Common/Validation/FileExistsAttribute.cs @@ -25,15 +25,41 @@ namespace slskd.Validation /// </summary> public class FileExistsAttribute : ValidationAttribute { + public FileExistsAttribute() + { + } + + public FileExistsAttribute(FileAccess fileAccess) + { + FileAccess = fileAccess; + } + + private FileAccess? FileAccess { get; } = null; + protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { var file = Path.GetFullPath(value?.ToString()); - if (!string.IsNullOrEmpty(file) && !File.Exists(file)) + if (!string.IsNullOrEmpty(file)) { - return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent file '{file}'."); + if (!File.Exists(file)) + { + return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent file '{file}'."); + } + + if (FileAccess is not null) + { + try + { + File.Open(file, FileMode.Open, FileAccess.Value).Dispose(); + } + catch (IOException) + { + return new ValidationResult($"The {validationContext.DisplayName} field specifies a file '{file}' that cannot be opened for required access '{FileAccess}'"); + } + } } } |
