-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
UploadFileTypeValidator.cs
80 lines (67 loc) · 3.09 KB
/
UploadFileTypeValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Web.Composing;
namespace Umbraco.Web.PropertyEditors
{
internal class UploadFileTypeValidator : IValueValidator
{
public IEnumerable<ValidationResult> Validate(object value, string valueType, object dataTypeConfiguration)
{
string selectedFiles = null;
if (value is JObject jobject && jobject["selectedFiles"] is JToken jToken)
{
selectedFiles = jToken.ToString();
}
else if (valueType?.InvariantEquals(ValueTypes.String) == true)
{
selectedFiles = value as string;
if (string.IsNullOrWhiteSpace(selectedFiles))
yield break;
}
var fileNames = selectedFiles?.Split(Constants.CharArrays.Comma, StringSplitOptions.RemoveEmptyEntries);
if (fileNames == null || !fileNames.Any())
yield break;
foreach (string filename in fileNames)
{
if (IsValidFileExtension(filename) is false || IsAllowedInDataTypeConfiguration(filename, dataTypeConfiguration) is false)
{
//we only store a single value for this editor so the 'member' or 'field'
// we'll associate this error with will simply be called 'value'
yield return new ValidationResult(Current.Services.TextService.Localize("errors", "dissallowedMediaType"), new[] { "value" });
}
}
}
internal static bool IsValidFileExtension(string fileName)
{
if (TryGetFileExtension(fileName, out var extension) is false) return false;
return Current.Configs.Settings().Content.IsFileAllowedForUpload(extension);
}
internal static bool IsAllowedInDataTypeConfiguration(string filename, object dataTypeConfiguration)
{
if (TryGetFileExtension(filename, out var extension) is false) return false;
if (dataTypeConfiguration is FileUploadConfiguration fileUploadConfiguration)
{
// If FileExtensions is empty no allowed extensions has been specified, so we allow everything.
// If there is any extensions specified, we need to check that the uploaded extension is one of them.
return fileUploadConfiguration.FileExtensions.IsCollectionEmpty() ||
fileUploadConfiguration.FileExtensions.Any(x => x.Value.InvariantEquals(extension));
}
return false;
}
internal static bool TryGetFileExtension(string fileName, out string extension)
{
extension = null;
if (fileName.IndexOf('.') <= 0) return false;
extension = fileName.GetFileExtension().TrimStart(".");
return true;
}
}
}