1
- using Azure . Storage . Sas ;
2
- using ContentReactor . Audio . Services . Models . Responses ;
3
- using ContentReactor . Audio . Services . Models . Results ;
4
- using ContentReactor . Shared ;
5
- using ContentReactor . Shared . BlobRepository ;
6
- using ContentReactor . Shared . EventSchemas . Audio ;
7
- using System ;
1
+ using System ;
8
2
using System . Collections . Generic ;
9
3
using System . IO ;
10
4
using System . Linq ;
11
5
using System . Threading . Tasks ;
6
+ using Azure ;
7
+ using Azure . Storage . Blobs . Models ;
8
+ using Azure . Storage . Blobs . Specialized ;
9
+ using Azure . Storage . Sas ;
10
+ using ContentReactor . Audio . Services . Models . Responses ;
11
+ using ContentReactor . Audio . Services . Models . Results ;
12
+ using ContentReactor . Shared ;
13
+ using ContentReactor . Shared . BlobHelper ;
14
+ using ContentReactor . Shared . EventSchemas . Audio ;
12
15
13
16
namespace ContentReactor . Audio . Services
14
17
{
@@ -24,7 +27,7 @@ public interface IAudioService
24
27
25
28
public class AudioService : IAudioService
26
29
{
27
- protected IBlobRepository BlobRepository ;
30
+ protected BlobHelper BlobHelper ;
28
31
protected IAudioTranscriptionService AudioTranscriptionService ;
29
32
protected IEventGridPublisherService EventGridPublisherService ;
30
33
@@ -34,59 +37,51 @@ public class AudioService : IAudioService
34
37
protected internal const string UserIdMetadataName = "userId" ;
35
38
protected internal const int TranscriptPreviewLength = 100 ;
36
39
37
- public AudioService ( IBlobRepository blobRepository , IAudioTranscriptionService audioTranscriptionService , IEventGridPublisherService eventGridPublisherService )
40
+ public AudioService ( BlobHelper blobHelper , IAudioTranscriptionService audioTranscriptionService , IEventGridPublisherService eventGridPublisherService )
38
41
{
39
- BlobRepository = blobRepository ;
42
+ BlobHelper = blobHelper ;
40
43
AudioTranscriptionService = audioTranscriptionService ;
41
44
EventGridPublisherService = eventGridPublisherService ;
42
45
}
43
46
44
47
public ( string id , string url ) BeginAddAudioNote ( string userId )
45
48
{
46
49
// generate an ID for this image note
47
- var audioId = Guid . NewGuid ( ) . ToString ( ) ;
50
+ string audioId = Guid . NewGuid ( ) . ToString ( ) ;
48
51
49
52
// create a blob placeholder (which will not have any contents yet)
50
- var blob = BlobRepository . CreatePlaceholderBlob ( AudioBlobContainerName , audioId ) ;
51
-
52
- // get a SAS token to allow the client to write the blob
53
- BlobSasBuilder sasBuilder = new BlobSasBuilder
54
- {
55
- StartsOn = DateTime . UtcNow . AddMinutes ( - 5 ) ,
56
- ExpiresOn = DateTime . UtcNow . AddHours ( 24 ) ,
57
- } ;
53
+ BlockBlobClient blob = BlobHelper . GetBlobClient ( AudioBlobContainerName , audioId ) ;
58
54
59
- sasBuilder . SetPermissions ( BlobSasPermissions . Create ) ;
60
- sasBuilder . SetPermissions ( BlobSasPermissions . Write ) ;
61
55
62
- var url = BlobRepository . GetSasTokenForBlob ( blob , sasBuilder ) ;
56
+ string urlAndSas = BlobHelper . GetSasUriForBlob ( blob , BlobSasPermissions . Create | BlobSasPermissions . Write ) ;
63
57
64
- return ( audioId , url ) ;
58
+ return ( audioId , urlAndSas ) ;
65
59
}
66
60
67
61
public async Task < CompleteAddAudioNoteResult > CompleteAddAudioNoteAsync ( string audioId , string userId , string categoryId )
68
62
{
69
- var imageBlob = await BlobRepository . GetBlobAsync ( AudioBlobContainerName , audioId ) ;
70
- if ( imageBlob == null || ! await BlobRepository . BlobExistsAsync ( imageBlob ) )
63
+ BlockBlobClient imageBlob = BlobHelper . GetBlobClient ( AudioBlobContainerName , audioId ) ;
64
+ if ( imageBlob == null || ! await imageBlob . ExistsAsync ( ) )
71
65
{
72
66
// the blob hasn't actually been uploaded yet, so we can't process it
73
67
return CompleteAddAudioNoteResult . AudioNotUploaded ;
74
68
}
75
69
76
- var response = await imageBlob . GetPropertiesAsync ( ) ;
70
+ // if the blob already contains metadata then that means it has already been added
71
+ Response < BlobProperties > response = await imageBlob . GetPropertiesAsync ( ) ;
77
72
if ( response . Value . Metadata . ContainsKey ( CategoryIdMetadataName ) )
78
73
{
79
74
return CompleteAddAudioNoteResult . AudioAlreadyCreated ;
80
75
}
81
76
82
77
// set the blob metadata
83
- var metaData = new Dictionary < string , string >
78
+ var metadata = new Dictionary < string , string >
84
79
{
85
80
{ CategoryIdMetadataName , categoryId } ,
86
81
{ UserIdMetadataName , userId }
87
82
} ;
88
83
89
- await BlobRepository . UpdateBlobMetadataAsync ( imageBlob , metaData ) ;
84
+ await imageBlob . SetMetadataAsync ( metadata ) ;
90
85
91
86
// publish an event into the Event Grid topic
92
87
var subject = $ "{ userId } /{ audioId } ";
@@ -98,37 +93,29 @@ public async Task<CompleteAddAudioNoteResult> CompleteAddAudioNoteAsync(string a
98
93
public async Task < AudioNoteDetails > GetAudioNoteAsync ( string id , string userId )
99
94
{
100
95
// get the blob, if it exists
101
- var audioBlob = await BlobRepository . GetBlobAsync ( AudioBlobContainerName , id ) ;
96
+ BlockBlobClient audioBlob = BlobHelper . GetBlobClient ( AudioBlobContainerName , id ) ;
102
97
if ( audioBlob == null )
103
98
{
104
99
return null ;
105
100
}
106
101
107
- // get a SAS token for the blob
108
- BlobSasBuilder sasBuilder = new BlobSasBuilder
109
- {
110
- StartsOn = DateTime . UtcNow . AddMinutes ( - 5 ) ,
111
- ExpiresOn = DateTime . UtcNow . AddHours ( 24 ) ,
112
- } ;
113
-
114
- sasBuilder . SetPermissions ( BlobSasPermissions . Read ) ;
115
- var audioUrl = BlobRepository . GetSasTokenForBlob ( audioBlob , sasBuilder ) ;
102
+ string audioUrlAndSas = BlobHelper . GetSasUriForBlob ( audioBlob , BlobSasPermissions . Read ) ;
116
103
117
104
// get the transcript out of the blob metadata
118
- var response = await audioBlob . GetPropertiesAsync ( ) ;
105
+ Response < BlobProperties > response = await audioBlob . GetPropertiesAsync ( ) ;
119
106
response . Value . Metadata . TryGetValue ( TranscriptMetadataName , out var transcript ) ;
120
107
121
108
return new AudioNoteDetails
122
109
{
123
110
Id = id ,
124
- AudioUrl = audioUrl ,
111
+ AudioUrl = audioUrlAndSas ,
125
112
Transcript = transcript
126
113
} ;
127
114
}
128
115
129
116
public async Task < AudioNoteSummaries > ListAudioNotesAsync ( string userId )
130
117
{
131
- var blobs = await BlobRepository . ListBlobsAsync ( AudioBlobContainerName ) ;
118
+ IList < BlobItem > blobs = await BlobHelper . ListBlobsAsync ( AudioBlobContainerName ) ;
132
119
var blobSummaries = blobs
133
120
. Select ( b => new AudioNoteSummary
134
121
{
@@ -146,7 +133,8 @@ public async Task<AudioNoteSummaries> ListAudioNotesAsync(string userId)
146
133
public async Task DeleteAudioNoteAsync ( string id , string userId )
147
134
{
148
135
// delete the blog
149
- await BlobRepository . DeleteBlobAsync ( AudioBlobContainerName , id ) ;
136
+ BlockBlobClient blob = BlobHelper . GetBlobClient ( AudioBlobContainerName , id ) ;
137
+ await blob . DeleteIfExistsAsync ( DeleteSnapshotsOption . IncludeSnapshots ) ;
150
138
151
139
// fire an event into the Event Grid topic
152
140
var subject = $ "{ userId } /{ id } ";
@@ -156,7 +144,7 @@ public async Task DeleteAudioNoteAsync(string id, string userId)
156
144
public async Task < string > UpdateAudioTranscriptAsync ( string id , string userId )
157
145
{
158
146
// get the blob
159
- var audioBlob = await BlobRepository . GetBlobAsync ( AudioBlobContainerName , id ) ;
147
+ BlockBlobClient audioBlob = BlobHelper . GetBlobClient ( AudioBlobContainerName , id ) ;
160
148
if ( audioBlob == null )
161
149
{
162
150
return null ;
@@ -166,21 +154,21 @@ public async Task<string> UpdateAudioTranscriptAsync(string id, string userId)
166
154
string transcript ;
167
155
using ( var audioBlobStream = new MemoryStream ( ) )
168
156
{
169
- await BlobRepository . DownloadBlobAsync ( audioBlob , audioBlobStream ) ;
157
+ await audioBlob . DownloadToAsync ( audioBlobStream ) ;
170
158
171
159
// send to Cognitive Services and get back a transcript
172
160
transcript = await AudioTranscriptionService . GetAudioTranscriptFromCognitiveServicesAsync ( audioBlobStream ) ;
173
161
}
174
162
175
163
// update the blob's metadata
176
- var metaData = new Dictionary < string , string >
164
+ var metadata = new Dictionary < string , string >
177
165
{
178
166
{ TranscriptMetadataName , transcript }
179
167
} ;
180
168
181
- await audioBlob . SetMetadataAsync ( metaData ) ;
169
+ await audioBlob . SetMetadataAsync ( metadata ) ;
182
170
// create a preview form of the transcript
183
- var transcriptPreview = transcript . Truncate ( TranscriptPreviewLength ) ;
171
+ string transcriptPreview = transcript . Truncate ( TranscriptPreviewLength ) ;
184
172
185
173
// fire an event into the Event Grid topic
186
174
var subject = $ "{ userId } /{ id } ";
0 commit comments