aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Validation/FileExistsAttribute.cs
blob: 8493b783bcf66c2293427c1257d418111701cebf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace slskd.Validation
{
    using System.ComponentModel.DataAnnotations;
    using System.IO;

    public class FileExistsAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                var file = Path.GetFullPath(value?.ToString());

                if (!string.IsNullOrEmpty(file) && !File.Exists(file))
                {
                    return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent file '{file}'.");
                }
            }

            return ValidationResult.Success;
        }
    }
}