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