-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathAzureBlobUpload.cs
More file actions
131 lines (86 loc) · 3.92 KB
/
AzureBlobUpload.cs
File metadata and controls
131 lines (86 loc) · 3.92 KB
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
using System;
using AdaptiveExpressions.Properties;
using Azure.Storage.Blobs.Models;
using Bot.Builder.Community.Components.Azure.BlobUpload.Upload;
using Microsoft.Bot.Builder.Dialogs;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Components.Azure.BlobUpload
{
public class AzureBlobUpload : Dialog
{
[JsonProperty("$Kind")]
public const string Kind = "AzureBlobUpload";
[JsonProperty("FileUrl")]
public StringExpression FileUrl { get; set; }
[JsonProperty("FileName")]
public StringExpression FileName { get; set; }
[JsonProperty("PublicAccessType")]
public EnumExpression<PublicAccessType> PublicAccessType { get; set; }
[JsonProperty("DeleteSnapshotsOption")]
public EnumExpression<DeleteSnapshotsOption> DeleteSnapshotsOption { get; set; }
[JsonProperty("Containers")]
public StringExpression Containers { get; set; }
[JsonProperty("ConnectionString")]
public StringExpression ConnectionString { get; set; }
[JsonProperty("resultProperty")]
public StringExpression ResultProperty { get; set; }
IAzureUpload _dataUpload;
public AzureBlobUpload([CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0) : base()
{
RegisterSourceLocation(sourceFilePath, sourceLineNumber);
}
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null,
CancellationToken cancellationToken = new CancellationToken())
{
IsPropertyIsValid(dc);
CreateDataStorage(dc);
var getUrl = FileUrl?.GetValue(dc.State);
var fileName = FileName?.GetValue(dc.State);
var result = await _dataUpload.UploadAsync(getUrl, fileName);
if (ResultProperty != null)
{
dc.State.SetValue(this.ResultProperty.GetValue(dc.State), result);
}
return await dc.EndDialogAsync(result: result, cancellationToken: cancellationToken);
}
private void CreateDataStorage(DialogContext dc)
{
if (_dataUpload == null)
return;
var containerName = Containers?.GetValue(dc.State);
var accessType = PublicAccessType?.GetValue(dc.State);
var deleteSnapshotsOption = DeleteSnapshotsOption?.GetValue(dc.State);
Enum.TryParse(accessType.ToString(), out PublicAccessType publicAccessType);
Enum.TryParse(deleteSnapshotsOption.ToString(), out DeleteSnapshotsOption deleteSnapshotsType);
var connectionString = ConnectionString?.GetValue(dc.State);
_dataUpload = new AzureUpload(connectionString, containerName, publicAccessType, deleteSnapshotsType);
}
private void IsPropertyIsValid(DialogContext dc)
{
var getUrl = FileUrl?.GetValue(dc.State);
if (string.IsNullOrEmpty(getUrl))
{
throw new Exception($"{nameof(AzureBlobUpload)} : FileUrl is required");
}
var fileName = FileName?.GetValue(dc.State);
if (string.IsNullOrEmpty(fileName))
{
throw new Exception($"{nameof(AzureBlobUpload)} : FileName is required");
}
var container = Containers?.GetValue(dc.State);
if (string.IsNullOrEmpty(container))
{
throw new Exception($"{nameof(AzureBlobUpload)} : Container name is required");
}
var connectionString = ConnectionString?.GetValue(dc.State);
if (string.IsNullOrEmpty(connectionString))
{
throw new Exception($"{nameof(AzureBlobUpload)} : connection string is required");
}
}
}
}