-
Notifications
You must be signed in to change notification settings - Fork 205
/
FileTriggerBinding.cs
207 lines (179 loc) · 7.78 KB
/
FileTriggerBinding.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.Bindings;
using Microsoft.Azure.WebJobs.Extensions.Files.Listener;
using Microsoft.Azure.WebJobs.Files.Listeners;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.Azure.WebJobs.Extensions.Files.Bindings
{
internal class FileTriggerBinding : ITriggerBinding
{
private readonly ParameterInfo _parameter;
private readonly FileTriggerAttribute _attribute;
private readonly IOptions<FilesOptions> _options;
private readonly IReadOnlyDictionary<string, Type> _bindingContract;
private readonly BindingDataProvider _bindingDataProvider;
private readonly IFileProcessorFactory _fileProcessorFactory;
private readonly ILogger _logger;
public FileTriggerBinding(IOptions<FilesOptions> options, ParameterInfo parameter, ILogger logger, IFileProcessorFactory fileProcessorFactory)
{
_options = options;
_parameter = parameter;
_logger = logger;
_fileProcessorFactory = fileProcessorFactory;
_attribute = parameter.GetCustomAttribute<FileTriggerAttribute>(inherit: false);
_bindingDataProvider = BindingDataProvider.FromTemplate(_attribute.Path);
_bindingContract = CreateBindingContract();
}
public IReadOnlyDictionary<string, Type> BindingDataContract
{
get
{
return _bindingContract;
}
}
public Type TriggerValueType
{
get { return typeof(FileSystemEventArgs); }
}
public Task<ITriggerData> BindAsync(object value, ValueBindingContext context)
{
FileSystemEventArgs fileEvent = value as FileSystemEventArgs;
if (fileEvent == null)
{
string filePath = value as string;
fileEvent = GetFileArgsFromString(filePath);
}
IValueBinder valueBinder = new FileValueBinder(_parameter, fileEvent);
IReadOnlyDictionary<string, object> bindingData = GetBindingData(fileEvent);
return Task.FromResult<ITriggerData>(new TriggerData(valueBinder, bindingData));
}
internal static FileSystemEventArgs GetFileArgsFromString(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
// TODO: This only supports Created events. For Dashboard invocation, how can we
// handle Change events?
string directory = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
return new FileSystemEventArgs(WatcherChangeTypes.Created, directory, fileName);
}
return null;
}
public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return Task.FromResult<IListener>(new FileListener(_options, _attribute, context.Executor, _logger, _fileProcessorFactory));
}
public ParameterDescriptor ToParameterDescriptor()
{
// These path values are validated later during startup.
string triggerPath = Path.Combine(_options.Value.RootPath ?? string.Empty, _attribute.Path ?? string.Empty);
return new FileTriggerParameterDescriptor
{
Name = _parameter.Name,
DisplayHints = new ParameterDisplayHints
{
Prompt = "Enter a file path",
Description = string.Format("File event occurred on path '{0}'", _attribute.GetRootPath()),
DefaultValue = triggerPath
}
};
}
private IReadOnlyDictionary<string, Type> CreateBindingContract()
{
Dictionary<string, Type> contract = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
contract.Add("FileTrigger", typeof(FileSystemEventArgs));
if (_bindingDataProvider.Contract != null)
{
foreach (KeyValuePair<string, Type> item in _bindingDataProvider.Contract)
{
// In case of conflict, binding data from the value type overrides the built-in binding data above.
contract[item.Key] = item.Value;
}
}
return contract;
}
private IReadOnlyDictionary<string, object> GetBindingData(FileSystemEventArgs value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
// built in binding data
Dictionary<string, object> bindingData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
bindingData.Add("FileTrigger", value);
string pathRoot = Path.GetDirectoryName(_attribute.Path);
int idx = value.FullPath.IndexOf(pathRoot, StringComparison.OrdinalIgnoreCase);
string pathToMatch = value.FullPath.Substring(idx);
// binding data from the path template
IReadOnlyDictionary<string, object> bindingDataFromPath = _bindingDataProvider.GetBindingData(pathToMatch);
if (bindingDataFromPath != null)
{
foreach (KeyValuePair<string, object> item in bindingDataFromPath)
{
// In case of conflict, binding data from the path overrides
// the built-in binding data above.
bindingData[item.Key] = item.Value;
}
}
return bindingData;
}
private class FileTriggerParameterDescriptor : TriggerParameterDescriptor
{
public override string GetTriggerReason(IDictionary<string, string> arguments)
{
string fullPath = null;
if (arguments != null && arguments.TryGetValue(Name, out fullPath))
{
return string.Format("File change detected for file '{0}'", fullPath);
}
return null;
}
}
private class FileValueBinder : StreamValueBinder
{
private readonly ParameterInfo _parameter;
private readonly FileSystemEventArgs _fileEvent;
public FileValueBinder(ParameterInfo parameter, FileSystemEventArgs fileEvent)
: base(parameter)
{
_parameter = parameter;
_fileEvent = fileEvent;
}
public override async Task<object> GetValueAsync()
{
if (_parameter.ParameterType == typeof(FileSystemEventArgs))
{
return _fileEvent;
}
else if (_parameter.ParameterType == typeof(FileInfo))
{
return new FileInfo(_fileEvent.FullPath);
}
return await base.GetValueAsync();
}
protected override Stream GetStream()
{
return File.OpenRead(_fileEvent.FullPath);
}
public override string ToInvokeString()
{
return _fileEvent.FullPath;
}
}
}
}